var hoverelcounter = 0;

$(document).ready(function(){
	$(".active-button").hover(
		function(){
			$(this).css("backgroundPosition","left bottom");
		},
		function(){
			$(this).css("backgroundPosition","left top");
		}
	);
	$(".active-input-button").hover(
		function(){
			if($(this).attr("id")=="" || $(this).attr("id")== undefined){
				$(this).attr("id","hoverel"+hoverelcounter);
				hoverelcounter++;
			}
			var newSrcName = switchImage($(this).attr("id"),0);
		},
		function(){
			var newSrcName = switchImage($(this).attr("id"),1);
		}
	);
	$(".bestell-button-hg").live("click",function(e){
		e.preventDefault();
		$(this).parents("form").submit();
		if($(".artikelseite-neu #artikelBestellen_but").length > 0){
			$("#artikelBestellen_but").append("<div class='putinbasket_loading' />");
		}
	});
	
	// show productvideo
	/*$(".artikel-video-link").click(function(e){
		e.preventDefault();
		$('#produktbild_gross_produkt1').hide();
		$("#product_video_player_wrapper").show();
		var video_link = $(this).attr("href");
		jwplayer('product_video_player').setup({
		    'flashplayer': '/admin/inc/jwplayer/player.swf',
		    'id': 'playerID',
		    'width': '290',
		    'height': '290',
		    'autostart': true,
		    'bufferlength': 3,
		    'controlbar': 'bottom',
		    'file': video_link
		  });

	});*/
	
	$(".videoliste-link").click(function(e){
		e.preventDefault();
		$("#video_liste_player_holder").show();
		var video_link = $(this).attr("href");
		jwplayer('video_liste_player').setup({
		    'flashplayer': '/admin/inc/jwplayer/player.swf',
		    'id': 'playerID',
		    'width': '680',
		    'height': '400',
		    'autostart': true,
		    'bufferlength': 3,
		    'controlbar': 'bottom',
		    'file': video_link
		  });
		$('html, body').animate({ scrollTop: 0 }, 0);
		
		$("#video_liste_player_product_info").html($("#artikel-info-"+$(this).attr("id")).html());
	});
	
	if($("#produktvideo_player").length > 0){
		var video_link = $("#produktvideo_player").attr("video-url");
		jwplayer('produktvideo_player').setup({
		    'flashplayer': '/admin/inc/jwplayer/player.swf',
		    'id': 'playerID',
		    'width': '650',
		    'height': '400',
		    'autostart': true,
		    'bufferlength': 5,
		    'controlbar': 'bottom',
		    'file': video_link
		  });
	}
	
	if(jQuery().colorbox){
		if($("#agbcolorbox").length > 0){
			$("#agbcolorbox").colorbox({
				inline:true, 
				opacity:0.7, 
				href:"#agb_layer",
				scrolling: false
			});
		}
		$(".agb_layer_footer").live("click",function(){
			$.colorbox.close();
		});
	}
	
	// set focus on search field
	var livesearch_input = $("#inpSuche");
	var partner_input = $(".partner_start_input:first");
	if(livesearch_input.length > 0 && partner_input.length <= 0){
    livesearch_input.focus();
  }else if(partner_input.length > 0){
    partner_input.focus();
  }
	
	setHilfeFunctions();
	setHoverFunctions();
	setNaviFunctions();
	switchLogo();
	toggleNlSubscribtionInfo();
	toggleMaximalShoppenInfo()
	toggleZahlungsartenInfo();
	initializeTooltips();
});

function confirmDeleteRZProduct(element){
	$("body").append("<div id='wk_loeschen_confirm'>" +
			"<div class='wk_loeschen_overlay'>&nbsp;</div>" +
			"<div class='wk_loeschen_div'>" +
			"<button class='wk_loeschen_button' id='wk_loeschen_button_ja'>ja</button>" +
			"<button class='wk_loeschen_button' id='wk_loeschen_button_nein'>nein</button>" +
			"</div>" +
			"</div>");
	
	$("#wk_loeschen_button_nein").live("click",function(){
		$("#wk_loeschen_confirm").remove();
	});
	$("#wk_loeschen_button_ja").live("click",function(){
		deleteRZProduct(element);
	});
}

function deleteRZProduct(element){
	$('#wkb_del').val(element);
	$('#wkb_new_za').val("1");
	$('#wk_form').submit();
}

