var ns = document.layers ? true : false;
var ie = document.all && !window.opera ? true : false;;    // IEs
var dom = document.getElementById && !ie ? true : false;  // DOM-Browser

function ImagePreload() 
{ 
 	document.Preload = new Array(); 
 	if(document.images) 
 	{ 
  		for(var i=0; i < ImagePreload.arguments.length; i++) 
  		{ 
   			document.Preload[i] = new Image(); 
   			document.Preload[i].src = ImagePreload.arguments[i]; 
  		} 
		
 	} 
} 

function setHash(hash){
	window.location.hash=hash;	
}

function ajaxhist(func,tc){
	var funcstring=func+"(";
	for(var i=2; i < ajaxhist.arguments.length; i++) 
  		{ 
			funcstring+='"'+ajaxhist.arguments[i]+'"';
			if((i+1) < ajaxhist.arguments.length) funcstring+=',';
		}
	funcstring+=')';
	var jetzt = new Date();
	$('historyframe').src='history.php?tc='+tc+'&callback='+escape(funcstring)+'#'+jetzt.getTime();
}

/////////////////////////
 // Graybox Stuff Start //
/////////////////////////
function showGrayBox() {
	center('gray_box');
	$('gray_overlay').show(); 
	hideSelect();
    return false;
}

function hideGrayBox(){
    $('gray_box').hide();
    $('gray_overlay').hide();
	showSelect();
	 $('gray_box_content').innerHTML = '<img src="images/loading_small2.gif" /><img src="images/loading_font.gif" />';
    return false;
}

function zoomImage(src){
	var zImg=src.split('/');
	$('gray_overlay').show();
	$('gray_box_content').innerHTML = "<img src='images/produkte/"+zImg[(zImg.length-1)]+"'>";
	center('gray_box');
	window.setTimeout("center('gray_box');",500);
	window.setTimeout("center('gray_box');",1500);
	hideSelect();
}

function changePassword(){
	// passwort über graybox änderbar
	hideSelect();
	
	//$('gray_box').show();
	$('gray_overlay').show();
	
	$('gray_box_content').innerHTML = "<table border='0' width='300' cellspacing='0' cellpadding='5'><tr><td colspan='2' align='left' style='font-size: 16px; font-weight: bold;'>Passwort &auml;ndern</td></tr><tr><td align='right'>Altes Passwort:</td><td align='left'><input type='password' name='old_pw' class='pw_password' id='old_pw'></td></tr><tr><td align='right'>Neues Passwort:</td><td align='left'><input type='password' name='new_pw' class='pw_password' id='new_pw'></td></tr><tr><td align='right'>Passwort wiederholen:</td><td align='left'><input type='password' name='new_pw_wdh' class='pw_password' id='new_pw_wdh'></td></tr><tr><td><div id='pwloading' style='display: none;'><img src='images/loading_small2.gif' /> <img src='images/loading_font.gif' /></div>&nbsp;</td><td align='left'><input type='button' style='background-image:url(images/btn_pwaendern.gif);width:128px;height:18px;border:none;' onClick='passwortUebernehmen();'></td></tr></table>";
	center('gray_box');
}

