/*################################################################################
	D E B U G
################################################################################*/

var debugModus = 0	; // Debugmodus EIN = 1 || AUS = 0

function debug(txt)
{
	if(!debugModus || typeof(console) != "object") { return; }
	else { console.log(txt); }
}
debug("debugModus = true");


/*################################################################################
	$$$
################################################################################*/

// get Elements by Name

function $$$(obj)
{
	var obj = document.getElementsByName(obj);
	obj = (obj.length==1)?obj[0]:obj;
	return obj;
}


/*################################################################################
	F U N C T I O N S
################################################################################*/

/*################################################################################
	setBackgroundImage()
################################################################################*/

function setBackgroundImage()
{
	var defWidth	 	= 1024;
	var defHeight		= 695;
	var defRatio		= defWidth / defHeight;
	var hgbCt			= $('backgroundContainer');
	var img				= $('backgroundContainerIMG');
	
	hgbCt.style.width	= "0px";
	hgbCt.style.height	= "0px";
	
	var w				= window.getSize().x;
	var h				= window.getSize().y;
	
	hgbCt.style.width	= w + "px";
	hgbCt.style.height	= h + "px";
	
	var tmpRatio = w / h;
	if(tmpRatio > defRatio) {
		img.style.width		= w + "px";
		img.style.height	= w/defRatio + "px";
	} else {
		img.style.height	= h + "px";
		img.style.width		= h*defRatio + "px";
	}
	img.style.visibility="visible";
}

window.addEvent("resize",setBackgroundImage);
window.addEvent("load",setBackgroundImage);


/*################################################################################
	setTransparenz()
################################################################################*/

function setTransparenz()
{
	$('logoBackground').setOpacity(0.9);
	$('mainNaviBackground').setOpacity(0.6);
	$('selectedNaviBackground').setOpacity(0.8);
	$('warenkorbBackground').setOpacity(0.7);
	$('searchBackground').setOpacity(0.4);
	$('scrollBarBackground').setOpacity(0.4);
	$$('.subNaviBackground').each(function(el){
		el.setOpacity(1);
	});
	$('serviceNaviBackground').setOpacity(0.6);
}

window.addEvent('domready', setTransparenz);


/*################################################################################
	showSearchInput()
################################################################################*/

function showSearchInput() 
{
	document.getElementById("suche").style.display='none';
	document.getElementById("suchFeld").style.display='block';
}


/*################################################################################
	getFormPostString(id)
################################################################################*/

function getFormPostString(id)
{
    var resultStr;
    var frm = document.getElementById(id);
    var fields = frm.elements;
    var result = new Array();
    for(i=0;i<fields.length;i++)
    {
        var el = fields[i];
       
        switch(el.type.toLowerCase())
        {
            case "text": case "textarea": case "hidden": case "password":                
                result.push(el.name + "=" + el.value);
            break;
            case "checkbox":
                if(el.checked) { result.push(el.name + "=" + el.value); }
            break;
            case "radio":
                var ra = document.getElementsByName(el.name);
                for(r=0;r<ra.length;r++) { if(ra[r].checked) { result.push(ra[r].name + "=" + ra[r].value); } }
                i = i + (r-1);
            break;
            case "select-one":
            	result.push(el.name + "=" + el.options[el.selectedIndex].value);
            break;
        }
    }
    resultStr = result.join("&");
    return resultStr;
}



/*################################################################################
	showProduktDetail() & AJAX & hideProduktDetail();
################################################################################*/

function showProduktDetail(target, x, post, nextStep) {
	if(typeof post == 'undefined') {
		post = null;
	}
	
	var ajax = new Request(
	{
		method: 'post',
		url: '/'+lang+'/ajax/'+target,
		onSuccess: function(content)
		{
      // reload cart if needed
			if(target.indexOf('order.php')>-1 || target.indexOf('myaccount.php')>-1) {
				reloadCart();
			}
			
			// build overlay div
			$('dynamicContainer').style.width = x+'px';
			$('dynamicContainer').style.height = 'auto';
			$('dynamicContent').innerHTML = content;
			$('dynamicContainer').style.display = 'block';
			if($('dynamicContainer').offsetHeight > 518) {
				$('dynamicContainer').style.height = 500+'px';
				$('dynamicContainer').style.width = $('dynamicContainer').offsetWidth+'px';
			}

			// load next step validation
			if(typeof nextStep == 'function') {
				nextStep();
			}
			
			// prepare and send credit card form 
			if(target.indexOf('order.php?step=4')>-1 && $('merchantId') != null) {
				$('orderForm').action = 'https://payment.datatrans.biz/upp/jsp/upStart.jsp';
				$('orderForm').submit();
			}
		},
		onFailure: function() { console.log("AJAX ERROR"); }
	});
	ajax.send(post);
}

