var dom = (document.getElementById) ? true : false;
var ns5 = ((navigator.userAgent.indexOf("Gecko")>-1) && dom) ? true: false;
var ie5 = ((navigator.userAgent.indexOf("MSIE")>-1) && dom) ? true : false;
var ns4 = (document.layers && !dom) ? true : false;
var ie4 = (document.all && !dom) ? true : false;
var nodyn = (!ns5 && !ns4 && !ie4 && !ie5) ? true : false;

function favorite() {
	var lang;
	var name=navigator.appName;
	var version = navigator.appVersion;
	if (name=="Microsoft Internet Explorer" && version >= "4.0"){
		lang=(navigator.browserLanguage);
		if(document.all){
			window.external.AddFavorite(url, LocalLang['addfavorite']);
		}
	}
	else{
		window.sidebar.addPanel(LocalLang['addfavorite'], url, '');
	}
}

var cookieInhalt = new Array();
var splitt='|';

/*MERKLISTE*/
function Get_Cookie(name) { 
   var start = document.cookie.indexOf(name+"="); 
   var len = start+name.length+1; 
   if ((!start) && (name != document.cookie.substring(0,name.length))) return null; 
   if (start == -1) return null; 
   var end = document.cookie.indexOf(";",len); 
   if (end == -1) end = document.cookie.length; 
   return unescape(document.cookie.substring(len,end)); 
} 

function Set_Cookie(name,value,days,path,domain,secure) { 
    var today = new Date(); 
    var cookie_expire_date = new Date(today.getTime() + (days * 86400000))
    
    var cookieString = name + "=" +escape(value) + 
       ( (cookie_expire_date) ? ";expires=" + cookie_expire_date.toGMTString() : "") + 
       ( (path) ? ";path=" + path : "") + 
       ( (domain) ? ";domain=" + domain : "") + 
       ( (secure) ? ";secure" : ""); 
    document.cookie = cookieString; 
} 

function Delete_Cookie(name,path,domain) { 
   if (Get_Cookie(name)) document.cookie = name + "=" + 
      ( (path) ? ";path=" + path : "") + 
      ( (domain) ? ";domain=" + domain : "") + 
      ";expires=Thu, 01-Jan-70 00:00:01 GMT"; 
} 

function savemerkliste(element){
	
	var str = '';
	
	//Aktivieren
	if(element.childNodes[0].name == 'off'){
		if(cookieInhalt.length<=20){
			cookieInhalt[cookieInhalt.length] = element.id;
			document.getElementById(element.id).innerHTML="<img src=\"" + url + "/img/checked.gif\" width=\"14\" height=\"13\" name=\"on\" style=\"cursor: pointer;\"> " + LocalLang['watchlist'];
		}
		else {
			alert(LocalLang['watchlist_max']);
		}
	}
	//Deaktivieren
	else {
		for(x in cookieInhalt) {
			if(cookieInhalt[x]==element.id) {
				cookieInhalt.splice(x, 1);
				break;
			}
		}
		document.getElementById(element.id).innerHTML="<img src=\"" + url + "/img/site_pfeil.gif\" width=\"14\" height=\"12\" name=\"off\" style=\"cursor: pointer;\"> " + LocalLang['watchlist'];
		
	}
	
	if(getContainer(element, 'DIV', 'quickviewitem')){
		Dialog.SetDialogContent(document.getElementById('quickview_' + element.id).innerHTML);
	}

	if(cookieInhalt.length>0) {
		for(y in cookieInhalt) {
			str += cookieInhalt[y];
			if(y<cookieInhalt.length-1)
				str += splitt;	
		}
	
	}
	
	Set_Cookie("ML",str,30,"/");
	
	if(cookieInhalt.length>0){
		document.getElementById("watchlist").innerHTML = LocalLang['watchlist_my'] + " ("+cookieInhalt.length+")";
	}
	else {
		document.getElementById("watchlist").innerHTML = LocalLang['watchlist_my'];
	}
}

