/*
	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;
var cnt = 0;
jQuery.fn.codaSlider = function(settings) {
var maxShow = (settings.maxShow)?settings.maxShow:0;
settings = jQuery.extend({easeFunc: "easeOut",easeTime: 750,toolTip: false}, settings);

    jQuery("a#flLeft").hide();
    jQuery("a#flRight").hide();
	return this.each(function(){
	    var showFl = function(sens){
            if((cPanel > 1)&&(panelCount>maxShow)){
                jQuery("a#flLeft").show();
            }
            else{
                jQuery("a#flLeft").hide();
            }
            if(panelCount-cPanel>=maxShow){
                jQuery("a#flRight").show();
            }
            else{
                jQuery("a#flRight").hide();
            }
            if(maxShow==1){
                var oldPanel=(sens==1)?(cPanel - 2):cPanel;
                //alert(cPanel+" "+oldPanel);
                var tmp=jQuery("div.panelContainer .panel:eq(" + oldPanel + ")");
                var progressive=200;
                jQuery("div.panelContainer").animate({ opacity: 0.5}, progressive, settings.easeFunc).animate({ left: cnt}, 400, settings.easeFunc).animate({ opacity: 1}, progressive, settings.easeFunc);
                /*
                jQuery(".actu:eq(0)",tmp).animate({ opacity: 0}, progressive, settings.easeFunc);
                jQuery(".actu:eq(1)",tmp).animate({ opacity: 0}, progressive*2, settings.easeFunc);
                jQuery(".actu:eq(2)",tmp).animate({ opacity: 0}, progressive*3, settings.easeFunc, function(){
                    jQuery("div.panelContainer .panel .actu").css("opacity","0");
                    jQuery("div.panelContainer").animate({ left: cnt}, 0, settings.easeFunc, function(){
                        var tmp=jQuery("div.panelContainer .panel:eq(" + (cPanel - 1) + ")");
                        var progressive=125;
                        jQuery(".actu:eq(0)",tmp).animate({ opacity: 1}, progressive, "easeIn");
                        jQuery(".actu:eq(1)",tmp).animate({ opacity: 1}, progressive*2, "easeIn");
                        jQuery(".actu:eq(2)",tmp).animate({ opacity: 1}, progressive*3, "easeIn");
                        //jQuery("div.panelContainer").animate({ opacity: 1}, settings.easeTime*0.5, settings.easeFunc);
                    });
                });*/
            }
            else{
                jQuery("div.panelContainer").animate({ left: cnt}, settings.easeTime, settings.easeFunc);
            }
            // Change the URL hash (cross-linking)...
            location.hash = cPanel;
            return false;
        }
		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();
		// Specify the current panel.
		// If the loaded URL has a hash (cross-linking) we take the right number for panel
		// Otherwise, we'll just set the current panel to 1...
        var cPanel = (location.hash && parseInt(location.hash.slice(1)) <= panelCount)?parseInt(location.hash.slice(1)):1;
        (cPanel > 1 + panelCount - maxShow)?(cPanel = 1 + panelCount - maxShow):0;
		// Calculate the width of all the panels when lined up end-to-end...
		var stripViewerWidth = 0;
		var k=1;
	    container.find("div.panel").each(
	        function(){
	            (k==cPanel)?(cnt = -stripViewerWidth):0;
	            stripViewerWidth +=$(this).width();
	            k++;
	        }
	    )
		// Use the above width to specify the CSS width for the panelContainer element...
		container.find("div.panelContainer").css("width" , stripViewerWidth);
	    // if we're not on first panel we're going to use that cPanel to give the slider a specific starting position...
		(cPanel>1)?jQuery(this).find("div.panelContainer").css({ left: cnt }):0;
		
		if((cPanel > 1)&&(panelCount>maxShow)){
		    jQuery("a#flLeft").show();
		}
		if(panelCount-cPanel>=maxShow){
		    jQuery("a#flRight").show();
		}
		
		// Create appropriate nav
		container.each(function(i) {
			// Left nav
			jQuery("a#flLeft").click(function(){
				if (cPanel > 1) {
					cPanel -= 1;
					cnt += container.find("div.panel:eq(" + (cPanel - 1) + ")").width();
				};
				return showFl(-1);
			});
			
			// Right nav
			jQuery("a#flRight").click(function(){
				if (cPanel <= panelCount-maxShow) {
				    cnt -= container.find("div.panel:eq(" + (cPanel - 1) + ")").width();
					cPanel += 1;
		        };
				return showFl(1);
			});
			
			// 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');
			});				
		});
		
		j++;
  });
};