function passwortUebernehmen(){
	var pwvgl;
	$('pwloading').show();
	new Ajax.Request('ajax/md5.php',
	{
		method: 'get',
		asynchronous: true,
		evalScript: true,
		parameters: 'str='+$('old_pw').value,
		onComplete: function(t) {	
			pwvgl=t.responseText;
			if($('new_pw')){
				//alert($('md5pw').value + "\n\n" + pwvgl);
				if(pwvgl!=$('md5pw').value){
					alert('Altes Passwort ist nicht korrekt.');
				}
				else if($('new_pw').value!=$('new_pw_wdh').value){
					alert('Neues Passwort stimmt nicht mit der Wiederholung überein.');
				}
				else if($('new_pw').value.length<6){
					alert('Neues Passwort muss mindestens 6 Zeichen haben.');
				}
				else{
					// alles klar - passwort ändern
					new Ajax.Request('ajax/profil.php?changepw=true',
					{
						method: 'post',
						asynchronous: true,
						evalScript: true,
						postBody: 'new_passwort='+$('new_pw').value,
						onComplete: function(t) {	
							showSelect();
							hideAllBoxes();
							$('hbprofil').update(t.responseText);
							$('profilresponse').innerHTML="Ihr Passwort wurde erfolgreich ge&auml;ndert.";
						},
						onFailure: function(t) {
							alert('Passwort konnte nicht geändert werden:\n\n'+t.status+' -- '+t.statusText);	
						}
					}
					);
				}
			}
		},
		// Handle other errors
		onFailure: function(t) {
			alert('Crypt-Error ' + t.status + ' -- ' + t.statusText);
		}		
	},
	$('pwloading').hide()
	);	
}

function profilAendern(){
	$('profil_loading').show();
	new Ajax.Request('ajax/profil.php',
		{
			method: 'post',
			asynchronous: true,
			evalScript: true,
			postBody: $('profilform').serialize(),
			onComplete: function(t) {	
				showSelect();
				hideAllBoxes();
				$('hbprofil').update(t.responseText);
				$('profil_loading').hide();
				$('profilresponse').innerHTML = "Daten wurde erfolgreich ge&auml;ndert.";
			},
			onFailure: function(t) {
				alert('Passwort konnte nicht geändert werden:\n\n'+t.status+' -- '+t.statusText);	
			}
		}
		);	
}


function showPinkBox() {
	$('gray_overlay').show(); 
	hideSelect();
    return false;
}

function hidePinkBox(){
    $('pink_box').hide();
	$('gray_overlay').hide();
	showSelect();
    return false;
}

function hideAllBoxes(){
	hideGrayBox();
	hidePinkBox();	
}

function center(element){
    try{
        element = $(element);
    }catch(e){
        return;
    }
	
	var arrayPageSize = getPageSize();
	var arrayPageScroll = getPageScroll();
	
	//Größe verändern
	if(ie) add = 50;
	else add = 0;
	
	if(ie) breite = arrayPageSize[0]+"px";
	else breite = "100%";
	
	$('gray_overlay').style.width = breite;
	$('gray_overlay').style.height = (arrayPageSize[1]+add)+"px";
	
	//alert(window.document.body.offsetHeight + " - " + document.body.clientHeight + " - " + document.documentElement.clientHeight + " - " + window.innerHeight + " - " + document.documentElement.scrollTop);
    
	element.style.position = 'absolute';
    element.style.zIndex   = 99;

    var elementDimensions = Element.getDimensions(element);

	//alert(elementDimensions.width + " " + element +" " + elementDimensions.height);
	
	element.style.top = (arrayPageScroll[1] + ((arrayPageSize[3] - 35 - elementDimensions.height) / 2) + 'px');
	element.style.left = (((arrayPageSize[0] - 20 - elementDimensions.width) / 2) + 'px');

    element.style.display  = 'block';
	
	//alert(arrayPageSize[0] + " - " + arrayPageSize[1] + " - " + arrayPageSize[2] + " - " + arrayPageSize[3]);
	//alert(arrayPageScroll[0] + " - " + arrayPageScroll[1]);
}

function getPageScroll(){

	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}

	arrayPageScroll = new Array('',yScroll) 
	return arrayPageScroll;
}


function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}
/*
\\\\\\\\\\\\\\\\\\\\\\\\
 \\ Graybox Stuff ENDE \\
  \\\\\\\\\\\\\\\\\\\\\\\\
*/

function hideSelect() {
	selects = parent.document.getElementsByTagName( 'select' );
   	for ( i = 0; i != selects.length; i++ )
	{
		selects[i].style.visibility = 'hidden';
    }
}

