/**
 * @author jr
 * @date 09.11.2009
 * fabrique d'images ebusiness gmbh (web: http://www.fdi.de)
 */

// ***** Helper functions *****

// go_home
function go_home(){
	s = '119,105,110,100,111,119';
	var lp = "";
	l=s.split(",");
	for(i=0;i<l.length;i++)
	{
		lp+=String.fromCharCode(l[i]);
	}
	lp += '.location="http:\/\/www.kunert.de\/"';
	eval( lp );
}

// cursor for input buttons to pointer (like normal a-tag)
function styleSubmitButton() {
	if( $("input[type=submit]").length > 0 ) {
		$("input[type=submit]").hover(
			// mouseover	
			function(){ $(this).css("cursor","pointer"); },
			// mouseout
			function(){ $(this).css("cursor","default"); }
		);
	}		
}

// change select value with image
function setColor( colorID ) {
	//alert("setze colorID: " + colorID );
	$("#attrColor select:visible").val( colorID );
	$(".md_select_variant").trigger('change');
}

// set ff value
function setFFValue( value, selectName ) {
	$("[name=" + selectName + "]").val( value ).change();
}


function toSockAbo() {
	var ean = $(".sockabo:visible").attr("name");
	var amount = $("input[name='am']:visible").val();
	$("form[name='sockenAboForm'] input[name='variantId']").val( ean );
	$("form[name='sockenAboForm'] input[name='amount']").val( amount );
	//dbg: alert("vor submit: Werte: variantId:" + ean + " amount:" + amount );
	$("form[name='sockenAboForm']").submit();
}

function submitCustRemindForm() {
	$("#customersRemindForm").submit();
}