function validate_order_address() {
	validator.init($('orderForm'));
	validator.send=function() {
		showProduktDetail('order.php?step=3',700,getFormPostString('orderForm'),validate_order_overview);
	}
}

function validate_order_overview() {
	validator.init($('orderForm'));
	validator.send=function() {
		showProduktDetail('order.php?step=4',700,getFormPostString('orderForm'));
	}
}

function validate_recommend() {
	validator.init($('recommend'));
	validator.send=function() {
		showProduktDetail('empfehlen.php',600,getFormPostString('recommend'));
	}
}

function validate_cart() {
	validator.init($('orderForm'));
	validator.send=function() {
		showProduktDetail('order.php?step=2',850,getFormPostString('orderForm'),validate_order_address);
	}
}

function validate_contact() {
	validator.init($('contact'));
	validator.send=function() {
		showProduktDetail('kontakt.php',500,getFormPostString('contact'));
	}
}

function validate_account_password() {
	validator.init($('accountForm'));
	validator.send=function() {
		showProduktDetail('myaccount.php?step=2',550,getFormPostString('accountForm'),validate_account_password);
	}
}

function validate_account_address() {
	validator.init($('accountForm'));
	validator.send=function() {
		showProduktDetail('myaccount.php?step=1',850,getFormPostString('accountForm'),validate_account_address);
	}
}

function hideProduktDetail() {
	$('dynamicContainer').style.display='none';
	return false;
}


function showProduktDetail2(target, x, post, nextStep) {
	if(typeof post == 'undefined') {
		post = null;
	}
	
	var ajax = new Request(
	{
		method: 'post',
		url: '/'+lang+'/ajax/'+target,
		onSuccess: function(content)
		{
			// build overlay div
			$('dynamicContainer2').style.width = x+'px';
			$('dynamicContainer2').style.height = 'auto';
			$('dynamicContent2').innerHTML = content;
			$('dynamicContainer2').style.display = 'block';
			if($('dynamicContainer2').offsetHeight > 518) {
				$('dynamicContainer2').style.height = 500+'px';
				$('dynamicContainer2').style.width = $('dynamicContainer2').offsetWidth+'px';
			}

			// load next step validation
			if(typeof nextStep == 'function') {
				nextStep();
			}
		},
		onFailure: function() { console.log("AJAX ERROR"); }
	});
	ajax.send(post);
}

function hideProduktDetail2() {
	$('dynamicContainer2').style.display='none';
	return false;
}



/*################################################################################
	nextstep(id)
################################################################################*/

var basketWindows = new Array("step1","step2","step3");
var activeBasketWindow = 0;
function nextStep(goTo)
{
	switch(goTo)
	{
		case "+":
			$(basketWindows[activeBasketWindow++]).style.display = "none";
			$(basketWindows[activeBasketWindow]).style.display = "block";
		break;
		case "-":
			$(basketWindows[activeBasketWindow--]).style.display = "none";
			$(basketWindows[activeBasketWindow]).style.display = "block";
		break;
		default:
			$(basketWindows[activeBasketWindow]).style.display = "none";
			$(basketWindows[goTo]).style.display = "block";
			activeBasketWindow = goTo;
		break;
	}
}


	
/*################################################################################
	swapNaviPNG(img)
################################################################################*/

function swapNaviPNG(img)
{
	var defaultPath	= (Browser.Engine.trident4)?img.filters["DXImageTransform.Microsoft.AlphaImageLoader"].src:img.src;
	var hoverPath	= defaultPath.replace("inactive","active");
	
	if(Browser.Engine.trident4) { img.filters["DXImageTransform.Microsoft.AlphaImageLoader"].sizingMethod = "crop"; }
	
	img.onmouseout = function()
	{
		this.src = defaultPath;
		if(Browser.Engine.trident4) IE7.recalc();
	}
	img.onmouseover = function()
	{
		this.src = hoverPath;
		if(Browser.Engine.trident4) IE7.recalc();
	}
	
	img.onmouseover();
}