function initializeTooltips(){
	$("#cvc_help")
		.live("mouseover",function(){
			$("body").append("<div id='tooltip_holder'>"+$(this).attr("info")+"<img src='/daten/"+snum+"/pics/cvc.jpg' /></div>");
			$(this).mousemove(function(e){
					$("#tooltip_holder").css({
						top: (e.pageY + 15) + "px",
						left: (e.pageX + 15) + "px"
					});
			})
			.mouseout(function(){         
				$("#tooltip_holder").remove();
	       	})
			.click(function(){         
				window.open(window.location.host+'?ste=cvc','CVC',''); 
	       	});
		});
}

function hideVideo(){
	$("#product_video_player_wrapper").replaceWith("<div class='noneDisplay' id='product_video_player'></div>");
}

function switchImage(theId,how){
	var elem = $("#"+theId);
	var theSrc = elem.attr("src");
	var path = theSrc.substring(0,theSrc.lastIndexOf('\/')+1);
	var filename = theSrc.substring(theSrc.lastIndexOf('\/')+1);
	var filenameParts = filename.split(".");
	if(how==0){
		filenameParts[0]=filenameParts[0]+"_2";
	}else{
		var filenamePs=filenameParts[0].split("_");
		filenamePs.pop();
		filenameParts[0]=filenamePs.join("_");
	}
	filename = filenameParts.join(".");
	elem.attr("src",path + filename);
}


function setHilfeFunctions() {
	$("[id*='jshilfe_lnk']").click(
		function() {
			src = $(this).attr("href");
			$(this).siblings(".div_ph").each(
				function() {
					$(this).toggle();
					if($(this).html() == "") {
						$(this).load(src+" #hilfe_container");
					}
				}
			);
			return false;
		}
	);
}
function setHoverFunctions() {
	$("#warenkorb_header_wrapper").children(".hover")
	.mouseenter(
		function() {
			$("#wk_hover_wrapper").show();
		}
	)
	.mouseleave(
		function() {
			$("#wk_hover_wrapper").hide();
		}
	);
}
function setNaviFunctions() {
	setSubNavi();
	setNaviWidth();
}
function setSubNavi() {
	$(".mainmenu-item").mouseenter(
		function() {
		  $(".main_subnav").hide();
			var act_width = $(this).children("a").children("img").width();
			$(this).children('.connector').width(act_width);
			$(this).children('.main_subnav').each(
				function(){
					$(this).show();
				}
			);
		}
	);
	$(".mainmenu-item").mouseleave(
		function() {
			$(this).children('.main_subnav').each(
				function(){
					$(this).hide();
				}
			);
		}
	);
}
function setNaviWidth() {
	$(".mainmenu-item").each(
		function() {
			act_width = $(this).children("a").children("img").width();
			$(this).children('.connector').width(act_width);
			width_parent = $(this).parent().width();
			pos_parent = $(this).parent().position().left;
			act_pos = $(this).position().left;
			padd = 17;
			width_all = (width_parent + pos_parent) - padd;
			act_width = $(this).children('.inner').width();
			new_pos = act_pos;
			if(act_pos + act_width > width_all) {
				marg_left = (act_pos + act_width) - width_all;
				new_pos = act_pos - marg_left;
				if(new_pos < pos_parent) {
					marg_left = pos_parent - pos_parent;
					new_pos = act_pos - marg_left;	
				}
				$(this).children('.inner').css("margin-left",-marg_left);
			}
			act_width_2 = width_all - new_pos;
			$(this).children('.inner').css("max-width",act_width_2);
		}
	);
}


function switchLogo() {
	$("#seen-on-tv-logo").cycle({
			fx:		'scrollDown',
			speed:   1000,
			timeout: 3000
	})
}

function toggleNlSubscribtionInfo() {
	$("#div_nl_anmeldung").mouseenter(
		function() {
			$("#nl_anmeldung_rollover").show();
		}
	);
	$("#div_nl_anmeldung").mouseleave(
		function() {
			$("#nl_anmeldung_rollover").hide();
		}
	);
}
function toggleMaximalShoppenInfo() {
	$("#sonder_1 img:first-child").mouseenter(
		function() {
			$("#maximal_shoppen_rollover").show();
		}
	);
	$("#sonder_1 img:first-child").mouseleave(
		function() {
			$("#maximal_shoppen_rollover").hide();
		}
	);
}
function toggleZahlungsartenInfo() {
	$("#sonder_4").mouseenter(
		function() {
			$("#zahlungsarten_rollover").show();
		}
	);
	$("#sonder_4").mouseleave(
		function() {
			$("#zahlungsarten_rollover").hide();
		}
	);
}