// ***** Onload event *****
$(function() {
	// prepare tooltips for products
	$(".ttip").each( function() {
		var $this = $(this);
		var contArr = $this.attr("title").split("~");
		var ttipHtml = '<div  style="z-index:99;"class="tooltip"><h5>&raquo;' + $.trim(contArr[0]) + '&laquo;</h5><p>' + 
					$.trim(contArr[1]) + '<br>' + '<strong>' + $.trim(contArr[2]) +  '</strong><span>*</span></p></div>';					
		$this.after( ttipHtml );
	});
	// prepare standard tooltips 
	$(".ttipStd").each( function() {
		var $this = $(this);
		var ttipHtml = '<div style="z-index:99;" class="tooltip"><h5>' + $this.attr("title") + '</h5></div>';					
		$this.after( ttipHtml );
	});
	
	// tooltip init
	$(".ttip").tooltip({ effect: 'fade', fadeOutSpeed: 100, predelay: 100, position: "top right", offset: [20,-20 ] }); 
	$(".ttipStd").tooltip({ effect: 'fade', fadeOutSpeed: 100, predelay: 100, position: "top right", offset: [-7,-10 ] }); 
	
	// mouse hover effect for submit input button
	styleSubmitButton();	
	
	// startpage slideshow
	if( $(".contHome").length > 0 ) {
		$('.image-slideshow').slideshow({
      		active_image_width: 540,	//Breite der Bilder in px
      		slide_speed: 900,			//Geschwindigkeit des auf/zu-sliden der Bilder in Millisekunden (default: 300)
      		inactive_image_opacity: 0.4,	//Deckkraft der inaktiven Bilder (default: 0.5)
 			on_slide_disable_navi: true,	//Falls 'true', wird die Navigation während dem Bildwechsel ausgeblendet (default: true)
			auto_slide: true,				//Falls 'true', wird die Slideshow automatisch gestartet (default: false)
      		auto_slide_speed: 9000,			//Dauer wie lang ein Bild angezeigt werden soll in Millisekunden (default: 1000)
      		auto_slide_mode: "there-and-back-again",	//Modus der Slideshow
      		pause_slide_on_hover: true
   		});
		// Newsletter-Form
		$("#newsletter input:text").focus( function() {
			if ($(this).val() == $(this).attr("title")) {
				$(this).val("");
			}
		});
		$("#newsletter input:text").blur( function() {
			if ($(this).val() == "") {
				$(this).val($(this).attr("title"));
			}
		});
	}
	
	// category page - styling for fast finder	
	if( $(".categoryPage").length > 0  && $("#_filterlist").length > 0 ) {
		var FFHtml = "";
		$("#_filterlist select").addClass("hiddenSelect");
		var curSelIsColor = false;
		$("#_filterlist select").each( function() {
			var selectName = $(this).attr('name');
			var selectVal = $(this).val();
			curSelIsColor = $(this).hasClass("farbe") || $(this).hasClass("color");
			var colImg = "";
			$(this).find("option").each( function(i) {
				if ((i == 0 && selectVal != "") || i != 0) {
					FFHtml += '<a class="fl';
					if ($(this).val() == selectVal) {
						FFHtml += ' active';
					}
					// color image
					colImg = $.trim( $(this).text() );
					colImg = colImg.replace(/ /,"").replace(/-/,"").replace(/\./,"");
					colImg += '.jpg';							
					FFHtml += '" href="javascript:setFFValue(\'' + $(this).val() + '\', \'' + selectName + '\');">';
					if ( curSelIsColor ) {
						if (colImg != 'Filterentfernen.jpg') {
							FFHtml += '<img style="width:40px;height:30px;" class="colorThumb" alt="' + $(this).text() + '" title="' + $(this).text() + '" src="' + location.protocol + '//' + location.host + '/out/kunert/2/img/colors/' + colImg + '">';
						} else {
							// link to delete filter
							FFHtml += '<a href="javascript:setFFValue(\'' + $(this).val() + '\', \'' + selectName + '\');">' + $(this).text() + '</a><br>';
						}
					} else {
						FFHtml += $(this).text();
					}
					FFHtml += '</a>';
					if( !curSelIsColor && colImg == 'Filterentfernen.jpg' ) {
						FFHtml += '<br>';
					}
				}
			});
			FFHtml = '<div class="ffItems lClear">' + FFHtml + "</div>";
			$(this).before( FFHtml );
			FFHtml = "";
		});
	}	
	
	// detail slider
	if( $(".slider").length > 0 ) {
		$('.slider').slider({
			num_visible_images : 3,
			slide_speed : 1000
		});
	}
	
	// detail page - move variants - onload and onchange
	if( $("#attrColor").length > 0 ) {
		var showVariants = true; // flag show variant pics in detail page
		// *** onload ***				
		$("#attrColor").append( $("select.md_select_variant:visible:first") );
	    $("#attrSize").append( $("select.md_select_variant:visible:last") );	
		
		// colorbox for product detail
		$("a.thickbox").colorbox({transition:"none", height:"95%"}); 
			
		// colors as image
		var out = ''; var colImg = '';
		$("#attrColor option").each( function (i) { 
			out += '<a class="colorBtn';
			colImg = $.trim( $(this).text() );
			colImg = colImg.replace(/ /,"").replace(/-/,"").replace(/\./,"");
			colImg += '.jpg';
			if( i == 0) { out += " activeColor "; }
			out += ' color_' + $(this).text() + 
				'" title="' + $(this).text() + '" name="' + $(this).val() + '" style="float:left;margin-right:4px;" href="javascript:setColor(\''+ $(this).val() + 
				'\')"><img class="colorThumb" src="' + location.protocol + '//' + location.host + '/out/kunert/2/img/colors/' + colImg + '"></a>'  
		})
		$("#attrColor h4").after( out );
		
	    if( showVariants ) {
    		$(".productImg a").attr("href", $(".variantPicsForJS:visible").find("em").attr("name") );
	    	$(".productImg img").attr("src", $(".variantPicsForJS:visible").find("b").attr("name"));
    	}
	    // *** onchange ***
	    if( $(".md_select_variant").length > 0 ) {
			// jr/fdi 14.01.2010 - customers remind - set articleid in reminder form (onload)
			$("#customersRemindForm [name='req_custrem_articleid']").val( $(".variants:visible [name='aid']").val() );
			$("#sockenAboForm [name='variantId']").val( $(".variants:visible [name='aid']").val() );
			if( $(".custRemindFlag:visible").length > 0 ) {
				$("#customersRemind").show();
			}
				
	    	$(".md_select_variant").change( function() {
			    $("#attrColor").append( $("select.md_select_variant:visible:first") );
			    $("#attrSize").append( $("select.md_select_variant:visible:last") ); 
				// jr/fdi 14.01.2010 - customers remind - set articleid in reminder form
				$("#customersRemindForm [name='req_custrem_articleid']").val( $(".variants:visible [name='aid']").val() );							
				$("#sockenAboForm [name='variantId']").val( $(".variants:visible [name='aid']").val() );
				if( $(".custRemindFlag:visible").length > 0 ) {
					$("#customersRemind").show();
				} else {
					$("#customersRemind").hide();
				}
				
			    // set focus to checkout button
		    	$("[name='am']:visible").focus();
		    	styleSubmitButton();
		    	// show item pic as product img
		    	if( showVariants ) {
		    		$(".productImg a").attr("href", $(".variantPicsForJS:visible").find("em").attr("name") );
			    	$(".productImg img").attr("src", $(".variantPicsForJS:visible").find("b").attr("name"));
		    	}		    	
			});
	    }
	}
	
	// detail page: image  color buttons - set active onclick
	if( $(".colorBtn").length > 0 ) {
		$(".colorBtn").click( function() {
			$(".colorBtn").removeClass("activeColor");
			$(this).addClass("activeColor");		
		});
	}
	
	// order - step 4 - show moved AGB checkbox - else noscript is shown
	if( $("#triggerAGBBox").length > 0 ) {
		$("#triggerAGBBox,#triggerAGB").show();
		$("#OrderConfirmAGBBottom").hide();
		$("#triggerAGB").change( function() {
	    	if( $("#triggerAGB:checked").length > 0 ) { 				
				$("#OrderConfirmAGBBottom").attr('checked',true) 
			} else {				
				$("#OrderConfirmAGBBottom").attr('checked',false) 
			}
		})		
	}	
	
});