/*
	Script: Functions
	Project: Primal Consultancy
	Author: Primal Consultancy
	Last Update: 16 January 2012

	TABLE OF CONTENTS:
		- Misc
		- Dropdown filter
		- Cases list - thumbs opacity
		- Cases list - hover animation
		- Cases list - equalize heights
		- Single case - image slider
		- Single case - services list animation
		- Contact form
		
*/
jQuery(function(){
	
	/*
				Misc
	*/
	
	jQuery(".no-js").removeClass("no-js"); /* Remove CSS fallback class */


	/*
				Dropdown filters
	*/
	
	var $filters = jQuery(".filters");
	
	// Hover animation
	if ( $filters.length > 0) {
		$filters.superfish({
			delay:       500,                           	// one second delay on mouseout 
	        animation:   {opacity:'show',height:'show'},  	// fade-in and slide-down animation  
	        autoArrows:  false 								// don't add arrows

		});
		jQuery("body").click(function(){					// close menu on click
			$filters.hideSuperfishUl();
		});

	}
	
	// Dynamic width calculation
	var $drop = $filters.children(".dropdown");
	
	$drop.each(function(){ // Find longest element in each dropdown
		var longest = 0;
		jQuery(this).find("li a").each(function(){ // Get each anchor's length
			var thisLength = jQuery(this).text().length;
			if ( thisLength > longest ) {
				longest = thisLength;
			}
		});
		
		// Check if the title is the longest
		var dropTitle 		= jQuery(this).children("a").eq(0).text();
		var longerString 	= ( longest > dropTitle.length ) ? longest : dropTitle.length;
		var newWidth 		= (longerString * 7) + 50;
		
		// Apply new width
		jQuery(this).css({width: newWidth});
		jQuery(this).children("ul").css({width: newWidth-2});
	});
	
	
	
	/*
				Cases list - thumbs opacity
	*/
	if ( !($.browser.msie && $.browser.version <= 8) ) { // No animation for IE8 or less
		var thumbOpacity 	= 0.4;
		var thumbSpeed 		= 150;
		
		jQuery(".cases img").hoverIntent(
			function(){
				jQuery(this).stop(false, true).fadeTo(thumbSpeed, thumbOpacity);
			}, 
			function(){
				jQuery(this).stop(false, true).fadeTo(thumbSpeed, 1);
			}
		);
	}
	
	
	
	/*
				Cases list - hover animation
	*/
	
		var $caseListItems 	= jQuery("#main .cases li");
		var caseListSpeed 	= 150; // animation duration
		
		$caseListItems.hoverIntent(
			function(){
				var $this = jQuery(this);
				var $caseListInfo 	= $this.find("p.info");
				var $caseListServ 	= $this.find("nav.case-services");
				var $caseListTitle 	= $this.find("a.title");
				var $caseListBranch = $this.find("a.branch");
				
				/* Toggle title */
				$caseListTitle.stop(false, true).animate({left: "-100%"}, caseListSpeed);
				$caseListBranch.stop(false, true).animate({left: 0}, caseListSpeed);
				/* Toggle info */
				$caseListInfo.stop(false, true).animate({left: "-100%"}, caseListSpeed);
				$caseListServ.stop(false, true).animate({left: 0}, caseListSpeed);
			},
			function(){
				var $this = jQuery(this);
				var $caseListInfo 	= $this.find("p.info");
				var $caseListServ 	= $this.find("nav.case-services");
				var $caseListTitle 	= $this.find("a.title");
				var $caseListBranch = $this.find("a.branch");
				
				/* Toggle title */
				$caseListTitle.stop(false, true).animate({left: 0}, caseListSpeed);
				$caseListBranch.stop(false, true).animate({left: "-100%"}, caseListSpeed);
				/* Toggle info */
				$caseListInfo.stop(false, true).animate({left: 0}, caseListSpeed);
				$caseListServ.stop(false, true).animate({left: "-100%"}, caseListSpeed);
			}
		);
	
	
	
	/*
				Cases list - equalize heights
	*/
	

	// Dynamicly set branch label height (equal border-bottom)
	var $caseListTitle 	= $caseListItems.find("a.title"); // get the case title
	var $caseListBranch = $caseListItems.find("a.branch"); // get the case branch
	
	var caseTitleHeight = $caseListTitle.css("height");
	$caseListBranch.css({height: caseTitleHeight}); // apply title's height to the branch
	
	// Check if the case info is shorter than the services
	var $caseListInfo = $caseListItems.find("p.info");
	var $caseListServ = $caseListItems.find("nav.case-services");
	
	var caseInfoHeight = $caseListInfo.css("height"); // height of the info
	var caseServHeight = $caseListServ.css("height"); // height of the services
	
	if ( parseInt(caseInfoHeight) < parseInt(caseServHeight) ) { // if the info's height is less...
		$caseListInfo.css({height: caseServHeight});	// ...expand it
	}
	
	
	
	/*
				Single case - image slider
	*/
	jQuery(".flex-container").flexslider({
		directionNav: false, 	// no arrows
		keyboardNav: false, 	// don't use arrow keys to prevent shadowbox conflict
		slideshowSpeed: 7000,
		animationDuration: 900
	});


	// Fade in on mouseover / Fade out on mouseout
	
	function fadeInOut(element, speed, lowOpacity, hiOpacity) {
		element.fadeTo(speed, lowOpacity);
		element.hoverIntent(
			function(){
				$(this).fadeTo(speed, hiOpacity);
			}, 
			function(){
				$(this).fadeTo(speed, lowOpacity);
			}
		); 
	}	
	
	
	// Fade slider navigation
	
	var sliderNav 		= jQuery(".flex-direction-nav li a.prev, .flex-direction-nav li a.next");
	var sliderSpeed 	= 200;
	var sliderOpacity 	= 0.6;
	
	fadeInOut(sliderNav, sliderSpeed, sliderOpacity, 1);
	
	
	// Case slider images - overlay
	
	var $sliderItems 	= jQuery(".flex-container .slides");
	var $sliderOverlay 	= $sliderItems.find("a.overlay");
	
	$sliderOverlay.fadeTo(0, 0);								// Hide the link
	fadeInOut($sliderOverlay, sliderSpeed, 0, sliderOpacity); 	// Show the link on hover
	
	
	// Image zoom
	if ( jQuery(".flex-container").length > 0 ) {
		Shadowbox.init({
			skipSetup: true
		});

		Shadowbox.setup("a.overlay", {
	        gallery: 		"gallery",
	        handleOversize: "drag",
			overlayOpacity: 0.9
	    });
	}
	
	
	/*
				Single case - services list animation
	*/
	var $caseServList = jQuery(".single-case nav a, .single-case .branch a");
	var $caseServSpan = $caseServList.children("span");
	var caseServSpeed = 200;
	var caseServOpacity = 0.0;
	
	$caseServSpan.fadeTo(0, caseServOpacity);
	
	$caseServList.hover(
		function(){
			jQuery(this).children("span").stop(false, true).fadeTo(caseServSpeed, 1);
		}, 
		function(){
			jQuery(this).children("span").stop(false, true).fadeTo(caseServSpeed, caseServOpacity);
		}
	);
	
	
	
	/*
				Contact Form
	*/
	
	$("form.contact").jqTransform();
	
});
