var Page = {
	qs_timestamp : 0,
	qs_buffer : "",
	qs_charbefore : "",

	init : function() {
		this.initGeneralHover();
		this.initButtonHover();
		this.initQS();
		this.imagePreview();

		$("#main").show();
		$("#footer").show();
	},

	imagePreview : function(){
		/* CONFIG */

			xOffset = 10;
			yOffset = 30;

			// these 2 variable determine popup's distance from the cursor
			// you might want to adjust to get the right result

		/* END CONFIG */
		$("img.preview").hover(function(e){
			this.t = this.title;
			this.title = "";
			var c = (this.t != "") ? "<br/>" + this.t : "";
			$("body").append("<p id='preview'><img src='"+ $(this).attr("src").replace("/Thumb/", "/Bilder/").replace("/Vorschau/", "/Bilder/") +"' alt='Image preview' /></p>");
			$("#preview")
				.css("top",(e.pageY - xOffset) + "px")
				.css("left",(e.pageX + yOffset) + "px")
				.fadeIn("fast");
	    },
		function(){
			this.title = this.t;
			$("#preview").remove();
	    });
		$("img.preview").mousemove(function(e){
			$("#preview")
				.css("top",(e.pageY - xOffset) + "px")
				.css("left",(e.pageX + yOffset) + "px");
		});
	},
	
	
	ajaxError : function(error, msg) {
		//alert(msg + ": " + error);
		//$('#qscategorysubmit').click();
	},

	initGeneralHover : function() {
		$(".hoverNeighbour").hover(function() {
			var url = $(this).children().attr("src");
			var urlSplit = url.split("images/");

			s = urlSplit[1].replace(/\.(.+)$/i, "_hover.$1").replace(/_hover_hover\.(.+)$/i, "_hover.$1");
			$(this).children().attr("src", urlSplit[0] + "images/" + s);
			$('img').show();
		}, function() {
			var url = $(this).children().attr("src");
			var urlSplit = url.split("images/");

			s = urlSplit[1].replace(/_hover\.(.+)$/i, ".$1");
			$(this).children().attr("src", urlSplit[0] + "images/" + s);
			$('img').show();
		});
	},

	initButtonHover : function() {
		$("button.submitBtn").hover(function() {
			$(this).children().css("background","url(/static/images/buttonGreenLeft_hover.gif) left no-repeat");
			$(this).children().css("color", "white");
			$(this).css("background","url(/static/images/buttonGreenRight_hover.gif) right no-repeat");
			$('img').show();
		}, function() {
			$(this).children().css("background","url(/static/images/buttonGreenLeft.gif) left no-repeat");
			$(this).children().css("color", "#ccc");
			$(this).css("background","url(/static/images/buttonGreenRight.gif) right no-repeat");
			$('img').show();
		});
	},

	initModellList : function(itemid) {
		$('#A' + itemid + 'BrandModellLink').toggle(
		function(){
	  		$(this).children().attr({src:"/static/images/open.gif"});
			$(this).children().next().html('ausblenden');
			$('#A' + itemid + 'BrandModellList').show();
		},function(){
	  		$(this).children().attr({src:"/static/images/close.gif"});
			$(this).children().next().html('einblenden');
			$('#A' + itemid + 'BrandModellList').hide();
			$('.thumb').show();
		});
	},

	initQS : function() {
		$(":input[name='KategorieID']").change(function () {

			// Fall 6.1 & 8.1 & 3.1 & 5.1
	 		$.ajax({
				 type: "post",
				 url: "/ajax_qs_old_hersteller.html" ,
				 dataType: "html",
				 data: {
				 'qs_k' : $(":input[name='KategorieID']").val(),
				 'qs_h' : $(":input[name='MarkeID']").val(),
				 'qs_m' : $(":input[name='ModellID']").val(),
				 'id'   : sid
				 },
				 timeout: 5000,
				 error: function(request,error) {
				  	Page.ajaxError(error, "kategorie li:click() hersteller, response: " + request);
				 },
				 success: function(request) {
					$(":input[name='MarkeID']").html(request);

	   				Page.initQS();
				 }
			});

		 	if ($(":input[name='MarkeID']").val().length > 0) {
				$.ajax({
					 type: "post",
					 url: "/ajax_qs_old_modell.html" ,
					 dataType: "html",
					 data: {
					 'qs_k' : $(":input[name='KategorieID']").val(),
					 'qs_h' : $(":input[name='MarkeID']").val(),
					 'qs_m' : $(":input[name='ModellID']").val(),
					 'id'   : sid
					 },
					 timeout: 5000,
					 error: function(request,error) {
					  	Page.ajaxError(error, "kategorie li:click() modell, response: " + request);
					 },
					 success: function(request) {
						$(":input[name='ModellID']").html(request);

		   				Page.initQS();
					 }
				});

				return;
			}
	    });

		$(":input[name='MarkeID']").change(function () {
			$.ajax({
				 type: "post",
				 url: "/ajax_qs_old_modell.html" ,
				 dataType: "html",
				 data: {
				 'qs_k' : $(":input[name='KategorieID']").val(),
				 'qs_h' : $(":input[name='MarkeID']").val(),
				 'qs_m' : $(":input[name='ModellID']").val(),
				 'id'   : sid
				 },
				 timeout: 5000,
				 error: function(request,error) {
					  	Page.ajaxError(error, "hersteller li:click() modell, response: " + request);
				 },
				 success: function(request) {
					$(":input[name='ModellID']").html(request);

	   				Page.initQS();
				 }
			});
	    });
	}
};

