/*
	jQuery Coda-Slider v1.1 - http://www.ndoherty.com/coda-slider
	
	Copyright (c) 2007 Niall Doherty
	
	Inspired by the clever folks at http://www.panic.com/coda
	Many thanks to Gian Carlo Mingati. Coda-Slider is a heavily modified version of his slideViewer, which can be found at  http://www.gcmingati.net/wordpress/wp-content/lab/jquery/imagestrip/imageslide-plugin.html
	
	Requirements:
	-  jQuery 1.2 ... available via  http://www.jquery.com
	-  jQuery easing plugin (1.2) ... available via  http://gsgd.co.uk/sandbox/jquery/easing/
	- jQuery easing compatability plugin ... available via  http://gsgd.co.uk/sandbox/jquery/easing/
	- CSS included in index.html
*/

jQuery(function(){
	//jQuery("div.csw").prepend("<p class='loading'>Loading...<br /><img src='images/ajax-loader.gif' alt='loading...'/ ></p>");
});
var j = 0;
jQuery.fn.codaSlider = function(settings) {
	 settings = jQuery.extend({
     easeFunc: "expoinout",
     easeTime: 750,
     toolTip: false
  }, settings);
	return this.each(function(){
		var container = jQuery(this);
		// Remove the preloader gif...
		//container.find("p.loading").remove();
		// Self-explanatory...
		//container.removeClass("csw").addClass("stripViewer");
		// Get the width of a panel, set from CSS...
		var panelWidth = container.find("div.panel").width();
		// panelCount gives us a count of the panels in the container...
		var panelCount = container.find("div.panel").size();
		// Calculate the width of all the panels when lined up end-to-end...
		var stripViewerWidth = panelWidth*panelCount;
		// Use the above width to specify the CSS width for the panelContainer element...
		container.find("div.panelContainer").css("width" , stripViewerWidth);
		// Set the navWidth as a multiple of panelCount to account for margin-right on each li
		var navWidth = panelCount*2;
		
		// Specify the current panel.
		// If the loaded URL has a hash (cross-linking), we're going to use that hash to give the slider a specific starting position...
		if (location.hash && parseInt(location.hash.slice(1)) <= panelCount) {
			var cPanel = parseInt(location.hash.slice(1));
			var cnt = - (panelWidth*(cPanel - 1));
			jQuery(this).find("div.panelContainer").css({ left: cnt });
		// Otherwise, we'll just set the current panel to 1...
		} else { 
			var cPanel = 1;
		};
		
		var cVid = ""; // flag for currently playing video, null if none
		var isHovering = false; // flag for hover over application, tied with autoplay feature
		
		// check for universal hover
		jQuery("div.sliderWrap").hover(function(){ isHovering = true; }, function(){ isHovering = false; });
		
		// Add click events to handle panels with videos
		/*jQuery(".hasvideo").click(function()
		{
		controlPlayer("block");
		cVid = this.parentNode.id;
		this.style.display = "none" ;
		});*/
		
		// Create appropriate nav
		container.each(function(i) {
			
			jQuery("div.sliderWrap").append
			(
				"<div class='stripNavR' id='stripNavR'><a href='#'><img src='themes/front/default/images/slider/spc.gif' width='31' height='50' border='0' alt='' \/><\/a><\/div>" +
				"<div class='bigphony'><div class='navWindow'><div class='stripNav' id='stripNav'><ul><\/ul><\/div><\/div><\/div>" +
				"<div class='stripNavL' id='stripNavL'><a href='#'><img src='themes/front/default/images/slider/spc.gif' width='30' height='50' border='0' alt='' \/><\/a><\/div>"
			);
			
		
			jQuery(this).find("div.panel").each(function(n) {
				
				//jQuery("div#stripNav ul").append("<li class='tab" + (n+1) + "' style='background:url(slider_imgs/slide" + (n+1) + "_thumb.jpg) center center no-repeat;'><a href='#" + (n+1) + "'>" + "<\/a><\/li>");
				
				if(null != settings.thumbs)
				{
					bgurl = settings.thumbs[n];
				}
				else
				{
					bgurl = '';
				}
				
				jQuery("div#stripNav ul").append("<li class='tab" + (n+1) + "' style='background:url("+bgurl+") center center no-repeat;'><a href='#" + (n+1) + "'>" + "<\/a><\/li>");
			});
			
				
			/*("<li class='tab" + (n+1) + "'><a href='#" + (n+1) + "'>" + "<img src='slider_imgs/slide0" + (n+1) + "_thumb.jpg' width='55' height='35' border='0' alt='' \/><\/a><\/li>");*/
			// the above is the alternate approach
			
			// check for current panel and manipulate thumbs menu and next/prev buttons accordingly
			refreshNav(cPanel, panelCount);
			
			// Tab nav
			jQuery("div#stripNav a").each(function(z) {
				// Figure out the navWidth by adding up the width of each li
				//navWidth += jQuery(this).parent().width();
				navWidth = 372;
				// What happens when a nav link is clicked
				jQuery(this).bind("click", function() {
				  
				  $f('actual_vid').stop();
				  jQuery('#actual_vid').hide();
				  jQuery('#sliderWrap').find("div.hasvideo").show();
				
					jQuery(this).addClass("current").parent().parent().find("a").not(jQuery(this)).removeClass("current"); // wow!
					var cnt = - (panelWidth*(z));
					cPanel = z + 1;
					jQuery("div.panelContainer").animate({ left: cnt}, settings.easeTime, settings.easeFunc);
					refreshNav(cPanel, panelCount);
					// restore videos to "click to play" status
					if (cVid != "")
					{
						controlPlayer("none");
						jQuery("div.panelContainer").find("div#" + cVid + " .hasvideo").css( "display", "block" ) ;
						cVid = "";
						pngfix();
					}
				});
			});

			// Left nav
			jQuery("div#stripNavR a").click(function(){

				$f('actual_vid').stop();
				jQuery('#actual_vid').hide();
				jQuery('#sliderWrap').find("div.hasvideo").show();

				if (cPanel == 1) {
					return false;

				} else {
					cPanel -= 1;
					var cnt = - (panelWidth*(cPanel - 1));
					jQuery(this).parent().parent().parent().find("div.stripNav a.current").removeClass("current").parent().prev().find("a").addClass("current");
					jQuery(this).parent().parent().parent().parent().find("div.panelContainer").animate({ left: cnt}, settings.easeTime, settings.easeFunc);
					// Change the URL hash (cross-linking)...
					location.hash = cPanel;
					refreshNav(cPanel, panelCount);
					// restore videos to "click to play" status
					if (cVid != "")
					{
						controlPlayer("none");
						jQuery("div.panelContainer").find("div#" + cVid + " .hasvideo").css( "display", "block" ) ;
						cVid = "";
						pngfix();
					}
				};
			});
			
			// Right nav
			jQuery("div#stripNavL a").click(function(){
			
			   	$f('actual_vid').stop();
				jQuery('#actual_vid').hide();
				jQuery('#sliderWrap').find("div.hasvideo").show();
			
				if (cPanel == panelCount) {
					return false;

				} else {
					var cnt = - (panelWidth*cPanel);
					cPanel += 1;
					jQuery(this).parent().parent().parent().find("div.stripNav a.current").removeClass("current").parent().next().find("a").addClass("current");
					jQuery(this).parent().parent().parent().parent().find("div.panelContainer").animate({ left: cnt}, settings.easeTime, settings.easeFunc);
					// Change the URL hash (cross-linking)...
					location.hash = cPanel;
					refreshNav(cPanel, panelCount);
					// restore videos to "click to play" status
					if (cVid != "")
					{
						controlPlayer("none");
						jQuery("div.panelContainer").find("div#" + cVid + " .hasvideo").css( "display", "block" ) ;
						cVid = "";
						pngfix();
					}
				};
			});
			
			// Same-page cross-linking
			jQuery("a.cross-link").click(function(){
				jQuery(this).parents().find(".stripNav ul li a:eq(" + (parseInt(jQuery(this).attr("href").slice(1)) - 1) + ")").trigger('click');
			});
			
			// Set the width of the nav using the navWidth figure we calculated earlier. This is so the nav can be centred above the slider
			//jQuery("div#stripNav" + j).css("width" , navWidth);
			
			// Specify which tab is initially set to "current". Depends on if the loaded URL had a hash or not (cross-linking).
			if (location.hash && parseInt(location.hash.slice(1)) <= panelCount) {
				jQuery("div#stripNav a:eq(" + (location.hash.slice(1) - 1) + ")").addClass("current");
			} else {
				jQuery("div#stripNav a:eq(0)").addClass("current");
			}
			
			// idle (no hover or video playing) autoplay
			
			jQuery(document).ready(function(){
			
				jQuery(this).everyTime(10000, function(){
										  
					  if (isHovering == false && cVid == "" && ( $f('actual_vid').getState() != 3 && $f('actual_vid').getState() != 4 ) ) // user is not hovering and no video is playing
					  {
						 $f('actual_vid').stop();
						 jQuery('#actual_vid').hide();
						 jQuery('#sliderWrap').find("div.hasvideo").show();
						  
						  if (cPanel == panelCount)
						  {
							   var cnt = 0;
							   cPanel = 1;
							   jQuery("div.stripNav a.current").removeClass("current").parent().prevAll(".tab1").find("a").addClass("current");
						   }
						   else
						   {
								var cnt = - (panelWidth*cPanel);
								cPanel += 1;
								jQuery("div.stripNav a.current").removeClass("current").parent().next().find("a").addClass("current"); 
						   }
						  
						   jQuery("div.panelContainer").animate({ left: cnt}, settings.easeTime, settings.easeFunc);
						   // Change the URL hash (cross-linking)...
							location.hash = cPanel;
							refreshNav(cPanel, panelCount);
					  }
			  }, 0);
        
			});
			

			
		});
		
		j++;
  });
};

