var deliverycostTO;

$(function(){
	if (jQuery.browser.msie) { trig_bind() }
	
	if(window.DD_belatedPNG) { DD_belatedPNG.fix('*'); }
	
	if($("#carrousel-box").length > 0) { 
		init_caroussel();
	 } else { 
	 	$('#frm-product-intobasket .visuals a').nvsMediaPopup({ 
				popUpRoundedBackground: true,
				popUpRoundedSize: 8,
				enableKeyboardPaging: true
		});
	}; 
	if($("#cart-user-data").length > 0 ) init_orderFormStep2();
	if($("#cart-extra-options").length > 0 ) init_orderFormStep3();
	if($("#auswahl").length > 0 ) init_auswahl();
	init_productDetailPage();
	init_productsearch();
	init_print();
	init_triblock();
	
	if($("#header-alert .btn").length >0) {
		startPos = 0- $("#header-alert").innerHeight( );
		
		$("#header-alert").css({ top: startPos+'px'});
		$("#header-alert").show();
		$("#header-alert").animate( { top:'0px' }, 800, "easeOutBounce");
		$("#header-alert, #header-alert .btn").click( function() { $("#header-alert").animate( { top: startPos+'px' }, 800, "easeOutBounce"); });
	}
	
	$("#deliverycost").mouseover(
		function() { 
			$("#pricepopup").show();
			
			$("#pricepopup, #deliverycost").mouseout(function() { 
				window.clearTimeout(deliverycostTO);
				deliverycostTO  = window.setTimeout(function() { _hide_deliverycost()	}, 500); 
			});
			$("#pricepopup *, #pricepopup, #deliverycost").mouseover(function() { window.clearTimeout(deliverycostTO); });
		}
		
	);
	
// balloon vsl left side	
	if($("#content-inner-box").height() > 855) { 
		$("#vsl-balloon").show();
	}

// Clean form GET without x & y params in the  url.
	$('form[method=get] input[type=image]').click(function() {
		this.form.submit();
		return false;
	});
	

})
	function _hide_deliverycost() {
		window.clearTimeout(deliverycostTO);
		$("#pricepopup").hide();
	}
	
	function init_productsearch() {
		$(".productsearch .resetknop").unbind('click');
		$(".productsearch .resetknop").click( function() {
			$(".productsearch select").val('');
			$(".productsearch select").change();
		});
		
		$(".productsearch select").unbind('change');
		$(".productsearch select").change( function() {
			$(".productsearch fieldset").fadeOut('fast');
			$(".productsearch .loader").fadeIn('fast');
			$.post("/ajax/get_productkiezer_html.php", $(".productsearch form").serialize(), function(data) { 
					$(".productsearch fieldset").html(data);
					window.setTimeout(function () { 
							$(".productsearch .loader").hide();
							$(".productsearch fieldset").fadeIn('fast');
							init_productsearch();	
						}, 1000);
				}, "html");
		})
	}
	
	
	function init_auswahl() {
		$("#auswahl .resetknop").unbind('click');
		$("#auswahl .resetknop").click( function() {
			$("#auswahl select").val('');
			$("#auswahl select").change();
		});
		
		$("#auswahl select").unbind('change');
		$("#auswahl select").change( function() {
			$("#auswahl form").fadeOut('fast');
			$("#auswahl .loader").fadeIn('fast');
			$.post("/ajax/get_productkiezer_html.php", $("#auswahl form").serialize(), function(data) { 
					$("#auswahl form").html(data);
					window.setTimeout(function () { 
							$("#auswahl .loader").hide();
							$("#auswahl form").fadeIn('fast');
							init_auswahl();	
						}, 1000);
				}, "html");
		})
	}
	
// order step 2
	function init_orderFormStep2() {
		if($("input[name=deliveryAddress]:checked").val() == "different") {
			$("#delivery-data").show();
		}
		
		$("input[name=deliveryAddress]").change( function() {
		
			if($("input[name=deliveryAddress]:checked ").val() == "different") {
				$("#delivery-data").show();
			} else {
				$("#delivery-data").hide();
			}
		}); 
	}
	
