/* jQuery Slider (heavily modified by mkafka @ Horn Group) - Niall Doherty */

var j = 0;
jQuery.fn.codaSlider = function(settings) {
	 settings = jQuery.extend({
     easeFunc: "easeInOutExpo",
     easeTime: 750,
     toolTip: false
  }, settings);
	return this.each(function(){


	// sets the number of items on each slider page
	// ['class name', 'number of items per page', 'zebra striping (y/n)']
	var ModulesInfo = [
						['casestudies-module',6,'n'], 
						//['client-module',21,y],
						//['client-module-past',65,y],
						//['featured-module',4,n],
						['thumb-module',12,'n'],
						['list-module',12,'n'],
						['awards-module',8,'n'],
						['recog-module',5,'n']
					];
	
	if ($('.panel-container:has(div[class*="module"])').length > 0){
		var moduleType = $('div[class*="module"]').attr('class');
		for (var i = 0; i < ModulesInfo.length; i++) {
			if(moduleType == ModulesInfo[i][0]){
				var NumItemsPerPage = (ModulesInfo[i][1] + 1);
				var itemNum = 1
				var pageNum = 1
				
				$('.' + moduleType).each(function(){
					if(itemNum == NumItemsPerPage){
						itemNum = 1
						pageNum++
						$(this).attr({alt: pageNum});
						itemNum++
					} else {
						$(this).attr({alt: pageNum});
						itemNum++
					}
				});
				
				for(i = 1; i <= pageNum; i++){
					$('.' + moduleType + '[alt="'+i+'"]').wrapAll('<div class="panel"></div>').removeAttr('alt');
				}
			}
		}
	}

		var container = jQuery(this);
		// Remove the preloader gif...
		container.find("p.loading").remove();
		container.removeClass("csw").addClass("stripViewer");
		// Get the width of a panel, set from CSS...
		// var panelWidth = container.find("div.panel").width();
		var panelWidth;

		if(container.find("div.panel").children('div').hasClass('awards-module') || container.find("div.panel").children('div').hasClass('recog-module')){
			panelWidth = 530;
		} else if(container.find("div.panel").children('div').hasClass('portfolio-piece')) {
			panelWidth = 600;
		} else {
			panelWidth = 915;
		}
		
		// 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 panel-container element...
		container.find("div.panel-container").css("width" , stripViewerWidth);
		// Set the navWidth as a multiple of panelCount to account for margin-right on each li
		var navWidth = panelCount*0+78;
		
		// 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.panel-container").css({ left: cnt });
		// Otherwise, we'll just set the current panel to 1...
		} else { 
			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='stripNavR' id='stripNavR" + j + "'><a href='#'>Right</a><\/div>");
			
			// Create the Tabs
			var tabsNum = 0
			jQuery(this).after("<div class='slider-pages'><div class='stripNav' id='stripNav" + j + "'><ul><\/ul><\/div></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>");												
						jQuery("div#stripNav" + j + " ul").append("<li class='tab" + (n+1) + "'><a onclick='return false' href='#" + (n+1) + "'><\/a><\/li>");	
						tabsNum++
			});
			if(tabsNum == 1){
				$('.stripNav').css({'display':'none'});	
			}
			
			// Create Pagination
			jQuery(".stripNav ul li a").append("<img src='themes/horngroup/images/paginate-btn-middle.png'/>");
			jQuery(".stripNav ul li:first a").empty().append("<img src='themes/horngroup/images/paginate-btn-start.png'/>");
			jQuery(".stripNav ul li:last a").empty().append("<img src='themes/horngroup/images/paginate-btn-end.png'/>");

			var pagename = jQuery('div.panel-container').attr("title");
			
			//if(pagename == 'screenshots'){
				jQuery(".stripNav ul li:last").after("<li class='end-w-mini'><img src='themes/horngroup/images/paginate-end-w-mini.png'/><div id='mininav'><a href='javascript:void(0)' id='prev-proj' class='minileft'><img src='themes/horngroup/images/mini_nav_left_page.png'/></a><a href='javascript:void(0)' id='next-proj' class='miniright'><img src='themes/horngroup/images/mini_nav_right_page.png'/></a></div></li>");
			//} else {
			//	jQuery(".stripNav ul li:last").after("<li style='background-image: none;'><img src='themes/horngroup/images/paginate-end.png'/></li>");
			//}


			if(pagename == 'screenshots'){
				jQuery('.stripNav ul').css({'background':'url(themes/horngroup/images/paginate-start-' + pagename + '.png) 0px 0px no-repeat'});
				jQuery('.stripNav ul').css({'padding':' 0px 0px 0px 95px'});
			} else {
				jQuery('.stripNav ul').css({'background':'url(themes/horngroup/images/paginate-start-' + pagename + '.png) 0px 0px no-repeat'});
				jQuery('.stripNav ul').css({'padding':' 0px 0px 0px 61px'});
			}
			jQuery('div.panel-container').removeAttr("title");
			
//------------------------------------------------------
//-- sets/resets View-specific styles onload and when called --
//------------------------------------------------------
	// creates span for img frames
	$('.thumb-module div').append("<span></span>");
	$('.list-module div').append("<span></span>");
	$('.featured-module div').append("<span></span>");
	$('.casestudies-module div').append("<span></span>");
	
	// creates span for mouseover effects and sets initial properties
	$('.thumb-module a').prepend("<span></span>");
	$('.list-module a').prepend("<span></span>");
	$('.casestudies-module a').prepend("<span></span>");
	
	// corrects _module width
	$('.slider-container:has(.list-module)').css({'width':'915px','margin':'0px 0px 0px 0px','background-color':'#e2e2e2'});
	$('.stripViewer:has(.list-module)').css({'width':'915px','padding':'0px 0px 0px 0px'});
	$('.slider-container:has(.list-module)').children('.slider-pages').css({'margin':'20px 0px 0px 20px'});
	
	$('.slider-container:has(.thumb-module)').css({'width':'915px','margin':'0px 0px 0px 0px','background-color':'#f1f1f1'});
	$('.stripViewer:has(.thumb-module)').css({'width':'855px','padding':'15px 30px 15px 30px','background-color':'#e2e2e2'});
	$('.slider-container:has(.thumb-module)').children('.slider-pages').css({'margin':'20px 0px 0px 30px'});
	
	$('.stripViewer:has(.featured-module)').css({'width':'865px','padding':'0px 0px 0px 0px'});

	$('.stripViewer:has(.portfolio-piece)').css({'width':'600px'});

	$('.stripViewer:has(.client-module)').css({'width':'915px','padding':'0px 0px 0px 0px'});
	$('.slider-container:has(.client-module)').css({'width':'915px','margin':'0px 0px 0px 0px'});
	$('.portfolio-bg:has(.client-module)').css({'background-color':'#ffffff'});
	$('.slider-container:has(.client-module)').children('.slider-pages').css({'margin':'10px 0px 0px 20px'});
	
	$('.stripViewer:has(.client-module-past)').css({'width':'915px','padding':'0px 0px 0px 0px'});
	$('.slider-container:has(.client-module-past)').css({'width':'915px','margin':'0px 0px 0px 0px'});
	$('.portfolio-bg:has(.client-module-past)').css({'background-color':'#ffffff'});
	$('.slider-container:has(.client-module-past)').children('.slider-pages').css({'margin':'10px 0px 0px 20px'});
	
	$('.stripViewer:has(.casestudies-module)').css({'width':'915px','padding':'0px 0px 0px 0px'});
	$('.slider-container:has(.casestudies-module)').css({'width':'915px','margin':'0px 0px 0px 0px'});
	$('.portfolio-bg:has(.casestudies-module)').css({'background-color':'#ffffff'});
	$('.slider-container:has(.casestudies-module)').children('.slider-pages').css({'margin':'10px 0px 0px 20px'});
	
	
	$('.portfolio-bg:has(h2:contains("Projects By Client"))').css({'background-color':'#ffffff'});
	$('.portfolio-bg:has(h2:contains("Projects By Client"))').find('.slider-container').css({'background-color':'#ffffff'});
	
	// styles list-module borders
	$('.list-module:even').addClass(' right-border');
	$('.list-module:nth-child(12n+1)').addClass(' no-top-border');
	$('.list-module:nth-child(12n+2)').addClass(' no-top-border');
	
	$('.client-module').css({'background-color':'#fdfdfd'});
	$('.client-module:nth-child(6n+4)').css({'background-color':'#f6f6f6'});
	$('.client-module:nth-child(6n+5)').css({'background-color':'#f6f6f6'});
	$('.client-module:nth-child(6n+6)').css({'background-color':'#f6f6f6'});
	
	$('.client-module-past').css({'background-color':'#fdfdfd'});
	$('.client-module-past:nth-child(10n+6)').css({'background-color':'#f6f6f6'});
	$('.client-module-past:nth-child(10n+7)').css({'background-color':'#f6f6f6'});
	$('.client-module-past:nth-child(10n+8)').css({'background-color':'#f6f6f6'});
	$('.client-module-past:nth-child(10n+9)').css({'background-color':'#f6f6f6'});
	$('.client-module-past:nth-child(10n+10)').css({'background-color':'#f6f6f6'});
	
	$('.thumb-module a span').css({opacity: 0});
	
	$('.casestudies-module:nth-child(even)').css({'border-right':'none'});
	
//---------------------------------------
//-- Thumb-Module Mouseover -------------
//---------------------------------------

	$('.thumb-module').hover(function(){
		$(this).children('a').css({'color':'#de2816'});
	},function(){
		$(this).children('a').css({'color':''});
	});

//---------------------------------------
//-- List-Module Mouseover -------------
//---------------------------------------
	
	$('.list-module').hover(function(){
		$(this).children('a').css({'color':'#de2816'});
	},function(){
		$(this).children('a').css({'color':''});
	});


//---------------------------------------
//-- DropDown ---------------------------
//---------------------------------------
	
	$('#dropdown ul').css({display: 'none'});

	$('#dropdown li ul li:last a').hover(function(){
		$(this).css({'height':'12px'});
	}, function (){
		$(this).css({});
	});
	
	
	var config3 = {    
		sensitivity: 100,
		interval: 100,
		over: ddmenuOpen,
		timeout: 250,
		out: ddmenuClose
	};
	
	
	$('#dropdown').hoverIntent(config3)
		
	var duration = 300;
	
	function ddmenuOpen(){
		var timer = 0; 
		var multiplier = 1;
		var time = 65;
		
		$('#dropdown').css({
			'background-image':'url(themes/horngroup/images/dropdown_hover_bg.png)',
			'background-repeat':'no-repeat'
		});
		
		$('#dropdown ul li').each(function(i){  
			$(this).css({opacity: 0}); 
			timer = (timer*multiplier + time); 
			$(this).animate({opacity: 0}, timer).animate({opacity: 1}, 100);
		}); 
		
		$(this).find('ul:first').css({visibility: 'visible',display: 'none'}).slideDown(duration);
	}
	
	function ddmenuClose(){
		$(this).find('ul:first').slideUp(duration);
		setTimeout("$('#dropdown').css({'background-image':'none'});", duration);
	}	

//--------------------------------------------			
//-- Return to Original Coda Slider ----------
//--------------------------------------------
			// Tab nav
			jQuery("div#stripNav" + j + " a:not([id])").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() {
					jQuery(this).addClass("current").parent().parent().find("a").not(jQuery(this)).removeClass("current"); // wow!
					var cnt = - (panelWidth*z);
					cPanel = z + 1;
					jQuery(this).parent().parent().parent().parent().prev().find("div.panel-container").animate({ left: cnt}, settings.easeTime, settings.easeFunc);
				});
			});
			
			// Left nav
			jQuery("a#prev-proj").click(function(){
				if (cPanel == 1) {
					var cnt = - (panelWidth*(panelCount - 1));
					cPanel = panelCount;
					jQuery(this).parent().parent().parent().parent().parent().find("div.stripNav a.current").removeClass("current").parent().parent().find("li:last").prev().find('a').addClass("current");
				} else {
					cPanel -= 1;
					var cnt = - (panelWidth*(cPanel - 1));
					jQuery(this).parent().parent().parent().parent().parent().find("div.stripNav a.current").removeClass("current").parent().prev().find("a").addClass("current");
				};
				jQuery(this).parent().parent().parent().parent().parent().parent().find("div.panel-container").animate({ left: cnt}, settings.easeTime, settings.easeFunc);
				// Change the URL hash (cross-linking)...
				location.hash = cPanel;
				return false;
			});
			
			// Right nav
			jQuery("a#next-proj").click(function(){
				if (cPanel == panelCount) {
					var cnt = 0;
					cPanel = 1;
					jQuery(this).parent().parent().parent().parent().parent().find("div.stripNav a.current").removeClass("current").parent().parent().find("a:eq(0)").addClass("current");
				} else {
					var cnt = - (panelWidth*cPanel);
					cPanel += 1;
					jQuery(this).parent().parent().parent().parent().parent().find("div.stripNav a.current").removeClass("current").parent().next().find("a").addClass("current");
				};
				jQuery(this).parent().parent().parent().parent().parent().parent().find("div.panel-container").animate({ left: cnt}, settings.easeTime, settings.easeFunc);
				// Change the URL hash (cross-linking)...
				location.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 (location.hash && parseInt(location.hash.slice(1)) <= panelCount) {
				jQuery("div#stripNav" + j + " a:eq(" + (location.hash.slice(1) - 1) + ")").addClass("current");
			} else {
				jQuery("div#stripNav" + j + " a:eq(0)").addClass("current");
			}
			
		});
		
		j++;
		setPortfolioView();
  });
};