function openWindow(theURL,winName,features) {
	window.open(theURL,winName,features);
}

function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			if (oldonload) {
				oldonload();
			}
			func();
		}
	}
}

// laurin
function clearLrnTitle(lrnTitle) {
	aSEARCH = new Array('\\xa0','\\xae','&reg;','%','®','\\.','Ä','Ö','Ü','ä','ö','ü','ß','`','´','&nbsp;','&', '\\x3f','<i>','<\\/i>','<b>','<\\/b>','<I>','<\\/I>','<B>','<\\/B>', '\\/', '<br>', '<BR>','  ', ' ', '___', '__','\\xa0','\\(','\\)');
	aREPLACE = new Array('','','','','','','Ae','Oe','Ue','ae','oe','ue','ss','','','_','_', '', '', '', '', '', '','','','','_','_','_','_','_','_','_','_','_','_');

	for (a=0; a<aSEARCH.length; a++) {
		eval('lrnTitle = lrnTitle.replace(/'+aSEARCH[a]+'/g, "'+aREPLACE[a]+'");');
	}
	return lrnTitle;
}

function elwin(elwinID,elwinW,elwinH,elwinURL,elwinOptions) {
	var height = '100%';
	if(elwinH != ''){
		height = elwinH;
	}
	var width = '1022';
	if(elwinW != ''){
		width = elwinW;
	}
	
	jQuery.fancybox({
		width: width,
		height: height,
		autoDimensions: false,
		href: elwinURL + '?fancy=true',
		centerOnScroll: true,
		padding: 0,
		overlayOpacity: 0.2,
		origin: jQuery('#content')
	});
//	o = new Array();
	// DEFINE DEFAULTS
//	o['r'] = 'no'; // resizable = no
//	o['s'] = 'no'; // scrollbars = no
//	if (elwinOptions == 's') {
//		for (c=0; c<elwinOptions.length; c++) {
//			o[elwinOptions.charAt(c)] = 'yes';
//		}
//	}
//	elWindow=window.open(elwinURL,'elwindow'+elwinID,'width='+elwinW+',height='+elwinH+',resizable='+o['r']+',scrollbars='+o['s']);
//	elWindow.focus();
}

function scaleup(scaleupID,scaleupW,scaleupH,scaleupURL,scaleupOptions) {
	o = new Array();
	// DEFINE DEFAULTS
	o['r'] = 'yes'; // resizable = no
	o['s'] = 'no'; // scrollbars = no
	if (scaleupOptions == 's') {
		for (c=0; c<scaleupOptions.length; c++) {
			o[scaleupOptions.charAt(c)] = 'yes';
		}
	}
	scaleupWindow=window.open(scaleupURL,'scaleupwindow'+scaleupID,'width='+scaleupW+',height='+scaleupH+',resizable='+o['r']+',scrollbars='+o['s']);
	scaleupWindow.focus();
}

function slideInElwin(elwinID,elwinW,elwinH,elwinURL) {
	elWindow=window.open(elwinURL,'elwindow'+elwinID,'width='+elwinW+',height='+elwinH+',resizable=yes,scrollbars=no');
	elWindow.focus();
}

function printPage(printUrl) {
	window.print();
}

function tellafriend(id) {
	mailWin=window.open('/index.php?fuseaction=tellafriend&pid='+id,'mailWindow','width=480, height=480,resizable=yes,scrollbars=yes');
}

function glossary(showHide,gURL) {
	gwin=window.open(gURL,'gwin','width=480, height=360,resizable=yes,scrollbars=yes');
	gwin.focus();
}