// order step 3
	function init_orderFormStep3() {
		
		if($('#wrapping:checked').val() == 'wrapping') {
			$('#gift-card-for input,#gift-card input,#gift-card textarea').attr('disabled', 'disabled');
			$('#gift-card-for .radio input').removeAttr('disabled');
		} else if($('#withcard:checked').val() == 'withcard') {
			$('#gift-card-for input,#gift-card input,#gift-card textarea').attr('disabled', 'disabled');
			$('#gift-card-for input,#gift-card input,#gift-card textarea').removeAttr('disabled');
		} else {
			$('#gift-card-for input:text, #gift-card textarea').val('');
			$('#gift-card input, #gift-card-for input:radio').attr('checked', '');
			$('#gift-card-for input,#gift-card input,#gift-card textarea').attr('disabled', 'disabled');	
		}
			
		$('input[name=wrapping]').change(function() { 
			if($(this).val() == 'wrapping') {
				$('#gift-card input:radio').attr('checked', '');
				$('#gift-card-for input:text, #gift-card textarea').val('');
				$('#gift-card-for input,#gift-card input,#gift-card textarea').attr('disabled', 'disabled');
				$('#gift-card-for .radio input').removeAttr('disabled');
			} else if($(this).val() == 'withcard') {
				$('#gift-card-for input,#gift-card input,#gift-card textarea').removeAttr('disabled');
			} else {
				$('#gift-card input, #gift-card-for input:radio').attr('checked', '');
				$('#gift-card-for input:text, #gift-card textarea').val('');
				$('#gift-card-for input,#gift-card input,#gift-card textarea').attr('disabled', 'disabled');	
			}
		})
		
		$('.magnify').nvsMediaPopup({ 
				popUpRoundedBackground: true,
				popUpRoundedSize: 8,
				enableKeyboardPaging: true
		});
	}


// Product detail carrousel
	function init_caroussel() { 
	
		$("#carrousel-box").jCarouselLite(
				{
					btnNext: "#carrousel .next",
					btnPrev: "#carrousel .prev",
					visible: 3,
					circular: false				
		});
		$('#carrousel .prev').addClass('disabled');
	
		$('#carrousel li a, .visuals a').nvsMediaPopup({ 
				popUpRoundedBackground: true,
				popUpRoundedSize: 8,
				enableKeyboardPaging: true
		});
	}

function init_productDetailPage() {
	// hide the non js select dropdowns
	$('#colour-label, #size-label').hide();
	
	$('#frm-product-intobasket').submit(function() { 
		retval = true;

		if($('#product-size').val() == 'notanoption') { 
			x = $('.productsize').position();
			$('.productsizealert').css({ top: x.top, left: '197px' });
			$('.productsizealert').show(200);
			retval = false;
		}
		if ($('#product-detail .productcolour').length > 0) {
			if($('#product-detail .productcolour a.selected').length < 1) {
				p = $('.productcolour').position();
				$('.productcolouralert').css({ top:  p.top, left: '197px' });
				$('.productcolouralert').show(200);
				retval = false;
			}
  		}

		return retval;
	})
	
	if($('#product-detail .productcolour a').length > 0) {
		
	// unbind events for colours.
		$('#product-detail .product-colour a').unbind('click');
		$('#product-colour').unbind('change');
	
	// add selected class to correct product colour icon
		$('#product-detail .productcolour a').each(function() { 
			$('#product-colour [value='+$(this).attr('name')+']').attr('disabled','').css({ color: ''});			  	
			if($(this).hasClass('na')) { 
				$('#product-colour [value='+$(this).attr('name')+']').attr('disabled','disabled').attr('selected','').css({ color: '#c6c6c6'});
			}
		})
		
		$('#product-detail .productcolour a[name='+$('#product-colour option:selected').val()+']').addClass('selected');

		// add product colour icon click function 	
		$('#product-detail .productcolour a').css({ cursor: 'pointer' }).click(function () { 
			if(!$(this).hasClass('na')) { 	
				
				$('.productcolouralert').hide(200);
				$(".productsize .sizes").css({ visibility: 'hidden' });
				$(".productsize .loader").fadeIn('fast');
				$('#product-detail .productcolour a').removeClass('selected');
				$(this).addClass('selected');
				$('#product-colour').val($(this).attr('name'));
				
				$("#dyncolour").text($(this).attr("title"));
				if($(".productsize .sizes").length > 0) {
					$.post("/ajax/get_product_sizes_html.php", $("#frm-product-intobasket").serialize(), function(data) { 
							$(".productsize .sizes").html(data);
							window.setTimeout(function () { 
									$(".productsize .loader").hide();
									$(".productsize .sizes").css({ visibility: 'visible' });
									init_productDetailPage();
								}, 500);
						}, "html");
				}
			} 
			return false;
		});
	}
	


	$('#product-size').change(function() { 
		$('.productsizealert').hide(200);
		$(".productcolour .colours").css({ visibility: 'hidden' });
		$(".productcolour .loader").fadeIn('fast');
		
		if($(".productcolour .colours").length > 0) {
			$.post("/ajax/get_product_colours_html.php", $("#frm-product-intobasket").serialize(), function(data) { 
					$(".productcolour .colours").html(data);
					window.setTimeout(function () { 
							$(".productcolour .loader").hide();
							$(".productcolour .colours").css({ visibility: 'visible' });
							init_productDetailPage();
						}, 500);
			}, "html");
		}
	})
	
	$('#product-colour').change(function() { 
		$('#product-detail .productcolour a[name='+$(this).val()+']').trigger('click');
	});
	
	
}