function loadMerkliste() {
	var cookieWert=Get_Cookie("ML");
	if(cookieWert&& cookieWert != "empty"){
     cookieInhalt = cookieWert.split(splitt);
    if(cookieInhalt.length>0){
			document.getElementById("watchlist").innerHTML = LocalLang['watchlist_my'] + " ("+cookieInhalt.length+")";
		}
		else {
			document.getElementById("watchlist").innerHTML = LocalLang['watchlist_my'];
		}      		
  }
}

function merkliste(id){
	var inCookie = 0;
	
	for(x in cookieInhalt)
	{
		if(cookieInhalt[x]==id) {
			inCookie = 1;
			break;
		}
	}

	if(inCookie == 1){
		document.write("<span id=\""+id+"\" onclick=\"savemerkliste(this)\" style=\"cursor: pointer; font-size:11px;\"><img src=\"" + url + "/img/checked.gif\" width=\"13\" height=\"13\" name=\"on\"> " + LocalLang['watchlist'] + "</span>");
	}
	else{
		document.write("<span id=\""+id+"\" onclick=\"savemerkliste(this)\" style=\"cursor: pointer; font-size:11px;\"><img src=\"" + url + "/img/site_pfeil.gif\" width=\"14\" height=\"12\" name=\"off\"> " + LocalLang['watchlist'] + "</span>");	
	}

}

/*Checkbox komplett markieren*/
var checkbox=0;
function select_checkbox(id){
	
	if(checkbox == 0){
		for(x=0; x<document.forms[id].elements.length; x++){
			document.forms[id].elements[x].checked = true;
		}
		checkbox=1;
	}
	else {
		for(x=0; x<document.forms[id].elements.length; x++){
			document.forms[id].elements[x].checked = false;
		}		
		checkbox=0;
	}
}
/*Auswahl Multi Select-Box komplett aufheben*/
function clearSelected(obj) {
	for (i=0; i<obj.options.length; i++) {
		if (obj.options[i].selected) {
			obj.options[i].selected = false;
		}
	}
}

function ebayad(query, cquery, limit, div){
	var aurl = url + "/ad/ebayad.php?q=" + encodeURIComponent(query) + "&cq=" + encodeURIComponent(cquery) + "&limit=" + limit;
	var ajax = new Ajax(
		aurl,			
		function (req){
			var xs = JSON.parse(req.responseText);
			if(xs.cat == 1)
			document.getElementById('ebayheader').innerHTML = "&quot;" + cquery + "&quot; " + LocalLang["ebay"];
			
			document.getElementById(div).innerHTML = xs.html;;
		}
	);
	ajax.request();
}

function checksearchlength(){
	return document.getElementById("search").value.length > 0 ? true : false;
}

function GotoUrl(burl, nw){
	var surl = burl.split('.');
	var furl=url;
	for(var i=0; i < surl.length; i++){
		furl+='/' + Base64.decode(surl[i]);
	}
	if(nw)
	window.open(furl);
	else
	location.href=furl;
}