// Browser Check
function browserCheck() {
	UA = new Array();
	browserPlatform = navigator.platform;
	ua = navigator.userAgent;
	an = navigator.appName;
	ua = ua.toLowerCase();
	ua_msie = ua.indexOf('msie');
	ua_netscape = ua.indexOf('netscape');
	ua_mozilla = ua.indexOf('mozilla');
	ua_opera = ua.indexOf('opera');
	ua_firefox = ua.indexOf('firefox');
	userAgent = 'other';
	if (ua_msie != -1 && ua_opera == -1) userAgent = 'MSIE';
	if (ua_opera != -1) userAgent = 'Opera';
	if (ua_firefox != -1) userAgent = 'Firefox';
	if (ua_msie == -1 && ua_opera == -1 && ua_firefox == -1) {
		userAgent = 'Mozilla';
		browserVersion = parseFloat(ua.substr(8, 4));
		if (browserVersion >= 5) { // Netscape 6 or higher
			pos = ua.indexOf('netscape');
			if (pos != -1) browserVersion = parseFloat(ua.substr(pos+9, 4));
		}
	}
	if (userAgent == 'MSIE') browserVersion = parseFloat(ua.substr(ua_msie + 5, 5));
	if (userAgent == 'Opera') browserVersion = parseFloat(ua.substr(ua_opera + 6, 4));
	if (userAgent == 'Firefox') browserVersion = parseFloat(ua.substr(ua_opera + 7, 4));
	UA[0] = userAgent;
	UA[1] = browserVersion;
	UA[2] = browserPlatform;
	return UA;
}
userAgent = browserCheck();

function initialShop(){
	jQuery("input[placeholder]").textPlaceholder();
	
	jQuery('.tabs').tabs();
	
	jQuery(".minicart").hover(function(){
		jQuery(".minicart .details").fadeIn();
	}, function(){
		jQuery(".minicart .details").fadeOut();
	});
	
	jQuery('.dropdown .close').live('click', function(){
		jQuery('.dropdown .button.active').click();
		return false;
	});
	
	jQuery(".dropdown .button").live('click', function(){
		$this = jQuery(this);
		$dopdownBox = $this.siblings('.dropdown_box');
		$this.addClass('current');
		jQuery(".dropdown .active").each(function(){
			if(!jQuery(this).hasClass('current')){
				jQuery(this).removeClass('active').siblings('.dropdown_box').removeClass('active_box').slideUp('fast', function(){
					jQuery(this).parent('.dropdown').removeClass('active_dropdown');
				});
			}
		});
		
		if($this.hasClass('active')){
			$dopdownBox.removeClass('active_box');
			$this.parent('.dropdown').removeClass('active_dropdown');
			$dopdownBox.slideUp(
				'fast',
				function(){
					$this.removeClass('active');
				}
			);
		}else{
			$dopdownBox.addClass('active_box');
			$this.addClass('active');
			$this.parent('.dropdown').addClass('active_dropdown');
			$dopdownBox.slideDown('normal');
		}
		
		checkDropdown($this.parent('.dropdown'));
		
		jQuery(this).removeClass('current');
		return false;
	});
	
	jQuery(".dropdown").each(function(){checkDropdown(this)});
	
	function checkDropdown(item){
		if(jQuery(item).find('.dropdown_box INPUT:checked').length > 0){
			jQuery(item).addClass('slected-dropdown');
		}else{
			jQuery(item).removeClass('slected-dropdown');
		}
	}
	
	jQuery(".filter_form").submit(
		function(event) {
			var data = jQuery(this).serialize();
			var form = jQuery(this);
			var url  = form.attr("action");
			
			jQuery('.dropdown .button.active').click();
			
			jQuery('.produktliste .loading_overlay').fadeIn().spin(spinneropts);
			
			jQuery(".produktliste.gefiltert").fadeTo('fast', 0.5);
				
			var callback = function(data, textStatus, jqXHR) {
				var newHtml = jQuery(data).find(".produktliste.gefiltert").eq(0).html();
				form.removeAttr("disabled");
				jQuery('.produktliste.gefiltert').html(newHtml).fadeTo("fast", 1);
				jQuery('.produktliste .loading_overlay').fadeOut();
				jQuery('.reset-filter').fadeIn();
				initialProducts();
			};
			form.attr("disabled", "disabled");
			var xhr = jQuery.post(url, data, callback, "html");
			/*xhr.error(function() { alert("Error Error Error"); });*/
			return false;
		}
	);
	
	jQuery(".aufklappbox").each(function(){
		var $aufklappbox = jQuery(this);
		var oldBoxHeight = $aufklappbox.height();
		jQuery(this).children('.handle').click(function(){
			var boxHeight = jQuery(this).siblings('.inner').height()+jQuery(this).height();
			if($aufklappbox.height() != boxHeight){
				$aufklappbox.animate({height: boxHeight}, 1000);
				jQuery(this).fadeOut(1000);
			}else{
				$aufklappbox.animate({height: oldBoxHeight}, 1000)
			}
		});
	});
	
	var sliderEasing = 'easeOutBack';
	var sliderSpeed = 1000;
	if(jQuery.browser.msie){
		sliderEasing = 'linear';
		sliderSpeed = 500;
	}

	jQuery('.slideShow').onStage({
		effect: 'slide',
		auto: false,
		infinity: false,
		scenesOnStage: 3,
		move: 3,
		btnPlay: "",
		btnPause: "",
		btnNext: "&nbsp;",
		btnPrev: "&nbsp;",
		duration: sliderSpeed,
		easing: sliderEasing
	});
	
	window.initialProducts = function(){
		jQuery(".product A.more").bigTarget({
			clickZone: '.product'
		});
		
		jQuery('.rating').each(function(){
			var rating = jQuery(this).attr('data-rating') || 0;
			rating = Math.round(rating*10)/10;
			jQuery(this).raty({
				readOnly: true,
				start: rating,
				half:  true,
				path: "/images/",
				noRatedMsg:     'noch keine Bewertungen',
				hintList:       ['unzufrieden', 'unterdurchschnittlich', 'mittelm&auml;&szlig;ig', 'zufrieden', 'ausgezeichnet']
			});
		});
	}
	
	initialProducts();
	
	// Kategorieübersicht
	jQuery(".kategorie_table a").bigTarget({
		clickZone: 'td'
	});
	
	jQuery(".more_products_container").hide();
	
	jQuery(".more_products").click(function(){
		jQuery(".more_products_container").slideToggle();
		return false;
	});
}; /* ende initialShop */