function init_popup_mediaalbum(zoomtool_path, in_zoomtool) {
	
	$('#nvs_popup_close, #nvs_popup_next, #nvs_popup_prev').unbind();
	$('#nvs_popup_close').click( function() { $.nvsMediaPopupOptions.kill(); })
	$('#nvs_popup_next').click( function() { $.nvsMediaPopupOptions.next(); })	
	$('#nvs_popup_prev').click( function() { $.nvsMediaPopupOptions.prev(); })	
	
	var zoomtool_width = $('#img_container img').attr('width');
	var zoomtool_height = $('#img_container img').attr('height');
	
	if (in_zoomtool) {
		var zoomtool_flashvars = {
		  phpPath : "/includes/zoomtool.inc.php",
		  filename: zoomtool_path,
		  omHeight: 50,
		  omColor: 0xdbdbdb,
		  omLabelColor: 0xdbdbdb,
		  omBtnPath: "/img/zoomtool/btn/",
		  omTxtColor: 0x4b4b4b,
		  loaderImg: '/swf/zoomtool_loader_icon.swf',
		  stageWidth: zoomtool_width,
		  stageHeight: zoomtool_height,
		  label_drag: 'Vertreiben',
		  label_zoom: 'Auswahl vergroßern',
		  label_zoom_in: 'Vergroßern',
		  label_zoom_out: 'Reduzieren',
		  label_reset: 'Alle wiederherstellen',
		  label_toggle: 'Verstecken'
		};
		var zoomtool_params  = {};
		var zoomtool_attributes  = {};
		
		swfobject.embedSWF("/swf/zoomtool.swf?" + zoomtool_path, "img_container", zoomtool_width, zoomtool_height, "9.0.0", "/swf/swfobject_install.swf", zoomtool_flashvars, zoomtool_params, zoomtool_attributes);
	}

}


function init_triblock() {
	if ($("#triblock-content").length > 0) {
		$("#triblock-content").each(function() { 
			myHeight = $(this).outerHeight() + 24;
			newHeight = (207 * (Math.ceil((myHeight + 9) / 207)))-9;
			if(newHeight < 588) { newHeight = 588 };
			newHeight = newHeight - 24;
			$(this).height(newHeight);
		})
	}
}

function init_print() {
	if (window.print && $("#print_btn, #print_btn_product").length > 0 ) {
		$("#print_btn, .product-print").show()
		$("#print_btn, #print_btn_product").click(function () { 
			window.print();
			return false;
		});
	};
}

function trig_bind() {
  //unbind old events
  $("input[type='checkbox']").unbind( 'click' )
  $("input[type='radio']").unbind( 'click' )
  //bind the events
  $("input[type='checkbox']").bind( 'click', function() {
    $(this).trigger( 'change' )
  })
  $("input[type='radio']").bind( 'click', function() {
    $(this).trigger( 'change' )
  })
}