/*////////////////////////////////////////////////////////////////////////////////////////////
//Base 64
////////////////////////////////////////////////////////////////////////////////////////////*/
var Base64 = {
	// private property
	_keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
	
	// public method for encoding
	encode : function (input) {
		var output = "";
		var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
		var i = 0;
 
		input = Base64._utf8_encode(input);
 
		while (i < input.length) {
 
			chr1 = input.charCodeAt(i++);
			chr2 = input.charCodeAt(i++);
			chr3 = input.charCodeAt(i++);
 
			enc1 = chr1 >> 2;
			enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
			enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
			enc4 = chr3 & 63;
 
			if (isNaN(chr2)) {
				enc3 = enc4 = 64;
			} else if (isNaN(chr3)) {
				enc4 = 64;
			}
 
			output = output +
			this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
			this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);
 
		}
 
		return output;
	},
 
	// public method for decoding
	decode : function (input) {
		var output = "";
		var chr1, chr2, chr3;
		var enc1, enc2, enc3, enc4;
		var i = 0;
 
		input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
 
		while (i < input.length) {
 
			enc1 = this._keyStr.indexOf(input.charAt(i++));
			enc2 = this._keyStr.indexOf(input.charAt(i++));
			enc3 = this._keyStr.indexOf(input.charAt(i++));
			enc4 = this._keyStr.indexOf(input.charAt(i++));
 
			chr1 = (enc1 << 2) | (enc2 >> 4);
			chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
			chr3 = ((enc3 & 3) << 6) | enc4;
 
			output = output + String.fromCharCode(chr1);
 
			if (enc3 != 64) {
				output = output + String.fromCharCode(chr2);
			}
			if (enc4 != 64) {
				output = output + String.fromCharCode(chr3);
			}
 
		}
 
		output = Base64._utf8_decode(output);
 
		return output;
 
	},
 
	// private method for UTF-8 encoding
	_utf8_encode : function (string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";
 
		for (var n = 0; n < string.length; n++) {
 
			var c = string.charCodeAt(n);
 
			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}
 
		}
 
		return utftext;
	},
 
	// private method for UTF-8 decoding
	_utf8_decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;
 
		while ( i < utftext.length ) {
 
			c = utftext.charCodeAt(i);
 
			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}
 
		}
 
		return string;
	}
}
/*////////////////////////////////////////////////////////////////////////////////////////////
//URL encode / decode
////////////////////////////////////////////////////////////////////////////////////////////*/
var Url = {
 
	// public method for url encoding
	encode : function (string) {
		return escape(this._utf8_encode(string));
	},
 
	// public method for url decoding
	decode : function (string) {
		return this._utf8_decode(unescape(string));
	},
 
	// private method for UTF-8 encoding
	_utf8_encode : function (string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";
 
		for (var n = 0; n < string.length; n++) {
 
			var c = string.charCodeAt(n);
 
			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}
 
		}
 
		return utftext;
	},
 
	// private method for UTF-8 decoding
	_utf8_decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;
 
		while ( i < utftext.length ) {
 
			c = utftext.charCodeAt(i);
 
			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}
 
		}
 
		return string;
	}
 
}

/*////////////////////////////////////////////////////////////////////////////////////////////
//Navigationsfunktionen
////////////////////////////////////////////////////////////////////////////////////////////*/
function filter(cat, value, search){	
	var loc;
	if(search){
		loc=url+'/?search='+ Url.encode(cat) + value;
	}
	else {
		loc=url+'/'+ cat;
		if(value){			
			var value_splitt = value.replace('\?','');
			value_splitt = value_splitt.split('=');
			if(value_splitt[1] == 'clear')
			Delete_Cookie(value_splitt[0],"/");
			else {
				Set_Cookie(value_splitt[0],value_splitt[1],0.5,"/");
				loc+='/'+value;
			}
		}
	}

	location.href=loc;
}

function navigate(url){
	var sp = url.split('?');
	sp = sp[1].split('&');
	sp = sp[1].split('=');
	if(sp[0] == 'limit' && sp[1] == '20'){
		url = url.replace('&limit=20','');
		Delete_Cookie(sp[0],"/");
	}
	else if(sp[0] == 'sort' && sp[1] == '0'){
		url = url.replace('&sort=0','');
		Delete_Cookie(sp[0],"/");
	}
	else
	Set_Cookie(sp[0],sp[1],0.5,'/');
	location.href=url;
}

function action(value){
	location.href=url+'/action/'+value;
}

function watchlist(){
	location.href=url+'/merkliste';
}

var modetip, Ex,Ey;
function popuptip(name) {	
	if (modetip = document.getElementById(name));
	if (!modetip) return;
	var offX		 	= 4;
	var offY		 	= 4; 
	var obj = (window.document.compatMode && window.document.compatMode == "CSS1Compat") ? window.document.documentElement : window.document.body || null;
	var tpWd = (ns4)? modetip.width: (ie4||ie5)? modetip.clientWidth: 300;
	var tpHt = (ns4)? modetip.height: (ie4||ie5)? modetip.clientHeight: modetip.offsetHeight;
	var winWd = (ns4||ns5)? window.innerWidth-20+window.pageXOffset: obj.clientWidth+obj.scrollLeft;
	var winHt = (ns4||ns5)? window.innerHeight-20+window.pageYOffset: obj.clientHeight+obj.scrollTop;

	if ((Ex+offX+tpWd)>winWd) 
	modetip.style.left = Ex-(tpWd+offX)+"px";
	else 
	modetip.style.left = Ex+"px";
	
	if ((Ey+offY+tpHt)>winHt) 
	modetip.style.top = winHt-(tpHt+offY)+"px";
	else 
	modetip.style.top = Ey+"px";
	
	modetip.style.display = "block";
}
function popdowntip() {
	if (!modetip) return;
	modetip.style.display = "none";			
}

