/*
	Logicworks - js/jquery/functions.js

	jQuery-dependent functions
*/



$(document).ready(function(){



	/* Cycle functions (js/jquery/cycle.js)
	------------------------------------------------------------------------ */

	// Add transitions to cycle.js
	(function($) {

		// Add 'scrollVert' functionality for homepage scroll boxes
		// taken from: http://www.malsup.com/jquery/cycle/jquery.cycle.trans.js?v2.22
		$.fn.cycle.transitions.scrollVert = function($cont, $slides, opts) {
		    $cont.css('overflow','hidden');
		//    $slides.show();
		    opts.before.push(function(curr, next, opts, fwd) {
		        $(this).show();
		        var currH = curr.offsetHeight, nextH = next.offsetHeight;
		        opts.cssBefore = fwd ? { top: -nextH } : { top: nextH };
		        opts.animIn.top = 0;
		        opts.animOut.top = fwd ? currH : -currH;
		        $slides.not(curr).css(opts.cssBefore);
		    });
		    opts.cssFirst = { top: 0 };
		    opts.cssAfter = { display: 'none' }
		};

	})(jQuery);




	// Main homepage Slideshow (cycle.js)
	
	var activeIndex = 9999; // counter used
	
	// redefine Cycle's updateActivePagerLink function 
	$.fn.cycle.updateActivePagerLink = function(pager, currSlideIndex) {

		// make "Learn More" links actually go to intended destination rather than just firing the next slide
		if (activeIndex == currSlideIndex) {
                        // 20081222 - CPM: Support new windows
                        var anchor = $(pager).find('li').filter('li:eq('+currSlideIndex+')').find('a');
                        if (anchor.attr('target') == '_blank') {
                            window.open(anchor.attr('href'));
                        } else {
  			    location.href = anchor.attr('href');
                        }
		}

	    $(pager).find('li')
			.filter('li:not('+currSlideIndex+')')
				.animate({height: '41', width: '330', backgroundColor: 'darkred'},600)
				.removeClass('on')
			.filter('li:eq('+currSlideIndex+')')
				.animate({height: '113', width: '450', backgroundColor: 'darkgrey'},650,'easeOutBack')
				.addClass('on');
		activeIndex = currSlideIndex;
	};

        // 07282008 - CPM: Add an extra delay if we just displayed a slide with the CSS class 'delay'
        //                 This is done to give extra tease time for certain slides.
        var animTimeout = 5000;
        var animDelayMultiplier = 1.5; 
        function onBefore(curr,next,opts) {
            if(next.className == 'delay') { 
                opts.timeout = animTimeout * animDelayMultiplier;
            } else if(next.className == 'delay2') { 
                opts.timeout = animTimeout * animDelayMultiplier * 2;
            } else if(opts.timeout != animTimeout) {
                opts.timeout = animTimeout;
            }
        }

	$('#slides ul').after('<div id="navSlides"><ol>').cycle({
		fx: 'fade'
		, speed: 1400
		, timeout: animTimeout
		, pager: '#navSlides ol'
		, pagerEvent: 'click'
		, pagerAnchorBuilder: function(idx, slide) {
			var $content = $('#slides ul li:eq(' + (idx) + ') div.nav').html();
			return '<li><div class="default">' + $content + '</div><div class="extended">' + $content + '</div></li>';
		}
                , before: onBefore
	});
	
	// Pause SLideshow on hover of slide nav
	$('#navSlides').hover(
		function(){ $('#slides ul').cycle('pause'); }
		, function(){ $('#slides ul').cycle('resume'); }
	);

	// Set Opacity of slide nav
	$('#navSlides li').css('opacity','0.90');


	/* End Cycle functions
	------------------------------------------------------------------------ */
	


});

/* EOF */
