function expandMenuListItem(arg){
	arg.find("img:first").animate({height:'80px', width:'57px'}, { queue:false, duration:400 })
//	arg.stop(true);
	arg.animate( { height:"80px" }, { queue:false, duration:400 })
}

function resetMenuListItem(arg){
//		arg.stop(true);
		arg.find("img:first").animate({height:'40px', width:'28px'}, { queue:false, duration:400 })
		arg.animate( { height:"40px" }, { queue:false, duration:400 })
}


function initializeShoppingCart(){
	$("#shoppingCart").hover(
		function(){
			$(this).stop(true);
			if($("#ShoppingCartInnerTitel").innerHeight()>20){
				$(this).animate({height: $("#ShoppingCartInnerTitel").innerHeight()+39 }, 700 ); 
			}
		},
		function(){
			$(this).stop(true);
	      	$(this).animate({height: "68px"}, 500 );
		}
	);		
	getCurrentCart();
}

function setActiveAccordion(){
	var myActiveAccordion 
	$.get("/f180-shop/F180-products-xml.xml",function(data){
			tmpNode = data.getElementsByTagName("f180shop");
			myActiveAccordion=tmpNode[0].getAttribute('activeSection');
			$("#leftMenu").accordion('activate' , parseInt(myActiveAccordion));
		}, "xml")
}

function setFirstProduct(arg){
	if(arg!="none"){
		setContentToProduct(arg,true)
	}else{
		if(gup("varenr")){
			setContentToProduct(gup("varenr"),true);
		}else{
			$.get("/f180-shop/F180-products-xml.xml",function(data){
				tmpNode = data.getElementsByTagName("f180shop")
					setContentToProduct(tmpNode[0].getAttribute('firstProduct'),true);
				}, "xml")
		}
	}
}

function setContentToProduct(varenr,setByUrl){
	if(currentPage=="checkout"){
		document.location = "http://shop.f180.dk/f180-shop/shop_product.aspx?varenr="+varenr;
	}
	else{
		$.getJSON("http://shop.f180.dk/f180-shop/product-db2json.aspx?varenr="+varenr, function(currentProduct,stat){
	 		$("#product-top-price").html(currentProduct.pris +",-");
	 		$("#product-heading").html(currentProduct.titel);
	 		$("#product-top-salespoint").html(currentProduct.productTopSalespoint);
	 		$("#tabs-1").html(currentProduct.productLongDescription);
			$("#product-add-to-cart").unbind("click")
	 		$("#product-add-to-cart").click(function(){add2cart(currentProduct.varenr)});
	 		$("#large-product-image").attr("src", "/productimages/"+ currentProduct.largeProductImage);
	 		$("#large-product-image").attr("alt", currentProduct.titel);
	 		insertVideoPlayer(currentProduct.video_url,currentProduct.largeProductImage);
	 		document.title=currentProduct.titel;
	 		pageTracker._trackPageview("/ajax/"+varenr+"-"+currentProduct.titel);
	    });
	}
}

function insertVideoPlayer(url,cover){	
	if(url != ""){
		$("#tabs-2").html("")
		$("#tabs-2").flash(
			{ src:'http://shop.f180.dk/common/flash/ParamPlayer3.swf',width:480,height:315,
			  flashvars:{flvURL:'http://server02.dk/myCmsUploads/flv/'+url, lgCover:'http://shop.f180.dk/productimages/'+cover, skinURL:'http://shop.f180.dk/common/flash/SteelExternalAll.swf'}
			},
	        { version: 8 }
	   	);
		$("#midTabArea").tabs( 'enable' , 0 )
		$("#midTabArea").tabs( 'select' , 0 ) //vælger video tabben for at være sikker på at den er valgt.
	} else {
		$("#tabs-2").html("Video er på vej")
		$("#midTabArea").tabs( 'select' , 1 ) //vælger beskrivelses tabben hvis der ikke er video
		$("#midTabArea").tabs( 'disable' , 0 )
		//$("#tabs-2").hide()
	}
}

function add2cart(varenr){
	$.post("/f180-shop/shop_cart_ws-j.asp", { action : 'addVare', varenr : varenr}, 
		function(data){
		    updateCart(data);
			animateCoverToCart();
		  }, "json");
	pageTracker._trackEvent('add2Cart', 'laeg i kurv', varenr);
}

function getCurrentCart(){
	$.getJSON("/f180-shop/shop_cart_ws-j.asp?action=getcart", function(data){
		if(data.products[0].antal>0){
			updateCart(data);
			//sender hele cart JSON objectet fra sessionCartObj
		}
	});
}

function updateCart(data){
	$("#ShoppingCartInnerStk").html("");
	$("#ShoppingCartInnerTitel").html("");
	$("#ShoppingCartInnerPris").html("");
	
//	console.log(data.products.length)
	//hvis der er varer i kurven
	if(data.products.length>0){
	   	$("#shoppingCartWrapper").click(function () {
	   		pageTracker._trackEvent('checkOut', 'click on Checkout') 
	      	document.location="/f180-shop/shop-checkout.asp";
	    });
	    $("#shoppingCartWrapper").css("cursor","pointer")
	}else{
		$("#shoppingCartWrapper").css("cursor","auto")
	}
	for(product in data.products){
		$("#ShoppingCartInnerStk").append(data.products[product].antal+"<br>");
 		$("#ShoppingCartInnerTitel").append(data.products[product].titel+"<br>");
 		$("#ShoppingCartInnerPris").append(data.products[product].pris+",-<br>");
	}
	$("#shoppingCartCheckOut").fadeIn("fast");
	$("#shoppingCartCheckOutRight").find("img:first").animate({marginLeft:'20px'}, 600, function(){
			$("#shoppingCartCheckOutRight").find("img:first").css("marginLeft","-5px")
			$("#shoppingCartCheckOutRight").find("img:first").animate({marginLeft:'0'}, 300)	
		}
	);
	
	if($("#ShoppingCartInnerTitel").innerHeight()+40>68){
		$("#shoppingCart").animate({height: $("#ShoppingCartInnerTitel").innerHeight()+40 }, 700,function(){
		      	$("#shoppingCart").animate({height: "68px"}, 2000 );
			}
		 );
	}
}

function gup(name){  
	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");  
	var regexS = "[\\?&]"+name+"=([^&#]*)";  
	var regex = new RegExp( regexS );  
	var results = regex.exec( window.location.href );  
	if(results == null) return "";  
	else return results[1];
}

function animateCoverToCart(){
	arg = $('#large-product-image');
	$(arg).fadeTo(5, 0.01);

	$(arg).clone().insertAfter(arg).css({
			'position' : 'relative',
			'top' : '-'+ $(arg).height()-2 +'px'
		}).animate({
			'left' : '530px',
			'height' : '70px',
			'width' : '50px',
			'z-index' : '10'
		},900,function(){
			$(this).remove();
			$(arg).fadeTo("slow", 1);
		});
}