function Position(e)
{	
	var win = window;
	Ex = (ns4||ns5)? e.pageX: win.event.x;
	Ey = (ns4||ns5)? e.pageY: win.event.y;
}

/*////////////////////////////////////////////////////////////////////////////////////////////
//Fade von Filterboxen
////////////////////////////////////////////////////////////////////////////////////////////*/
var fadeBoxbuffer=new Array();
var fadeBoxbufferrun=new Array();
function fadeBox(id, fade){
	var box=document.getElementById(id);
	if(box.offsetHeight > 2 && fadeBoxbufferrun[id] != 'undefined' && fadeBoxbufferrun[id] != 1){
		if(!fadeBoxbuffer[id])
		fadeBoxbuffer[id] = box.offsetHeight-8;		
		//Faden erlaubt
		if(fade == 1){
			fadeBoxbufferrun[id]=1;
			document.getElementById(id+'_img').src='/img/arrow_right.png';
			document.getElementById(id+'_title').title=LocalLang["box_out"];
			fadeit(id, "in");
			
		}
		else{
			box.style.height=0 + 'px';
			box.style.padding=0 + 'px';
		}
			
	}
	else if(fadeBoxbufferrun[id] != 1){
		fadeBoxbufferrun[id]=1;
		document.getElementById(id+'_img').src='/img/arrow_down.png';
		document.getElementById(id+'_title').title=LocalLang["box_in"];
		fadeit(id, "out");
		//box.style.height=fadeBoxbuffer[id] + 'px';
		//box.style.padding=4 + 'px';
	}
}

function fadeit(obj, mode){
	var box=document.getElementById(obj);
	var box_letter=document.getElementById(obj + "_letter");
	var Ht = (ns4)? box.height: (ie4||ie5)? box.clientHeight: box.offsetHeight;
	if(mode == "in"){
		if(box_letter)
		box_letter.style.display = 'none';
		
		if(Ht-8 > 2){
			nextheight=Ht-30 > 0 ? Ht-30 : 0;
			box.style.height=nextheight  +'px';
			window.setTimeout("fadeit('"+obj+"','"+mode+"')", 100);
		}
		else {
			box.style.height=0 + 'px';
			box.style.padding=0 + 'px';
			fadeBoxbufferrun[obj]=0;
		}
	}

	else if(mode == "out"){
		if(parseInt(box.style.height) == 0 || Ht-8 < fadeBoxbuffer[obj]){
			box.style.padding=4 + 'px';
			nextheight=Ht+20 < fadeBoxbuffer[obj] ? Ht+20 : fadeBoxbuffer[obj];
			box.style.height=nextheight  +'px';
			window.setTimeout("fadeit('"+obj+"','"+mode+"')", 100);
		}
		else {
			fadeBoxbufferrun[obj]=0;
			if(box_letter)
			box_letter.style.display = '';
		}
	}
}

function scrollBox(id, sign){
	var obj = document.getElementById(id);
	var obj_sign = document.getElementById(id + sign);
	var thePos=obj_sign.offsetTop-obj.offsetTop-2; 
	obj.scrollTop=thePos;
}

