$(document).ready( function () {

	/**
	 * Small search
	 */
	
	/* Show and hide */
	var allow_small_search_auto_hide = true;
	
	$("#search_small_title").mouseenter(function(){
		
		$("#search_small_hover").fadeIn("fast");
	});
	
	$("#search_small_hover").click(function(){
		
		allow_small_search_auto_hide = false;
	});		
	
	$("#search_small_hover").mouseleave(function(){
		
		if (allow_small_search_auto_hide === true) {
		
			$("#search_small_hover").fadeOut("slow");
		}
	});	
	
	$("#search_small_hover img.close_btn").click(function(){
		
		$("#search_small_hover").fadeOut("medium");
		
		allow_small_search_auto_hide = true;
	});
	
	/* Selectbox */
	$("#small_search_con .selectbox").click(function(){
		
		$(this).stop().children("div.choices").css("display","block");
	});
	
	$("#small_search_con .selectbox").mouseleave(function(){
		
		$(this).stop().children("div.choices").delay(50).fadeOut(200);

	});	

	/**
	 * All category and brands
	 */
	
	var allcb = new Object();
	allcb.hide_pos = "-2000px";
	allcb.show_pos = "155px";
	allcb.speed = 200;
	allcb.easing = "jswing";

	$(".all_category a.trigger").click(function(){
		
		$("#all_category div.overlay_all").css('left',allcb.show_pos);
		$("#all_brand div.overlay_all").css('left',allcb.hide_pos);

		return false;
	});
	
	$("#all_brand a.trigger").click(function(){
		
		$("#all_category div.overlay_all").css('left',allcb.hide_pos);
		$("#all_brand div.overlay_all").css('left',allcb.show_pos);

		return false;
	});
	
	$("#nav_left_new img.close_btn").click(function () {
		
		$("#all_category div.overlay_all").css('left',allcb.hide_pos);
		$("#all_brand div.overlay_all").css('left',allcb.hide_pos);
	});

	/**
	 * Content block slider
	 */

	var cbs = new Object();
	cbs.step_pos = 250;
	cbs.max_pos = 0-$("#content_block_con div.second .item").length;
	cbs.min_pos = 0;
	cbs.current_pos = 0;
	cbs.current_pos_screen = 1;
	cbs.next_pos_px = 0;
	cbs.speed = 700;
	cbs.easing = "easeOutBack";

	$("#content_block_con div.second .prev_btn").click(function(){
		
		cbs.current_pos++;
		cbs.next_pos_px = cbs.current_pos*cbs.step_pos;

		if (cbs.current_pos <= 0) {
		
			// Previous item
			$("#content_block_con div.second div#item_con").stop().animate({"marginLeft":cbs.next_pos_px+"px"}, cbs.speed, cbs.easing);
			
			// Num on screen
			cbs.cbs.current_pos_screen--;
			$("#content_block_con div.second span.active").text(cbs.cbs.current_pos_screen);
			
		} else {
			
			cbs.current_pos--;
		}
	});
	
	$("#content_block_con div.second .next_btn").click(function(){
		
		cbs.current_pos--;
		cbs.next_pos_px = cbs.current_pos*cbs.step_pos;

		if (cbs.current_pos > cbs.max_pos) {
		
			// Next item
			$("#content_block_con div.second div#item_con").stop().animate({"marginLeft":cbs.next_pos_px+"px"}, cbs.speed, cbs.easing);
			
			// Num on screen
			cbs.cbs.current_pos_screen++;
			$("#content_block_con div.second span.active").text(cbs.current_pos_screen);
			
		} else {
			
			cbs.current_pos++;
		}
	});	
	
	/**
	 * Spotlight
	 */
	
	var spotl = new Object();
	spotl.step_pos = 302;
	spotl.max_pos = 0-$("#spotlight .item").length;
	spotl.min_pos = 0;
	spotl.current_pos = 0;
	spotl.current_pos_screen = 1;
	spotl.next_pos_px = 0;
	spotl.speed = 700;
	spotl.easing = "easeOutBack";
	
	// Previous
	$("#spotlight .prev_btn").click(function(){
		
		spotl.current_pos++;
		spotl.next_pos_px = spotl.current_pos*spotl.step_pos;
	
		if (spotl.current_pos <= 0) {
		
			// Previous item
			$("#spotlight div#item_con").stop().animate({"marginLeft":spotl.next_pos_px+"px"}, spotl.speed, spotl.easing);
			
			// Num on screen
			spotl.spotl.current_pos_screen--;
			$("#spotlight span.active").text(spotl.spotl.current_pos_screen);
			
		} else {
			
			spotl.current_pos--;
		}
	});
	
	// Next
	$("#spotlight .next_btn").click(function(){
		
		spotl.current_pos--;
		spotl.next_pos_px = spotl.current_pos*spotl.step_pos;
	
		if (spotl.current_pos > spotl.max_pos) {
		
			// Next item
			$("#spotlight div#item_con").stop().animate({"marginLeft":spotl.next_pos_px+"px"}, spotl.speed, spotl.easing);
			
			// Num on screen
			spotl.spotl.current_pos_screen++;
			$("#spotlight span.active").text(spotl.current_pos_screen);
			
		} else {
			
			spotl.current_pos++;
		}
	});
	
	/**
	 * Header loader
	 */
	
	// Build loader
	header_content_loadbar_builder ();
	
	// Call next img function
	next_header_content_img ();
	
	/**
	 * Item
	 */
	
	/* Hover */
	$(".list_item_con div.item").mouseenter(function(){
		
		$(this).css("border-color","#00bcc8");
		$(this).children("img").css("border-color","#00bcc8");
		$(this).children("div.arrow").css("background-position","top right");
	});
	
	$(".list_item_con div.item").mouseleave(function(){
		
		$(this).css("border-color","#cbc7c2");
		$(this).children("img").css("border-color","#cbc7c2");
		$(this).children("div.arrow").css("background-position","top left");
	});	
	
	/**
	 * Large search
	 */
	
	var lsearch = new Object();
	lsearch.marker_extra_pos = 190;
	lsearch.budget_max_left = 0;
	lsearch.budget_max_right = 230;
	lsearch.budget_min = 0;
	lsearch.budget_max = 15000;
	lsearch.marker_width = 25;
	
	/* Open search */
	$("div#open_large_search_trigger").click(function(){

		$("div#large_search_con").css("display","block");
		$("div#open_large_search_trigger").css("display","none");
		
		page_content_position ();
		
		return false;
	});		
	
	/* Close search */
	$("a#close_large_search_btn").click(function(){

		$("div#large_search_con").css("display","none");
		$("div#open_large_search_trigger").css("display","block");

		page_content_position ();
		
		return false;
	});		
	
	/* Open advanced */
	$("#large_search_con div.trigger_open").click(function(){

		$("#large_search_con div.advanced").css("display","block");
		$("#large_search_con div.trigger_open").css("display","none");
		$("#large_search_con div.trigger_close").css("display","block");
		
		page_content_position ();
	});
	
	/* Close advanced */
	$("#large_search_con div.trigger_close").click(function(){

		$("#large_search_con div.advanced").css("display","none");
		$("#large_search_con div.trigger_open").css("display","block");
		$("#large_search_con div.trigger_close").css("display","none");
		
		page_content_position ();
	});	
	
	/* Budget */
	$(".budget_con span.selection_min").text($(".budget_con input#budget_min").attr("value"));
	$(".budget_con span.selection_max").text($(".budget_con input#budget_max").attr("value"));
	
	/* Min marker */
	$("#large_search_con .first_row .budget_bar img.start_marker").bind("drag",function(event){
		
		/* Marker pos */
		var window_width_half = $(window).width()/2;

		$(this).css( event.shiftKey ? { top: event.offsetY } : { left: event.offsetX-(window_width_half+lsearch.marker_extra_pos) });
		
		/* Max and min pos */
		var current_left = parseInt($(this).css("left"));
		
		if (current_left < lsearch.budget_max_left) {

			$(this).css("left",lsearch.budget_max_left+"px");
		}
		
		if (current_left > lsearch.budget_max_right) {

			$(this).css("left",lsearch.budget_max_right+"px");
		}
		
		/* Set budget value */
		set_min_budget();	
	});
	
	/* Set min budget */
	$("#large_search_con .first_row .budget_bar img.start_marker").mouseup(function() {

		set_min_budget();
	});
	
	function set_min_budget() {
		
		var el = $("#large_search_con .first_row .budget_bar img.start_marker");
		var current_left = parseInt(el.css("left"));
		
		var new_min_amount_pc = current_left*(100/lsearch.budget_max_right);
		var new_min_amount = parseInt((lsearch.budget_max*new_min_amount_pc)/100);
		
		if (new_min_amount >= lsearch.budget_min && new_min_amount < lsearch.budget_max) {
			
			/* Set label */
			$(".budget_con span.selection_min").text(new_min_amount);
			
			/* Set value */
			$(".budget_con input#budget_min").attr("value", new_min_amount);
		}
	}

	/* Max marker */
	$("#large_search_con .first_row .budget_bar img.end_marker").bind("drag",function(event){
		
		/* Marker pos */
		var window_width_half = $(window).width()/2;

		$(this).css( event.shiftKey ? { top: event.offsetY } : { left: event.offsetX-(window_width_half+lsearch.marker_extra_pos) });
		
		/* Max and min pos */
		var current_left = parseInt($(this).css("left"));

		if (current_left < lsearch.budget_max_left) {

			$(this).css("left",lsearch.budget_max_left+"px");
		}
		
		if (current_left > lsearch.budget_max_right) {

			$(this).css("left",lsearch.budget_max_right+"px");
		}

		set_max_budget();	
	}); 
	
	/* Set max budget */
	$("#large_search_con .first_row .budget_bar img.end_marker").mouseup(function() {

		set_max_budget();
	});
	
	function set_max_budget() {
		
		var el = $("#large_search_con .first_row .budget_bar img.end_marker");
		var current_left = parseInt(el.css("left"));
		
		var new_max_amount_pc = current_left*(100/lsearch.budget_max_right);
		var new_max_amount = parseInt((lsearch.budget_max*new_max_amount_pc)/100);
		
		if (new_max_amount > lsearch.budget_min && new_max_amount <= lsearch.budget_max) {
			
			/* Set label */
			$(".budget_con span.selection_max").text(new_max_amount);
			
			/* Set value */
			$(".budget_con input#budget_max").attr("value", new_max_amount);			
		}
	}

	/* Selectbox */
	$("#large_search_con .second_row .selectbox").click(function(){
		
		$(this).stop().children("div.choices").css("display","block");
		
		set_flash_element_display (false);
	});
	
	$("#large_search_con .second_row .selectbox").mouseleave(function(){

		$(this).stop().children("div.choices").delay(50).fadeOut(200);
		
		set_flash_element_display (true);
	});
	
	/**
	 * Page content position
	 */
	
	setTimeout(function() { page_content_position (); }, 200);

	/**
	 * Style chooser
	 */
	
	var stylec = new Object();
	stylec.step_pos = 126;
	stylec.max_pos = 0-$("#style_chooser .item").length;
	stylec.min_pos = 0;
	stylec.current_pos = 0;
	stylec.current_pos_screen = 1;
	stylec.next_pos_px = 0;
	stylec.speed = 700;
	stylec.easing = "easeOutBack";
		
	// Notice
	$("#style_chooser div.item_con").delay(800).animate({"marginLeft":"0px"}, stylec.speed, stylec.easing);

	// Previous item
	$("#style_chooser div.left_arrow_con").click(function(){

		stylec.current_pos++;
		stylec.next_pos_px = stylec.current_pos*stylec.step_pos;

		if (stylec.current_pos <= 0) {
		
			// Previous item
			$("#style_chooser div.item_con").stop().animate({"marginLeft":stylec.next_pos_px+"px"}, stylec.speed, stylec.easing);
			
		} else {
			
			stylec.current_pos--;
		}
	});

	// Next item
	$("#style_chooser div.right_arrow_con").click(function(){
		
		stylec.current_pos--;
		stylec.next_pos_px = stylec.current_pos*stylec.step_pos;

		if (stylec.current_pos > stylec.max_pos) {
		
			// Previous item
			$("#style_chooser div.item_con").stop().animate({"marginLeft":stylec.next_pos_px+"px"}, stylec.speed, stylec.easing);
			
		} else {
			
			stylec.current_pos++;
		}
	});

	/**
	 * Product images
	 */
	
	// Start
	$(".product_con .images .thumb_image img#thumb1").parents("div:first").addClass("selected_thumb_con");
	$(".product_con .images .large_image img#img1").fadeIn("fast");
	
	// Mouse action
	$(".product_con .images .thumb_image div").mouseover(function(){
		
		// Determine image ID from thumb
		var img_id = $(this).children("img").attr("id");
		
		if (img_id) {
			
			img_id = img_id.replace("thumb", "");
	
			// Set new selected thumb
			$(".product_con .images .thumb_image div").removeClass("selected_thumb_con");
			$(this).addClass("selected_thumb_con");
	
			// Hide current image
			$(".product_con .images .large_image img").css("display","none");
			
			// Show larger image
			$(".product_con .images .large_image #img"+img_id).fadeIn("fast");
		}
	});

	/**
	 * Color choice
	 */
	
	var colorc = new Object();
	colorc.id_var = "color_";
	colorc.item_width = 18;
	colorc.speed = 300;
	colorc.easing = "easeInOut";
	
	// Color choice
	$(".color_bar div.color_choice").click(function(){
		
		// Color ID
		var color_id = $(this).attr("id");
		var color_id = color_id.replace(colorc.id_var, "");
		
		// Split color ID to nr and keyname
		var color_id_parts = color_id.split('_'); 
		var color_nr = color_id_parts[0];
		var color_hex = color_id_parts[1];
		var color_keyname = color_id_parts[2];
		
		// Set color note
		$(".color_bar img#color_choice_note").fadeIn("fast");
		var color_note_left = parseInt(colorc.item_width*(color_nr-1));
		$(".color_bar img#color_choice_note").css("left",color_note_left+"px");
		
		var color_name = color_keyname;
		var f = color_name.charAt(0).toUpperCase();
		f += color_name.substr(1);
		color_name = f;

		$(".color_con span.color").text(color_name);
		
		// Set color visual
		$(".color_con img.color_visual_overlay").css("display","block");
		$(".color_con div.color_visual").css("display","block");
		$(".color_con div.color_visual").css("background-color","#"+color_hex);
		$(".color_con input#no_color_choice").attr("checked","");
		
		// Set color hidden input
		$("input#color").val(color_keyname);
	});
	
	// Reset color choice
	$(".color_con input#no_color_choice").click(function(){

		// Set color note
		$(".color_bar img#color_choice_note").fadeOut("fast");
		$(".color_con span.color").text("Alles");
		
		// Set color visual
		$(".color_con img.color_visual_overlay").css("display","none");
		$(".color_con div.color_visual").css("display","none");
		$(".color_con div.color_visual").css("background-color","#fff");
		
		// Set color hidden input
		$("input#color").val("");
	});
	
	/**
	 * Fancybox
	 */
	
	// Quick link for window
	$(".window_quick_link").fancybox({
		'width' : 800,
		'height' : 500,
		'autoScale'	: false,
		'type' : 'iframe'
	});	
	
	// Intern window popop
	$(".window_intern_popup").fancybox({
		'width' : 1050,
		'height' : 600,
		'autoScale'	: false,
		'type' : 'iframe'
	});	
	
	/**
	 * Cycle
	 */	

    $(".slideshow").cycle({
		fx: "fade",
		timeout: 8000
	});
	
	/**
	 * Tabs
	 */
	
	$(".tabs li").bind("click", function () {
		
		// Hide all divs and remove all selected
		$(".tab_content").hide();
		$(".tabs li").removeClass("selected");
		
		// Set selected
		$(this).addClass("selected");
		$("div#" + $("a", this).attr("rel")).show();
		
		// Set page content position
		page_content_position ();
	});
	
	// Default select first
	$(".tabs li").eq(0).trigger("click");
	
	/**
	 * Activate tab
	 */
	
	// Onload from url
	activate_anchor_tab ();
	
	// Extern from tab link
	$(".anchor_tab_link").bind("click", function () {
	
		// Get rel id value
		var anchor_tab_link_value = $(this).attr("rel");
		anchor_tab_link_value = anchor_tab_link_value.replace("anchor_", "");
		
		// Trigger tab action
		$("[rel="+anchor_tab_link_value+"]").trigger("click");
		
		// Set page content position
		page_content_position ();
	});
});

