hc(document).ready(function() {
	if (top.location!= self.location && $("body").attr("id") != "cms") {
		top.location = self.location.href;
	}

    hc("#pay_with_dibs").click(function(event) {
        event.preventDefault();
        hc('.payment_module, #center_column h3').hide();
        hc('#dibs_form').submit();
        hc('#transFrame').css('height','700px');
    });

	hc("#logout_li a").click(function(e){
		e.preventDefault();
		try {
			FB.getLoginStatus(function(response) {
			  if (response.session) {
				FB.logout(function(response){ 
					window.location='/index?mylogout'; 
				}); 
			  } else {
				window.location='/index?mylogout';
			  }
			});
		} catch(err) { window.location='/index?mylogout'; }
	});

	startSlider(hc('body#index .anythingSlider'),true);

	//initializing the assortment slider 
	if(hc("#active_assortment").length > 0)
	{
		//show the div
		var name = hc("#active_assortment").html();
		hc("div #"+name).show();

		//switch active classes
		hc("#assortment_menu li").removeClass("active");
		hc("#assortment_menu img").each(function(){
			hc(this).attr("src",hc(this).attr("src").replace("_blue.gif", ".gif"));
		});
		hc("li#assortment_"+name).addClass("active");
		hc("li#assortment_"+name+" img").attr("src",hc("li#assortment_"+name+" img").attr("src").replace(".gif", "_blue.gif"));

		//start the slider
		startSlider(hc("div #"+name),false);
	}

	hc("#assortment_menu a").click(function(e){
		//prevent the link to do something standard
		e.preventDefault();
		//switch active classes
		hc("#assortment_menu li").removeClass("active");
		hc("#assortment_menu img").each(function(){
			hc(this).attr("src",hc(this).attr("src").replace("_blue.gif", ".gif"));
		});
		hc(this).parent("li").addClass("active");
		hc(this).parent("li").find("img").attr("src",hc(this).parent("li").find("img").attr("src").replace(".gif", "_blue.gif"));
		
		//hide all sliders and then show the one we clicked on
		hc(".anythingSlider").hide();
		var name = hc(this).parent("li").attr("id").replace("assortment_", "");
		hc("#"+name).show();

		//active slider on the div
		startSlider(hc("div #"+name),false);

		//go to page 1 on that slider (hopefully)
	    hc("#"+name).anythingSlider(1);

		//set session so it remembers which assortment menu are on
		hc("#loader").load("/assortment_session.php?current="+name);
	});
	
	hc("#product_list li").hover(function(){
		hc(".product_info").not(hc(".product_info",hc(this))).hide();
		if(hc(this).find(".product_info").css("display") == "none") hc(this).find(".product_info").show("clip",100);
	},function(){
		hc(".product_info").hide();
	})
	
	
	hc(".share_icons img").hover(function(){
		hc(this).attr("src",hc(this).attr("src").replace("_grey",""));
	}, function(){
		hc(this).attr("src",hc(this).attr("src").replace(".jpg","_grey.jpg"));
	});
	hc(".share_icons a.facebook").click(function(e){
		e.preventDefault();
		window.open(hc(this).attr("href"),'mywin','width=550,height=400,toolbar=0,resizable=1');
	})
	hc(".share_icons a.blogger, .share_icons a.twitter").click(function(e){
		e.preventDefault();
		window.open(hc(this).attr("href"),'mywin');
	});
	
	hc("body#index .bigpic div").click(function(){
		window.location = hc(this).parent("div").find("a").attr("href");
	});

	hc("#selectable li").click(function(){
		if(hc(this).attr("class").indexOf("ui-selected") == -1) hc(this).addClass("ui-selected"); else hc(this).removeClass("ui-selected");
		fix_selections();
	});
	
	if(hc("#selectable").length > 0) fix_selections();

	//make hoverimages hover
	hc(".hover_img").hover(function(){
		//old src
		var src = hc(this).attr("src");
		//new src
		var newsrc = src.substr(0,src.lastIndexOf(".")) + "_hover" + src.substr(src.lastIndexOf("."));
		if(src.indexOf("hover") == -1) hc(this).attr("src",newsrc);
	},function(){
		var src = hc(this).attr("src");
		//switch back when we de-hover
		if(src.indexOf("hover") != -1) hc(this).attr("src",hc(this).attr("src").replace("_hover",""));
	});
	
	//slider on search page
	if(hc("#slider-range").length > 0)
	{
		var min_price = parseInt(hc("#min_price").val());
		var max_price = parseInt(hc("#max_price").val());
		var max_limit = parseInt(hc("#max_limit").html());

		hc("#slider-range").slider({
			range: true,
			min: 0,
			max: max_limit,
			step: 10,
			values: [min_price, max_price],
			slide: function(event, ui) {
				hc("#amount").val(ui.values[0] + ' kr - ' + ui.values[1]+ ' kr');
				hc("#min_price").val(ui.values[0]);
				hc("#max_price").val(ui.values[1]);
			}
		});
		hc("#amount").val(hc("#slider-range").slider("values", 0) + ' kr - ' + hc("#slider-range").slider("values", 1)+' kr');
		hc("#min_price").val(hc("#slider-range").slider("values", 0));
		hc("#max_price").val(hc("#slider-range").slider("values", 1));
	}
	
	hc("#attributes").change(function(){
		hc("#resetImages").show();
	});
	hc("#resetImages").click(function(){
		hc("#resetImages").fadeOut();
	});
});