/*////////////////////////////////////////////////////////////////////////////////////////////
//Schnellansichtfunktion
////////////////////////////////////////////////////////////////////////////////////////////*/
var active_qv = null;
function QuickView(event){
	var e = (event.target) ? event.target : event.srcElement;
	var container = getContainer(e, 'DIV', 'productpic')
	if(container == null && active_qv != null){
		document.getElementById(active_qv).lastChild.style.display = 'none';	
		active_qv=null;
	}
	
	if(container != null){
		var pid = container.getAttributeNode("id").nodeValue;
		if(active_qv != null && container.getAttributeNode("id") && active_qv != pid)
		document.getElementById(active_qv).lastChild.style.display = 'none';
		
		var qv = container.lastChild;
		qv.style.display = 'block';
		active_qv = pid;
		
		if(e.getAttributeNode("class") && e.getAttributeNode("class").nodeValue == 'quickview'){
			var prid = pid.split("_");
			e.onclick = function(){
				Dialog.Dialog("", document.getElementById('quickview_' + prid[1]).innerHTML);
			};
		}
	}
}

function getContainer(node, tagName, attribute) {
	
	if(attribute.indexOf('#') == 0){
	  while (node != null) {
	    if (node.tagName != null && node.tagName == tagName && hasId(node, attribute.replace('#','')))
	      return node;
	    node = node.parentNode;
	  }
	}
	else{
		while (node != null) {
	    if (node.tagName != null && node.tagName == tagName && hasClassName(node, attribute))
	      return node;
	    node = node.parentNode;
	  }
	}

  return node;
}

function hasClassName(el, name) {

  var i, list;
  list = el.className.split(" ");
  for (i = 0; i < list.length; i++)
    if (list[i] == name)
      return true;

  return false;
}

function hasId(el, name) {
	var i, list;
	list = el.id.split(" ");
	for (i = 0; i < list.length; i++)
	  if (list[i] == name)
	    return true;
	return false;
}

/*////////////////////////////////////////////////////////////////////////////////////////////
//Produktnavigation
////////////////////////////////////////////////////////////////////////////////////////////*/
var active_in = null;
function ItemNavi(event){
	
	var e = (event.target) ? event.target : event.srcElement;
	var pcontainer = getContainer(e, 'DIV', '#produktdetails')
	if(pcontainer == null && active_in != null){
		var pnavl = document.getElementById('produktdetails_nav_left');
		var pnavr = document.getElementById('produktdetails_nav_right');
		if(pnavl != null)
		pnavl.style.display = 'none';
		if(pnavr != null)
		pnavr.style.display = 'none';
		active_in=null;
	}
	
	if(pcontainer != null){
		var pnavl = document.getElementById('produktdetails_nav_left');
		var pnavr = document.getElementById('produktdetails_nav_right');
		if(pnavl != null){
			pnavl.style.height = pcontainer.offsetHeight - 2 + 'px';
			pnavl.childNodes[0].childNodes[0].style.paddingTop = (pcontainer.offsetHeight / 2 - 8) + 'px';
			pnavl.childNodes[0].childNodes[0].style.paddingBottom = (pcontainer.offsetHeight / 2 - 8) + 'px';
			pnavl.style.display = 'block';
			active_in=1;
		}
		
		if(pnavr != null){
			pnavr.style.height = pcontainer.offsetHeight - 2 + 'px';
			pnavr.childNodes[0].childNodes[0].style.paddingTop = (pcontainer.offsetHeight / 2 - 8) + 'px';
			pnavr.childNodes[0].childNodes[0].style.paddingBottom = (pcontainer.offsetHeight / 2 - 8) + 'px';
			pnavr.style.display = 'block';
			active_in=1;
		}
	}
}

/*////////////////////////////////////////////////////////////////////////////////////////////
//Events
////////////////////////////////////////////////////////////////////////////////////////////*/
function addEvent(obj, type, fn ) {
	if(obj.attachEvent){
		obj[type+fn] = function(){fn.call(obj,window.event);}
		obj.attachEvent( 'on'+type, obj[type+fn] );
	} 
	else
		obj.addEventListener( type, fn, false );
}

function removeEvent( obj, type, fn ) {
	if ( obj.detachEvent ) {
		obj.detachEvent( 'on'+type, obj[type+fn] );
		obj[type+fn] = null;
	} 
	else
	obj.removeEventListener( type, fn, false );
} 

/*////////////////////////////////////////////////////////////////////////////////////////////
//Events
////////////////////////////////////////////////////////////////////////////////////////////*/
addEvent(document, "mousemove", function(event){Position(event);QuickView(event);ItemNavi(event);});