function controlPlayer(cssDisplay)
{
document.getElementById("actual_vid").style.display = cssDisplay ;
}

function refreshNav(cPanel, panelCount)
{
	// first decide whether next/prev buttons should be enabled/disabled
	if (cPanel == 1)
	{
		document.getElementById("stripNavR").style.visibility = "hidden";
		if (panelCount > 1)
		{
			document.getElementById("stripNavL").style.visibility = "visible";
		}
	}
	else if (cPanel == panelCount)
	{
		document.getElementById("stripNavL").style.visibility = "hidden";
	}
	else
	{
		document.getElementById("stripNavR").style.visibility = "visible";
		document.getElementById("stripNavL").style.visibility = "visible";
	}
	// now decide on how to shift nav vertically
	var currentRow = Math.ceil(cPanel / 7);
	var calculatedTop = -50 * (currentRow - 1);
	// shift
	document.getElementById("stripNav").style.top = calculatedTop + "px";
}

function pngfix()
{
	var clear="/homepage3/slider/clear.gif"; //path to clear.gif

	document.write('<script type="text/javascript" id="ct" defer="defer" src="javascript:void(0)"><\/script>');var ct=document.getElementById("ct");ct.onreadystatechange=function(){pngfix()};pngfix=function(){var els=document.getElementsByTagName('*'),ip=/\.png/i,al="progid:DXImageTransform.Microsoft.AlphaImageLoader(src='",i=els.length,uels=new Array(),c=0;while(i-->0){if(els[i].className.match(/unitPng/)){uels[c]=els[i];c++;}}if(uels.length==0)pfx(els);else pfx(uels);function pfx(els){i=els.length;while(i-->0){var el=els[i],es=el.style,elc=el.currentStyle,elb=elc.backgroundImage;if(el.src&&el.src.match(ip)&&!es.filter){es.height=el.height;es.width=el.width;es.filter=al+el.src+"',sizingMethod='crop')";el.src=clear;}else{if(elb.match(ip)){var path=elb.split('"'),rep=(elc.backgroundRepeat=='no-repeat')?'crop':'scale',elkids=el.getElementsByTagName('*'),j=elkids.length;es.filter=al+path[1]+"',sizingMethod='"+rep+"')";es.height=el.clientHeight+'px';es.backgroundImage='none';if(j!=0){if(elc.position!="absolute")es.position='static';while(j-->0)if(!elkids[j].style.position)elkids[j].style.position="relative";}}}}};};
}