/* Blaettern */

function gotoPage(pageNr){
	jQuery(".produktliste").fadeTo('fast', 0.5);
	window.scrollTo(0, 250);
	
	jQuery('.produktliste .loading_overlay').fadeIn().spin(spinneropts);
	
	var callback = function(data, textStatus, jqXHR) {
		var newHtml = jQuery(data).find(".produktliste.gefiltert").eq(0).html();
		jQuery(".produktliste").html(newHtml).fadeTo("fast", 1);
		document.body.style.cursor = "default";
		initialProducts();
	};
	
	document.body.style.cursor = "wait";
	
	var xhr = jQuery.post(lrnPath, {pn: pageNr}, callback, "html");
	
	return false;
}

// ausgewählte Zahlungsweise speichern
function getZahlungsart() {
	var cp_zahlungsart = jQuery("#payment .scene.active").attr("data-zahlungsart");
	jQuery("#cp_zahlungsart").val(cp_zahlungsart);
	return cp_zahlungsart;
}

/* Checkout: Versandkosten/Gesamtsumme bei Auswahl von Montage-Option 
 * und Lieferoptionen aktualisieren */
function addAssemblingCosts() {
	updateCartAmount(jQuery("#cart_montage_form"));
}
function addShippingCosts() {
	var val = jQuery("#wunschliefertermin").attr("checked");
	// Kalender ein-/ausblenden
	jQuery("#wunschliefertermin_opts").css("display", val ? "block" : "none");
	// Formular absenden
	updateCartAmount(jQuery("#cart_payment_form"));
}
function addTrustedShop(){
	updateCartAmount(jQuery("#cart_payment_form"));
}
function updateCartAmount(form) {
	var data = form.serialize();
	var url  = form.attr("action");
	
	form.children('.loading_overlay').fadeIn().spin(spinneropts);
	
	form.fadeTo('fast', 0.5);
		
	var callback = function(data, textStatus, jqXHR) {
		var cartAmount = jQuery(data).find(".show_cart_amount").eq(0).html();
		var cartTable = jQuery(data).find("#cart_montage_form .cart_table").eq(0).html()
		var trustedForm = jQuery(data).find("#trustedform").eq(0).html()
		form.removeAttr("disabled");
		jQuery('.show_cart_amount').html(cartAmount);
		jQuery('#cart_montage_form .cart_table').html(cartTable);
		jQuery('#trustedform').html(trustedForm);
		form.fadeTo("fast", 1);
		form.children('.loading_overlay').fadeOut();
		
	};
	form.attr("disabled", "disabled");
	var xhr = jQuery.post(url, data, callback, "html");
	xhr.error(function() { alert("Sorry. Error beim Aktualisieren der Gesamtkosten."); });
	
	return false;
}