function showSelect() {
	selects = parent.document.getElementsByTagName( 'select' );
    for ( i = 0; i != selects.length; i++ )
	{
		selects[i].style.visibility = 'visible';
	}
}


//OpenLogin
function openLogin() {
	showGrayBox();
	new Ajax.Request('modules_gb/login.php',
	{
		method: 'get',
		asynchronous: true,
		evalScript: true,
		onComplete: function(t) {	
		   $('gray_box_content').update(t.responseText);
		   center('gray_box');
		},
		// Handle other errors
		onFailure: function(t) {
			alert('Error ' + t.status + ' -- ' + t.statusText);
		}		
	}
	);
}

function pwVergessen() {
	$('gray_box_content').innerHTML = '<img src="images/loading_small2.gif" alt="" /><img src="images/loading_font.gif" alt="" />';
	new Ajax.Request('modules_gb/lostpw.php',
	{
		method: 'get',
		asynchronous: true,
		evalScript: true,
		onComplete: function(t) {	
		   $('gray_box_content').update(t.responseText);
		   center('gray_box');
		},
		// Handle other errors
		onFailure: function(t) {
			alert('Error ' + t.status + ' -- ' + t.statusText);
		}		
	}
	);
}

//Logincheck
function checkLogin() {
	$('login_loading').show();
	new Ajax.Request('ajax/login.php',
	{
		method: 'post',
		postBody: $('login_form').serialize(),
		asynchronous: true,
		evalScript: true,
		onComplete: function(t) {
		   $('login_loading').hide();	
		   $('login_result').update(t.responseText);
		},
		// Handle other errors
		onFailure: function(t) {
			alert('Error ' + t.status + ' -- ' + t.statusText);
		}		
	}
	);
}

function checkLogin2() {
	$('login_loading2').show();
	new Ajax.Request('ajax/login2.php',
	{
		method: 'post',
		postBody: $('login_form2').serialize(),
		asynchronous: true,
		evalScript: true,
		onComplete: function(t) {
		   $('login_loading2').hide();	
		   $('login_result2').update(t.responseText);
		},
		// Handle other errors
		onFailure: function(t) {
			alert('Error ' + t.status + ' -- ' + t.statusText);
		}		
	}
	);
}

function checkLostPw() {
	$('lostpw_loading').show();
	new Ajax.Request('ajax/lostpw.php',
	{
		method: 'post',
		postBody: $('lostpw_form').serialize(),
		asynchronous: true,
		evalScript: true,
		onComplete: function(t) {
		   $('lostpw_loading').hide();	
		   $('lostpw_result').update(t.responseText);
		},
		// Handle other errors
		onFailure: function(t) {
			alert('Error ' + t.status + ' -- ' + t.statusText);
		}		
	}
	);
}

//Bildanzeigen
function displayBild(w,h,img,titel) {
	showGrayBox();
	new Ajax.Request('modules_gb/bild.php',
	{
		method: 'get',
		asynchronous: true,
		evalScript: true,
		parameters: "w="+w+"&h="+h+"&img="+img+"&titel="+titel,
		onComplete: function(t) {	
		   $('gray_box_content').update(t.responseText);
		   center('gray_box');
		},
		// Handle other errors
		onFailure: function(t) {
			alert('Error ' + t.status + ' -- ' + t.statusText);
		}		
	}
	);
}

//Bestellung anzeigen
function displayOrders(ident,title) {
	showGrayBox();
	new Ajax.Request('modules_gb/orders.php',
	{
		method: 'get',
		asynchronous: true,
		evalScript: true,
		parameters: "ident="+ident+"&title="+title,
		onComplete: function(t) {	
		   $('gray_box_content').update(t.responseText);
		   center('gray_box');
		},
		// Handle other errors
		onFailure: function(t) {
			alert('Error ' + t.status + ' -- ' + t.statusText);
		}		
	}
	);
}