/**
 * Function : Activate anchor tab
 */

function activate_anchor_tab () {
	
	// Fetch anchor from url
	var anchor = self.document.location.hash.substring(1);
	
	if (anchor != "") {
		
		// Trigger tab action
		$("[rel="+anchor+"]").trigger("click");	
		
		// Set page content position
		page_content_position ();		
	}
}

/**
 * Function : Next header content img
 */

var headc = new Object();
headc.spacer_pos = 0;
headc.loadbar_width = 794;
headc.image_num = 0;
headc.spacer_step = 0;
headc.loadbar_previous_width = 0;
headc.loadbar_active_width = 0;
headc.speed = 5000;
headc.fade_speed = 1000;
headc.active_img_index = 0;
headc.easing = "jswing";
headc.new_img_index = 0;

function header_content_loadbar_builder () {

	// Get image num
	headc.image_num = $("#content_header .image_con div").length;
	
	// Determine spacer step
	headc.spacer_step = Math.round(headc.loadbar_width/headc.image_num);
	
	// Append spacers
	for (i=1; i<headc.image_num; i++) {

		headc.spacer_pos = i*headc.spacer_step;

		$("#content_header .loading_bar").append('<div class="step_spacer" style="left:'+headc.spacer_pos+'px"></div>');
	}
	
	// Append loader
	$("#content_header .loading_bar").append('<div class="loader"></div>');		
}

