/**
 * @author jxu
 */

jQuery(function(){
	jQuery("div.csw").prepend("<p class='loading'>Loading...<br /><img src='images/ajax-loader.gif' alt='loading...'/ ></p>");
});

var j = 0;
var hash = 0;

jQuery.fn.headline = function(settings) {
	 settings = jQuery.extend({
     changingTime:10000,
	 noTab:false,
     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 = 250;
		
		
		// Specify the current panel.
		var cPanel = 1;
		
		// Create appropriate nav
		container.each(function(i) {

			// Create the Left and Right arrows
            //jQuery(this).before("<div class='stripNavL' id='stripNavL" + j + "'><a href='#'>Left</a><\/div>");
			jQuery(this).after("<div class='stripNavL' id='stripNavL" + j + "'><a href='#'>Left</a><\/div><div class='stripNavR' id='stripNavR" + j + "'><a href='#'>Right</a><\/div>");
			
			// Create the Tabs
			if (!settings.noTab) {
				jQuery(this).before("<div class='stripNav' id='stripNav" + j + "'><ul><\/ul><\/div>");
				jQuery(this).find("div.panel").each(function(n){
					jQuery("div#stripNav" + j + " ul").append("<li class='tab" + (n + 1) + "'><a href='#" + (n + 1) + "'>" + jQuery(this).attr("title") + "<\/a><\/li>");
				});
				
				// Tab nav
				jQuery("div#stripNav" + j + " a").each(function(z){
					// Figure out the navWidth by adding up the width of each li
					navWidth += jQuery(this).parent().width();
					// What happens when a nav link is clicked
					jQuery(this).bind("click", function(event){
						event.preventDefault();
						jQuery(this).addClass("current").parent().parent().find("a").not(jQuery(this)).removeClass("current"); // wow!
						cPanel = z + 1;
						jQuery(this).parent().parent().parent().next().find("div.panelContainer").find("div.panel").each(function(i){
							$(this).hide();
						});
						jQuery(this).parent().parent().parent().next().find("div.panelContainer").find("div.panel:eq(" + z + ")").fadeIn();
					});
				});
			}
			
			// Left nav
			jQuery("div#stripNavL" + j + " a").click(function(event){
				event.preventDefault();
				if (cPanel == 1) {
					cPanel = panelCount;
					jQuery(this).parent().parent().find("div.stripNav a.current").removeClass("current").parent().parent().find("li:last a").addClass("current");
				} else {
					cPanel -= 1;
					jQuery(this).parent().parent().find("div.stripNav a.current").removeClass("current").parent().prev().find("a").addClass("current");
				};
				
                jQuery(this).parent().parent().find("div.panelContainer").find("div.panel").each( function(i){ $(this).hide();});
                jQuery(this).parent().parent().find("div.panelContainer").find("div.panel:eq("+(cPanel-1)+")").fadeIn();
                                
				// Change the URL hash (cross-linking)...
				hash = cPanel;
				return false;
			});
			
			// Right nav
			jQuery("div#stripNavR" + j + " a").click(function(event){
				event.preventDefault();
				if (cPanel == panelCount) {
					cPanel = 1;
					jQuery(this).parent().parent().find("div.stripNav a.current").removeClass("current").parent().parent().find("a:eq(0)").addClass("current");
				} else {
					cPanel += 1;
					jQuery(this).parent().parent().find("div.stripNav a.current").removeClass("current").parent().next().find("a").addClass("current");
				};

				jQuery(this).parent().parent().find("div.panelContainer").find("div.panel").each( function(i){ $(this).hide();});
                jQuery(this).parent().parent().find("div.panelContainer").find("div.panel:eq("+(cPanel-1)+")").fadeIn();

				// Change the URL hash (cross-linking)...
				hash = cPanel;
				return false;
			});
			
			// 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 (hash && hash <= panelCount) {
				jQuery("div#stripNav" + j + " a:eq(" + hash + ")").addClass("current");
			} else {
				jQuery("div#stripNav" + j + " a:eq(0)").addClass("current");
			}
		});
		
		container.everyTime(settings.changingTime,"forward", function(){
			jQuery("div#stripNavR0 a").trigger('click');
		});
		
		container.oneTime(1,function(){
			jQuery("div#stripNavR0 a").trigger('click');
		});
		
		j++;
  });
};