function empfehlen(ident,qrystring){
	showGrayBox();
	new Ajax.Request('ajax/empfehlen.php',
	{
		method: 'get',
		asynchronous: true,
		evalScript: true,
		parameters: "ident="+ident+"&qstring="+qrystring,
		onComplete: function(t) {
			$('gray_box_content').update(t.responseText);
			center('gray_box');
		},
		onFailure: function(t) {
			alert('Error ' + t.status + ' -- ' + t.statusText);
		}		
	}
	);
}



function sendempfehlen() {
	//$('loading').show();
	new Ajax.Request('ajax/empfehlen.php',
	{
		method: 'post',
		postBody: $('empfehlen_form').serialize(),
		asynchronous: true,
		evalScript: true,
		onComplete: function(t) {
		  // $('loading').hide();	
		   $('gray_box_content').update(t.responseText);
		   center('gray_box');
		},
		// Handle other errors
		onFailure: function(t) {
			alert('Error ' + t.status + ' -- ' + t.statusText);
		}		
	}
	);
}

function reorder(ident){
	new Ajax.Request('ajax/reorder.php',
		{
			method: 'get',
			asynchronous: true,
			evalScript: true,
			parameters: "ident="+ident,
			onComplete: function(t) {
				showGrayBox();
				$('gray_box_content').update(t.responseText);
				center('gray_box');
			},
			onFailure: function(t) {
				alert('Error ' + t.status + ' -- ' + t.statusText);
			}		
		}
	);
}

var newkat;
var opts;

function changeKat(newkat,opts){
	window.setTimeout("document.location.href='main.php?kat="+newkat+"&"+opts+"';",5);	
}

Xoffset=5;   // modify these values to ...
Yoffset=0;    // change the popup position.
var x = 0;
var y = 0;
document.onmousemove=get_mouse;
	
function popup(headline,text){
	if(text != '') {
				var content= "<table border='0' cellspacing='0' cellpadding='2' width='500'><tr><td style='background-color: #2e2f43; color: white;'>"+headline+"</td></tr><tr><td>"+text+"</td></tr></table>";
				$('hoverdiv').innerHTML=content;
				$('hoverdiv').style.left=(x-(Xoffset+$('hoverdiv').getWidth()))+"px";
				$('hoverdiv').style.top=(y+Yoffset)+"px";
				$('hoverdiv').show();
	}
}
	
function get_mouse(e){
	if(!e) e = window.event;
	x = e.clientX+document.documentElement.scrollLeft;
	//	if (document.documentElement.scrollTop!=0) alert(document.documentElement.scrollTop );
    y = e.clientY+document.documentElement.scrollTop ;
}
function kill(){
	$('hoverdiv').hide();			
}
	
function customCursor(img) {	
	var objBody = document.getElementsByTagName( 'body' ).item( 0 );
	
	if($('custom_cursor')) {
		$('custom_cursor').show();
		$('custom_cursor').style.left = x + "px";
		$('custom_cursor').style.top = y + "px";
	} else {
		var customc = document.createElement("div");
		
		customc.setAttribute('id', 'custom_cursor');
		
		customc.innerHTML = "<img src='"+img+"' border='0' />";
		
		customc.style.position = "absolute";
		customc.style.left = x + "px";
		customc.style.top = y + "px";
		
		objBody.appendChild(customc);
	}
	
}

function killCursor() {
	if($('custom_cursor')) $('custom_cursor').hide();
}
	