function next_header_content_img () {

	// Loop through items
	$("#content_header .image_con div").each(function(i, v) {

	    if (i == headc.active_img_index) {

	    	// Fade in active image
	    	$(this).fadeIn(headc.fade_speed);

	    	// Set new width of load bar
	    	headc.loadbar_active_width = (headc.active_img_index+1)*headc.spacer_step;
	    	
	    	// Set new image index
	    	headc.new_img_index = headc.active_img_index+1;

	    } else {

	    	// Fade out active image
	    	$(this).fadeOut(headc.fade_speed);
	    }
	});

	// Set new active img index
	headc.active_img_index = headc.new_img_index;
	
	if (headc.active_img_index == headc.image_num) {

		headc.active_img_index = 0;
		headc.new_img_index = 0;
	}

	// Extra width check
	if (headc.loadbar_active_width > (headc.loadbar_width-2)) { headc.loadbar_active_width = (headc.loadbar_width-2); }

	if (headc.loadbar_active_width < headc.loadbar_previous_width) {
		
		$("#content_header .loader").css("width","0px");
	}
	
	// Loading bar
	$("#content_header .loader").animate({"width":headc.loadbar_active_width+"px"}, headc.speed, headc.easing, function() {

		headc.loadbar_previous_width = headc.loadbar_active_width;
		
		next_header_content_img ();
	});
}

/**
 * Function : Page content position
 */

function page_content_position () {
	
	var pos = $("div#page_content_con").position();
	var height = $("div#page_content").height();
	
	if (pos) {
		
		var new_page_content_pos = pos.top + 13;
		var new_page_container_height = height + 15;
		
		$("div#page_content").css("top",new_page_content_pos+"px");
		$("div#page_content_con").css("height",new_page_container_height+"px");
	}
}

/**
 * Function : Set flash element display
 */

function set_flash_element_display (display) {
	
	if (display === false) {
		
		$(".flash_element").css("visibility","hidden");
		
	} else {
		
		$(".flash_element").css("visibility","visible");
	}
}



