﻿$(document).ready(function(){
	// Fix PNGs
	$.ifixpng('pixel.gif'); 
	$(".pngimg").ifixpng();
	
	/* Configure animated stage area on homepage */
	$('.stage-banners div:first').fadeIn(500, function() {
		$.fn.cycle.updateActivePagerLink = function(pager, currSlideIndex) { 
			// Restore the previous selected img
			var src1 = $(pager).find('li.activeLI > a > img').attr('src');
			if (src1 != undefined) {
				$(pager).find('li.activeLI > a > img').attr('src', src1.replace('selected.png', 'normal.png'));
				$(pager).find('li.activeLI > a > img').ifixpng();
			}
			
			// Set the new selected img
			$(pager).find('li').removeClass('activeLI').filter('li:eq('+currSlideIndex+')').addClass('activeLI');
			src1 = $(pager).find('li.activeLI > a > img').attr('src');
			$(pager).find('li:eq('+currSlideIndex+') img').attr('src', src1.replace('normal.png', 'selected.png').replace('over.png', 'selected.png'));
			$(pager).find('li:eq('+currSlideIndex+') img').ifixpng();
		}; 
		
		$.fn.cycle.pauseAfter = false;
	
		$('.stage-banners').cycle({ 
			fx:          'fade', 
			speed:       1300, 
			timeout:     9000, 
			fastOnEvent: 0,
			pager:      '.stage-icons', 
			pagerEvent: 'click',
			pauseOnPagerHover: true,
			pagerAnchorBuilder: function(idx, slide) {
				// return sel string for existing anchor
				$('.stage-icons > li > a')
				return '.stage-icons li:eq(' + (idx) + ') a';
	        },
	        after: function() {
				var src1 = $('.stage-icons > li.pause-play').find('img').attr('src');
				if (src1 != undefined) {
					if (src1.match(/play/)) {
						$('.stage-banners').cycle('pause');
					}
				}
	        },
	        pagerClick: function() {
	        	//$.fn.cycle.pauseAfter = true;
	        	var src1 = $('.stage-icons > li.pause-play').find('img').attr('src');
				if (src1 != undefined) {
					$('.stage-icons > li.pause-play').find('img').attr('src', src1.replace('pause-over.png', 'play-over.png').replace('pause.png', 'play.png'));
				}
	        }
		});
		
		$('.stage-icons > li').hover(
			function() {
				if (!$(this).hasClass('activeLI')) {
					var src1 = $(this).find('img').attr('src');
					if (src1 != undefined) {
						$(this).find('img').attr('src', src1.replace('normal.png', 'over.png').replace('pause.png', 'pause-over.png').replace('play.png', 'play-over.png'));
						$(this).find('img').ifixpng();
					}
				}
			},
			
			function() {
				var src1 = $(this).find('img').attr('src');
				if (src1 != undefined) {
					$(this).find('img').attr('src', src1.replace('pause-over.png', 'pause.png').replace('play-over.png', 'play.png').replace('over.png', 'normal.png'));
					$(this).find('img').ifixpng();
				}
			}
		);
		
		$('.stage-icons > li.pause-play').click(function(){
			var src1 = $(this).find('img').attr('src');
			if (src1 != undefined) {
				if (src1.match(/pause/)) {
					$('.stage-banners').cycle('pause');
					$(this).find('img').attr('src', src1.replace('pause-over.png', 'play-over.png').replace('pause.png', 'play.png'));
				} else {
					$('.stage-banners').cycle('next');
					$('.stage-banners').cycle('resume');
					$(this).find('img').attr('src', src1.replace('play-over.png', 'pause-over.png').replace('play.png', 'pause.png'));
				}
			}
		});
	});
});	