selectBox = function(selectName,boxWidth,boxHeight) {

    /*
    **     selectbox v0.0.0.0.7.3pre-alpha rev. 0.5cvs
    **     © 2007 christian kaiser
    */


    // Eigenschaften
    //

    var staticFontSize        = new Number(12);        // Textfeld Schriftgröße
    var staticFontFace        = new String("arial");        // Textfeld Schriftart
    var staticBorderColor     = new String("#979797");    // Textfeld Rahmenfarbe

    var imageWidth            = new Number(19);
    var img_up_mout           = new String("images/up.gif");        // image up-scroll mouseout
    var img_up_mover          = new String("images/up2.gif");    // image up-scroll mouseover
    var img_down_mout         = new String("images/down.gif");    // image down-scroll mouseout
    var img_down_mover        = new String("images/down2.gif");    // image down-scroll mouseover
    
    var output          = new String();
    var spanRandId      = new String(Math.floor(Math.random()*10000000));
    var selectRandId    = new String(Math.floor(Math.random()*10000000));
    var imagesRandId    = new String(Math.floor(Math.random()*10000000));

    
    output += "<table cellspacing='0px' cellpadding='0px'>";
    output += "<tr>";
    output += "<td rowspan='2'>";
    output += "<span id='"+spanRandId+"'>";
    output += "<input type='text' style='";
    output += "width:"+(boxWidth-imageWidth)+"px;";
    output += "font:"+staticFontSize+"px "+staticFontFace+";";
    output += "border:1px solid "+staticBorderColor+";";
    output += "height:"+boxHeight+"px;cursor:default' ";
    output += "id='_"+selectRandId+"_1' readonly>";
    output += "<input type='hidden' id='_"+selectRandId+"_2' name='"+selectName+"'>";
    output += "</span>";
    output += "</td>";
    output += "<td valign='bottom'>";
    output += "<img src='"+img_up_mout+"' name='"+imagesRandId+"_1' ";
    output += "onmouseover=\"this.src='"+img_up_mover+"'\" ";
    output += "onmouseout=\"this.src='"+img_up_mout+"'\" ";
    output += "onclick=\"document.getElementById('"+spanRandId+"').setLower()\">";
    output += "</td>";
    output += "</tr>";
    output += "<tr>";
    output += "<td valign='top'>";
    output += "<img src='"+img_down_mout+"' name='"+imagesRandId+"_2' ";
    output += "onmouseover=\"this.src='"+img_down_mover+"'\" ";
    output += "onmouseout=\"this.src='"+img_down_mout+"'\" ";
    output += "onclick=\"document.getElementById('"+spanRandId+"').setUpper()\">";
    output += "</td>";
    output += "</tr>";
    output += "</table>";

    document.write(output);

    var obj               = document.getElementById(spanRandId);
    obj.selectedIndex     = 0;
    obj.spanRandId        = spanRandId;
    obj.selectLable       = new Array();
    obj.selectValue       = new Array();
    obj.valueBoxId        = document.getElementById("_"+selectRandId+"_1");
    obj.optionBoxId       = document.getElementById("_"+selectRandId+"_2");

    obj.setValues = function() {
        this.valueBoxId.value  = this.selectLable[this.selectedIndex];
        this.optionBoxId.value = this.selectValue[this.selectedIndex];
    }

    obj.setUpper = function() {
        if(this.selectedIndex<this.selectLable.length-1) {
            this.selectedIndex++;
            this.setValues();
        }
    };

    obj.setLower = function() {
        if(this.selectedIndex>0) {
            this.selectedIndex--;
            this.setValues();
        }
    };

    this.prototype = obj;
    this.addValues = function(lable,value) {

        var p = this.prototype;

        p.selectLable[p.selectLable.length] = lable;
        p.selectValue[p.selectValue.length] = value;

        p.valueBoxId.value  = p.selectLable[0];
        p.optionBoxId.value = p.selectValue[0];

        p = undefined;
    };

    obj                  = undefined;
    output               = undefined;
    spanRandId           = undefined; 
    selectRandId         = undefined;
    imagesRandId         = undefined;
    
    staticFontSize       = undefined;
    staticFontFace       = undefined;
    staticBorderColor    = undefined;

    imageWidth           = undefined;
    img_up_mout          = undefined;
    img_up_mover         = undefined;
    img_down_mout        = undefined;
    img_down_mover       = undefined;
};