/*################################################################################
	changePNG(img,hoverPath)
################################################################################*/

function changePNG(img,hoverPath)
{
	img.src = hoverPath;
	if(Browser.Engine.trident4) IE7.recalc();
}


/*################################################################################
	slider
################################################################################*/

function slider()
{
	// Anzahl Produkte
	var anzProducts = $("innen").getChildren().length;
	
	// Breite eines Produkts = 1 Schritt zum Sliden
	// var widthProd = $("innen").getChildren().length;
	
	// setze Breite für Innen-Div mit allen Produkten
	$("innen").style.width = anzProducts * 248 +'px';
	var steps = anzProducts * 248 - $("mask").offsetWidth;
//	var steps=anzProducts-3
	
	// First Example
	var el = $('myElement');
				
	// Create the new slider instance
	new Slider(el, el.getElement('.knob'), {
		steps: steps,
	//	range: [8],	
		onChange: function(value){
			// Everytime the value changes, we change the font of an element
			//$('innen').style.left = (value) * -203+'px'; 
			$('innen').style.left = -value+'px'; 
		}
	}).set(0);
	
};

window.addEvent('load', slider);

/*################################################################################
	blaetter(richtung)
################################################################################*/

var startPosition = 0;
			
function blaetter(richtung) {
	var def = 248;
	var hiddenSteps = 3;
	if((richtung != "links" && startPosition == ((def*hiddenSteps)*-1)) || (richtung == "links" && startPosition == 0)) return;
	if(!isNaN(richtung)) {
		startPosition = (richtung*248)*-1;
	} else {
		startPosition = (richtung == "links") ? (startPosition + def) : (startPosition - def);
	}
	$("innen").style.marginLeft = startPosition + "px";
	//alert($("innen").style.marginLeft);
	
}		


/*################################################################################
	showLieferanschrift(id)
################################################################################*/

function showLieferanschrift() {
	var img = document.images.haekchen.src;
	if (img == "http://hsgshop.worksite.ch/img/main/kasten_inactive.jpg") {
		document.images.haekchen.src='/img/main/kasten_active.jpg';
		$('rightTable').style.display='block';
	} else {
		document.images.haekchen.src='/img/main/kasten_inactive.jpg';
		$('rightTable').style.display='none';
	}
}

/*################################################################################
	showPasswordReminder
################################################################################*/

function showPasswordReminder() {
	var status = $('passwordReminder').style.display;
	if(status == "block") {
		$('passwordReminder').style.display = "none";	
	} else {
		$('passwordReminder').style.display = "block";	
	}
}

/*################################################################################
	showOrderDetail
################################################################################*/

function showOrderDetail(id) {
	var tr = $('article_content_'+id);
	var img = $('article_img_'+id);
	if(tr.style.display == 'none') {
		img.src = '/img/main/kasten_minus.jpg';
		tr.style.display = '';
	} else {
		img.src = '/img/main/kasten_plus.jpg';
		tr.style.display = 'none';
	}
}

/*################################################################################
	reloadCart
################################################################################*/

function reloadCart() {
	var ajax = new Request(
	{
		method: 'post',
		url: '/'+lang+'/ajax/cart.php',
		onSuccess: function(content)
		{
			$('warenkorb').innerHTML = content;
		},
		onFailure: function() { console.log("ERROR: AJAX REQUEST"); }
	});
	ajax.send();
}

function updateSavedata() {
	var chbx = $('checkboxsavedata');
	var hidden = $('savedata');
	if(chbx.checked == true) {
		hidden.value = '1';
	} else {
		hidden.value = '0';
	}
}

/*################################################################################
	reloadCart
################################################################################*/

function checksearch() {
	el = $('suchFeld');
	if(el.value.length >= 3) {
		$('searchform').submit();
	} else {
		el.style.backgroundColor = '#FF0000';
		el.style.color = '#FFFFFF';
	}
}