/**
 * Custom Scripts.
 */
 
$jq(document).ready(function() {
	
	var main = $jq(".child .main-content").height();
	var side = $jq(".child .sidebar").height();
	
	if(side > main) {
		$jq(".child .main-content").css("min-height",side);
	}
	
	$jq("#primary-nav li:nth-child(5) > a").attr('href', '#');
	
	$jq("#primary-nav li.expanded").hover(function() {
		
		var menuWidth = $jq(this).width();
		
		$jq("ul", this).width(menuWidth);										 
		$jq("ul", this).stop(true, true).slideDown();
		$jq("ul", this).prev().addClass("hovered");
		$jq("ul", this).prev().addClass("flat");
		
		
	}, function() { 

		$jq("ul", this).stop(true, true).slideUp(function() {
			$jq(this).prev().removeClass("hovered");
			$jq(this).prev().removeClass("flat");
		});
		return false;
		
	});
	
	
	formatBanner();
	
	$jq(".user-toggle").click(function() {
		$jq(".login-form").fadeIn();
	});
	
	$jq("#close").click(function() {
		$jq(".login-form").fadeOut();
	});
	
});


$jq(window).resize(function() {
	formatBanner();	
});


function formatBanner() {
	
	$jq("#arrow-left").hide();
	var press = 0;
	var winWidth = $jq(window).width() + 17;
	
	$jq("#banner li").width(winWidth);
	$jq("#banner-text li").width(winWidth);
	
	var maxPress = $jq("#banner li").length - 1;
	
	$jq("#arrow-left").unbind("click");
	$jq("#arrow-right").unbind("click");
	
	function animate_banner(){
		
		if(press < maxPress) {
		
			$jq("#banner ul").animate({
				"marginLeft":"-="+winWidth
			});
			
			$jq("#banner-text ul").animate({
				"marginLeft":"-="+winWidth
			});
			
			press++;
			
			if(press >= 1) {
				$jq("#arrow-left").fadeIn();
			}
			
			if(press == maxPress) {
				$jq("#arrow-right").fadeOut();
			}
			
			console.log(press);
		
		}
		
		else if(press == maxPress) {
		
			$jq("#banner ul").animate({
				"marginLeft":"0px"
			});
			
			$jq("#banner-text ul").animate({
				"marginLeft":"0px"
			});
			
			press = 0;
			
			$jq("#arrow-left").fadeOut();
			$jq("#arrow-right").fadeIn();
			
			console.log(press);
		
		}

	}
	
	var loop = setInterval(animate_banner, 10000)
	
	loop;
	
	$jq("#arrow-left").click(function() {
		
		clearInterval(loop);
		
		if(press >= 1) {
									
			$jq("#banner ul").animate({
				"marginLeft":"+="+winWidth
			});
			
			$jq("#banner-text ul").animate({
				"marginLeft":"+="+winWidth
			});
			
			press--;
			
			if(press == 0) {
				$jq("#arrow-left").fadeOut();
			}
			
			if(press < maxPress) {
				$jq("#arrow-right").fadeIn();
			}

		}
		
	});
	
	$jq("#arrow-right").click(function() {
									 
		clearInterval(loop);
									 
		if(press < maxPress) {
									 
			$jq("#banner ul").animate({
				"marginLeft":"-="+winWidth
			});
			
			$jq("#banner-text ul").animate({
				"marginLeft":"-="+winWidth
			});
			
			press++;
			
			if(press >= 1) {
				$jq("#arrow-left").fadeIn();
			}
			
			if(press == maxPress) {
				$jq("#arrow-right").fadeOut();
			}

		}
		
	});
	
}
