// CONTENTS ====================================================================
/*
- DEFAULT 							- common code that runs on every page.
- NAV								- indicator animation for main nav
- SUBNAV 							- small animation for subnav items
- NETWORKS							- bounce animation for network icons
- MESSAGES OF SUPPORT				- set up alt rows
- SHOP								- gallery images for item detail
*/

// DEFAULT --------------------------------------------------------------
function InitDefault(){
	
	// CALL DEFAULT INIT FUNCTIONS
	InitNav();
	InitSubNav();
	InitNetworks();
	SetBiggerClick();
	SetExternalLinks();
	InitLightBox();
	
	// NAV CENTERING: gets width of the main lav list items and centers the nav
	var totalWidth = 0;
	$("#navigation li a").each(function(){
		totalWidth += ( $(this).outerWidth() + parseInt( $(this).css("padding-right") ) );
	})
	if (!jQuery.browser.msie || (jQuery.browser.msie && parseInt(jQuery.browser.version) > 6) ) {
  		$("#navigation ul").css("margin-left", (980 - totalWidth) / 2);
	}
	
	// NEWS LISTINGS: remove bg-img border from the bottom of the last one
	$(".listing li:not('.grid-listing li'):last").css("background", "none");
	
	// GRID POD LISTINGS: works out how many should be in row, adds a class to last in row, resets the padding if listing has no image, EQ's the pods
	$(".grid-listing").each(function(){
		var inRow 	= Math.floor( $(this).parent().width() / $(this).children("li:first").width() );
		var i 		= 0;
		$(this).children("li").each(function(){
			i++;
			//if(i % inRow == 0) $(this).addClass("row-end");
			if( $(this).children("img").length == 0 ) {
				$(this).addClass("with-divider");
				$(this).addClass("no-image");
			}
		})
		
		// EQ by row
		var items = $(this).children("li");
		for (var i = 0; i < ($(items).length / inRow); i++){
			
			var max 	= 0;
			var start 	= (i * inRow);
			for (var j = start; j < start + inRow; j++){
				if(items[j]){
					if( $($(items)[j]).height() > max ) max = $($(items)[j]).height();
				}
			};
			for (var k = start; k < start + inRow; k++){
				if(items[k]){
					$($(items)[k]).height(max);
				}
			};
			
		};
		
	});
	
	// EVENT DETAIL: animation for facebook icon
	$("#fb-icon").hover(
		function(){
			$(this).stop().animate(
				{top: -2},	
				{duration: 300}
			)
			.animate(
				{top: 7},
				{duration: 300}
			)
		}, function(){
			// out
		}
	)
	
	// DETAIL TABLES: set striping
	$("table").each(function(){
		// alert("1");
		var i = 0;
		$(this).find("tr").each(function(){
			// alert("2");
			if(i%2==0) $(this).addClass("striped");
			i++;
		});
	})
	
	// FEATURED CONTENT: open & close them
	$(".featured-content").each(function(){
		$(this).find(".feature-detail").hide();
		$(this).width(173);
		$(this).find(".click-for-more").toggle(function(){
			$(this).text("click to hide...");
			$(this).parent().next("p").css("clear", "both");
			$(this).parent().stop().animate(
				{width: 540},	
				300,
				"swing",
				function(){
					$(this).find(".feature-detail").fadeIn();
				}
			)
		}, function(){
			$(this).text("click for more...");
			$(this).parent().find(".feature-detail").hide();
			$(this).parent().stop().animate(
				{width: 173},	
				300,
				"swing",
				function(){
					$(this).next(".feature-detail").css("clear", "none");
				}
			)
		})
	})
	
	// FORMS
	if( $(".cart-tab").length == 0 && $(".buy-item").length == 0 && $(".card").length == 0 ){
		$("input, select, textarea").bind("focus", function(){
			$("input:not(input[type$='checkbox']), select, textarea").stop().animate(
				{opacity: 0.5},
				{duration: 300}
			);
			$(this).stop().animate(
				{opacity: 1},
				{duration: 300}
			);
		})
		$("input, select, textarea").bind("blur", function(){
			$("input, select, textarea").stop().animate(
				{opacity: 1},
				{duration: 300}
			);
		})
	}
	
	// SIDEBAR PODS: adjust for 1 or 2 line title
	$(".sidebar-pod h4").each(function(){
		$(this).height("auto");
		if( $(this).innerHeight() > 20 ) {
			// double line title
			$(this).height(27);
		}else{
			$(this).height(21);
			$(this).css("padding-top", "15px");
		}
	})
	
}

// NAV --------------------------------------------------------------
function InitNav(){
	
	// add span to act as indicator
	$("#navigation li").each(function(){
		// $(this).append("<span class='ind'></span><span class='cover'></span>");
		$(this).append("<span class='ind'></span>");
	})
	
	// set behaviours on nav links
	$('#navigation a')
	.mouseover(function(){
		if( !$(this).parent().hasClass("selected") ){
			
			$(this).siblings(".ind").stop().animate(
				{bottom:10},
				{duration: 300}
			)
			
		}
	})
	.mouseout(function(){
		if( !$(this).parent().hasClass("selected") ){
			
			$(this).siblings(".ind").stop().animate(
				{bottom:0},
				{duration: 50}
			)
			
		}
	})
	
	// remove divider from li preceeding selected
	$("#navigation li.selected").prev().css("background", "none");
	
}

// SUBNAV --------------------------------------------------------------
function InitSubNav(){
	$("#sub-navigation a").hover(
		function(){
		$(this).stop().animate(
			{paddingLeft: 10},
			{duration: 100}
		)
	}, function(){
		$(this).stop().animate(
			{paddingLeft: 8},
			{duration: 100}
		)
	})
}

// NETWORKS --------------------------------------------------------------
function InitNetworks(){
	$("#networks a").hover(
		function(){
		$(this).stop().animate(
			{marginTop: -7},
			{duration: 300}
		)
		.animate(
			{marginTop: 0},
			{duration: 300}
		)
	}, function(){
		// out
	})
}

// MESSAGES OF SUPPORT --------------------------------------------------------------
function InitMessagesOfSupport(){
	var i = 0;
	$("#messages-of-support li").each(function(){
		i++;
		if(i%2 == 0){
			$(this).addClass("alt");
		}
		$(this).find(".message p:last").css("margin-bottom", 4);
	})
}

// SHOP --------------------------------------------------------------
function InitGallery(){
	if(!$.browser.msie) $("#shop-item-gallery a img").addClass("drop-shadow");
	
	$("#shop-item-gallery a").click(function(){
		$(".detail-hero-img").attr("src", this.href);
		//$(".detail-hero-img").css("opacity", 0);
		//$(".detail-hero-img").stop().animate({opacity: 1}, 500);
		$("#shop-item-gallery a img").stop().animate({opacity: 1}, 300);
		$(this).children("img").stop().animate(
			{opacity: 0.5},
			{duration: 500}
		)
		return false;
	})
	// 'select' first one
	$("#shop-item-gallery a:first img").css("opacity", 0.5);
}

// ======================================================================

function InitLightBox() {
	
	$("a[rel='video']").colorbox({
		rel: 'nofollow',
		iframe: true,
		width: 660,
		height: 405
	});
}