$(document).ready(function() {

	$('#qs').show();

	$("#headerMid li").hover(function() {
		$(this).css("background","url(/static/images/left_hover.gif) no-repeat left top");
		$(this).children().css("color","#000000");
		$(this).children().css("background","url(/static/images/right_hover.gif) no-repeat right top");
	}, function() {
		$(this).css("background","url(/static/images/left.gif) no-repeat left top");
		$(this).children().css("color","#8b8b8a");
		$(this).children().css("background","url(/static/images/right.gif) no-repeat right top");
	});

	$("#headerMid li#current").hover(function() {
		$(this).css("background","url(/static/images/left_active.gif) no-repeat left top");
		$(this).children().css("background","url(/static/images/right_active.gif) no-repeat right top");
	}, function() {
		$(this).css("background","url(/static/images/left_active.gif) no-repeat left top");
		$(this).children().css("background","url(/static/images/right_active.gif) no-repeat right top");
	});

	$("#headerMid li#highlight").hover(function() {
		$(this).css("background","url(/static/images/left_hover.gif) no-repeat left top");
		$(this).children().css("background","url(/static/images/right_hover.gif) no-repeat right top");
	}, function() {
		$(this).css("background","url(/static/images/left_on.gif) no-repeat left top");
		$(this).children().css("background","url(/static/images/right_on.gif) no-repeat right top");
	});

// Funktion fuer das Menu
	$('div#menu:eq(0)> div.menuContent').hide();
	
  	//$('div.menuContent2nd').show();

	$('div#menu:eq(0)> div.menuEntryClick, div#menu:eq(0)> div.menuEntryClickSelected').click(function() {
    	$(this).next().slideToggle('fast');
    	//$(this).next('div.menuContent:hidden').slideDown('fast').siblings('div.menuContent:visible').slideUp('fast');
  	});
	
	$('div#menu:eq(0)> div.menuEntry2ndClick, div#menu:eq(0)> div.menuEntry2ndClickSelected').click(function() {
    	$(this).next().slideToggle('fast');
    	//$(this).next('div.menuContent:hidden').slideDown('fast').siblings('div.menuContent:visible').slideUp('fast');
  	});

	$('.menuEntryClickSelected').next().slideToggle('fast');
	
  	$('.menuEntryClick').toggle(function(){
  	if($(this).is("#qs")) {
		$(this).children().children(".status").attr({src:"/static/images/close.gif"});
	} else {
		$(this).children().children(".status").attr({src:"/static/images/open.gif"});
	}
	},function(){if($(this).is("#qs")) {
		$(this).children().children(".status").attr({src:"/static/images/open.gif"});
	} else {
		$(this).children().children(".status").attr({src:"/static/images/close.gif"});
	}
	});

// Funktion fuer das Sliden das Warenkorbs
	$('#ex2').jqm({
  		ajax: '/ajax_cart.html',
  		trigger: 'a.ex2trigger',
  		onShow: function(h) {
        	/* callback executed when a trigger click. Show notice */
        	h.w.css('opacity',1.0).fadeIn("slow");
        },
      	onHide: function(h) {
        	/* callback executed on window hide. Hide notice, overlay. */
       		h.w.fadeOut("normal",function() { if(h.o) h.o.remove(); });
       		Page.init();
       	}
	});

// Funktion fuer die Artikelsuchergebniss
	$('div.articles').show();
	
// Funktion fuer die Downloads
	$('div#download:eq(0)> div.downloadContent').hide();
	$('div#download:eq(0)> div.downloadTitle').click(function() {
    	$(this).next().slideToggle('fast');
  	});
  	$('.downloadTitle').toggle(function(){
		$(this).addClass("selected");
		$(this).children().attr({src:"/static/images/open.gif"});
	},function(){
		$(this).removeClass("selected");
		$(this).children().attr({src:"/static/images/close.gif"});
	});



	/* "einblenden"
	$('.showModell').toggle(function(){
		$(this).children().attr({src:"/static/images/open.gif"});
	}, function(){
		$(this).children().attr({src:"/static/images/close.gif"});
	}); */

	Page.init();

  	$(".overviewLevel2").hide();
  	$(".overviewLevel3").hide();

	$('.overviewLevel1').click(function() {
		$(".overviewLevel2").hide();
		//$(".overviewLevel3").hide();
		$(this).children().children(".overviewLevel2").show();
  	});

  	$('.overviewLevel2').click(function() {
  		$temp = true;
  		$before = false;

  		if($('body').find('.overviewLevel3:visible').html() != null) {
  			$before = true;
  		}

		$(".overviewLevel2").hide();
		$(".overviewLevel3").hide();
		$(this).children().children(".overviewLevel3").show();

  		if(($('body').find('.overviewLevel3:visible').html() != null) && !$before) {
  			$temp = false;
  		}
		$(this).show();
		$(this).siblings().show();

		return $temp;
  	});

  	$('.priceViewHideLink').click(function() {
  		$('.priceViewHide').click();
  	});

  	$('.priceViewShowLink').click(function() {
  		$('.priceViewShow').click();
  	});

  	$.expr[":"].startsWith = function(el, i, m) {
	    var search = m[3];
	    if (!search) return false;
	    return eval("/^[/s]*" + search + "/i").test($(el).text());
	};

	//neue (old) QS
	$('#quicksearchSubmitGui').click(function(){
		$('#quicksearchSubmit').click();

		return false;
	});

	$.expr[":"].focus = function(el, i, m) {
 		if(document.activeElement == el) {
 			return true;
 		} else {
 			return false;
 		}
	};

});

// Hover Image Preload
$(window).bind('load', function() {
	var preload = new Array();
	$(".hover").each(function() {
		var url = $(this).children().attr("src");
		var urlSplit = url.split("images/");

		s = urlSplit[1].replace(/\.(.+)$/i, "_hover.$1").replace(/_hover_hover\.(.+)$/i, "_hover.$1");

		preload.push(urlSplit[0] + "images/" + s)
	});
	var img = document.createElement('img');
	$(img).bind('load', function() {
		if(preload[0]) {
			this.src = preload.shift();
		}
	}).trigger('load');
});

// Check minimum value
function check_minimum (minimum, element_id) {
    var qty;
    
    qty = Number(document.getElementById(element_id).value);

    if (minimum > 0 && qty < minimum) {
	return false;
    }

    return true;
};