function fix_selections()
{
	hc("#selected_categories").empty();
	hc("#selectable .ui-selected").each(function(){
		hc("#selected_categories").append("<input type=\"hidden\" name=\"search_categories[]\" value=\""+hc(this).attr("class").replace(" ui-widget-content ui-selected","").replace("category_","")+"\" />");
	})
}

function startSlider(element, autoplay)
{
	var element_id = element.attr("id");
	var nmb_slides = hc(".nav_"+element_id+" a").size();
	if(nmb_slides == 0)
	{
		element.anythingSlider({
	        easing: "easeInOutExpo",        // Anything other than "linear" or "swing" requires the easing plugin
	        autoPlay: autoplay,                 // This turns off the entire FUNCTIONALY, not just if it starts running or not.
	        delay: 5000,                    // How long between slide transitions in AutoPlay mode
	        startStopped: false,            // If autoPlay is on, this can force it to start stopped
	        animationTime: 600,             // How long the slide transition takes
	        hashTags: true,                 // Should links change the hashtag in the URL?
	        buildNavigation: true,          // If true, builds and list of anchor links to link to each slide
			pauseOnHover: true             // If true, and autoPlay is enabled, the show will pause on hover
	    });
	}
	$("body[id!=index] #thumbNav").hide();
	
	if(hc("body#category").length > 0 || hc("body#product").length > 0 || hc("body#search").length > 0) {
		var nmb_slides = hc(".nav_"+element_id+" a").size();
		if(parseInt(nmb_slides) < 2) {
			hc(".nav_"+element_id).hide();
			hc("#forward_div em").hide();
			hc("#"+element_id+" .arrow").hide();
		} 
		else {
			hc("#"+element_id+" a.back").css({"margin-left":860-(nmb_slides*31)+"px"});
			if($(".arrow.back").is(':hidden')) hc("#forward_div em").show();
		}
	}
	if($("ul.blocks a.active",element).length > 0 && $("ul.blocks a.active",element).attr("href").lastIndexOf("#panel") != "-1"){
		$("#forward_div em").hide();
		if($(".arrow.forward").is(':visible')) $(".arrow.back").show();
	}
	if($(".arrow.back").is(':visible')) $("#forward_div em").hide();

	$("#forward_div, a.arrow.forward").click(function() {
		$("#forward_div em").hide();
		$(".arrow.back").show();
	});
}

function dump(obj) {
//    hc("#adv_search h2").html(": "+obj);
var out = '';
for (var i in obj) {
    out += i + ": " + obj[i] + "\n";
}

alert(out);

}

