/* close function */
function close_menu(){
	if( !$("#LayoutColumn1, #LayoutColumn2, #LayoutColumn3, #overlay #items").is(":animated") ) {
		$("#LayoutColumn1").stop().fadeTo("fast",1);
		$("#LayoutColumn2").stop().fadeTo("fast",1);
		$("#LayoutColumn3").stop().fadeTo("fast",1);
		$("#overlay #items").stop().slideUp("fast",function(){
			$(this).removeAttr("style");
		})
		clearInterval(closing);
		came_off_sub_nav = false;
	}
}
var came_off_sub_nav = false;
var closing;

/* unwrap em */
setInterval("style_price()",250);
function style_price(){
	if($('.PriceMain').length == 0){
		$(".ProductPrice").wrapInner('<p class="PriceMain">');
	}
}
//variables
var total_new = 0;
var total_featured = 0;


$(function(){
	total_new = $("#HomeNewProducts li").size();
	total_featured = $("#HomeFeaturedProducts li").size();
	/* HEADER MENU */
	/* hide sub uls*/	
	$("#Header #SideCategoryList").hover(function(){
		if(came_off_sub_nav == false){
			if( !$("#LayoutColumn1, #LayoutColumn2, #LayoutColumn3, #overlay #items").is(":animated") ) {
				$("#LayoutColumn1").stop().fadeTo("fast",0.2);
				$("#LayoutColumn2").stop().fadeTo("fast",0.2);
				$("#LayoutColumn3").stop().fadeTo("fast",0.2);
				$("#overlay #items").stop().slideDown();
			}
		}else{
			clearInterval(closing);
			came_off_sub_nav = false;
		}
	},function(){
		closing = setInterval("close_menu()",250);
	});
	
	/* HEADER MENU OVERLAY OPTIONS */
	$("<div id='overlay'><div id='items'></div></div>").insertBefore("#LayoutColumn1");
	$("#items").hover(function(){
		came_off_sub_nav = true;
		clearInterval(closing);
	},function(){
		closing = setInterval("close_menu()",250);
	});
	
	
	/* HEADER SUB-MENU moved to OVERLAY */
	var sub_menu = $("#Header #SideCategoryList ul li ul");
	$("#items").append(sub_menu);
	
	/* HEADER SUB-MENU - add images */
	//speedlite accessories
	for(i=0;i<7;i++){
		$("#items ul:eq(0) a:eq("+i+")").append('<img src="http://www.viewfinderphotography.co.uk/templates/__custom/images/menu/speedlite_accessories/'+(i+1)+'.jpg"/>');
	}
	//studio lighting
	for(i=0;i<8;i++){
		$("#items ul:eq(1) a:eq("+i+")").append('<img src="http://www.viewfinderphotography.co.uk/templates/__custom/images/menu/studio_lighting/'+(i+1)+'.jpg"/>');
	}
	//location light
	for(i=0;i<6	;i++){
		$("#items ul:eq(2) a:eq("+i+")").append('<img src="http://www.viewfinderphotography.co.uk/templates/__custom/images/menu/location_kit/'+(i+1)+'.jpg"/>');
	}
	//video light
	for(i=0;i<4	;i++){
		$("#items ul:eq(3) a:eq("+i+")").append('<img src="http://www.viewfinderphotography.co.uk/templates/__custom/images/menu/video_lighting/'+(i+1)+'.jpg"/>');
	}
	//pro softbox
	for(i=0;i<6;i++){
		$("#items ul:eq(4) a:eq("+i+")").append('<img src="http://www.viewfinderphotography.co.uk/templates/__custom/images/menu/softbox/'+(i+1)+'.jpg"/>');
	}
	//backdrops
	for(i=0;i<8;i++){
		$("#items ul:eq(5) a:eq("+i+")").append('<img src="http://www.viewfinderphotography.co.uk/templates/__custom/images/menu/backdrops/'+(i+1)+'.jpg"/>');
	}
	//studio lighting accessories
	for(i=0;i<9;i++){
		$("#items ul:eq(6) a:eq("+i+")").append('<img src="http://www.viewfinderphotography.co.uk/templates/__custom/images/menu/studio_lighting_accessories/'+(i+1)+'.jpg"/>');
	}
	//parabolic
	for(i=0;i<7;i++){
		$("#items ul:eq(7) a:eq("+i+")").append('<img src="http://www.viewfinderphotography.co.uk/templates/__custom/images/menu/parabolic/'+(i+1)+'.jpg"/>');
	}
	
	//clear all floating menus
	for(i=0;i<8;i++){
		$("#items ul:eq("+i+") li:last").after('<br class="Clear">');
	}
	
	/* HEADER MENU HOVER & CLICK */
	$("#Header #SideCategoryList a").hover(function(){
		var index = $("#Header #SideCategoryList a").index(this);
		$("#items ul").hide();
		$("#items ul:eq("+index+")").show();
	});
	
	/* Key Info */
	$("#KeyInfo h3:eq(0)").click(function(){
		$("#KeyContainer").stop().animate({
			"left":-300
		});
	});
	$("#KeyInfo h3:eq(1)").click(function(){
		$("#KeyContainer").stop().animate({
			"left":10
		});
	});
	
	/* PRODUCT PRICES */
	/* excluding VAT on all prices */
	$(".ProductPriceRating em").append(" exc.VAT");
	
	/* BILLBOARD */
	setInterval("next_ad()",5000);
	/* CATEGORY IMAGES */
	$("#HomeCategories li").css("height","auto");
	
	/* SCROLLER - NEW PRODUCTS */
	var scroller = setInterval("scroller()",4000);
	$("#HomeNewProducts .ProductList, #HomeFeaturedProducts .ProductList").hover(function(){
		clearInterval(scroller);
	},function(){
		scroller = setInterval("scroller()",4000);
	});
	
	
	/* CATEGORY PAGE */
	
	/* remove image link on category pages */
	$(".SubCategoryListGrid li:last").height(0);
		
	$(".SubCategoryListGrid li img").unwrap();
	$(".ProductList li img").unwrap();
	
	/* li hover */	
	$(".ProductList li, #CategoryContent li").live('mouseover mouseout', function(event) {
	  if (event.type == 'mouseover') {
		$(this).addClass("hover");
	  }else if(event.type == 'mouseout'){
		$(this).removeClass("hover").removeAttr("style");
	  }
	});
	$(".ProductList li img").live('click',function(){
		var url = $(this).parent().parent().find("a").attr("href");
		window.location.href = url;
	});
	$("#HomeCategories .ProductList li img").live('click',function(){
		var url = $(this).parent().find("a").attr("href");
		window.location.href = url;
	});
	$(".SubCategoryListGrid	 li img").live('click',function(){
		var url = $(this).parent().find("a").attr("href");
		window.location.href = url;
	});
});
var current_ad = 1;
function next_ad(){
	if( !$("#HomeNewProducts .ProductList").is(":animated") ) {
		if(current_ad < 5){
			$("#screens").animate({
				left:"-=1200"
			},1000,"easeInOutQuad");
			current_ad++;
		}else{
			$("#screens").animate({
				left:0
			},1000,"easeInOutQuad");
			current_ad = 1;
		}
	}
}

/* SCROLLING ADS */
var next_new = 0;
var next_featured = 0;
function scroller(){
	if( !$("#screens").is(":animated") ) {
		if(next_new >= total_new){
			next_new = 0;
		}
		$("#HomeNewProducts .ProductList").animate({
				left:"-=300"
			},500);
		$("#HomeNewProducts li:eq("+next_new+")").clone().appendTo("#HomeNewProducts .ProductList").hide().fadeIn().removeClass("hover");	
		next_new++;
		//---------------------------------------------
		if(next_featured >= total_featured){
			next_featured = 0;
		}
		//$("#HomeNewProducts li:eq("+next+")").fadeTo(500,0.05);
		
		$("#HomeFeaturedProducts .ProductList").animate({
			left:"-=300"
		},500);
		$("#HomeFeaturedProducts li:eq("+next_featured+")").clone().appendTo("#HomeFeaturedProducts .ProductList").hide().fadeIn().removeClass("hover");	
		next_featured++;
	}
}
	
