// handle Cookies
function createCookie(name,value,days,domain) {
	if(!days) days = cookie_ablauf;
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = ""; // if days == 0
	if (!domain && typeof(cookie_domain) != "undefined") domain = cookie_domain;
	if (domain) {
		document.cookie = name+"="+value+expires+"; domain="+cookie_domain+"; path=/";
	} else {
		document.cookie = name+"="+value+expires+"; path=/";
	}
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return decodeURIComponent(c.substring(nameEQ.length,c.length));
	}
	return '';
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}
// End handle Cookies

function change_product(selectbox) {
	if (selectbox.value != '') {
		for (var i=0; i<selectbox.length; i++) {
			if (selectbox.options[i].selected == true) window.location = selectbox.options[i].title;
		}
	}
}

function decryptThis(s) {
	var n=0;
	var r="";
	for(var i=0;i<s.length;i++) { 
		n=s.charCodeAt(i); 
		if (n>=8364) {n = 128;}
		r += String.fromCharCode(n-(2)); 
	}
	return r;
}

function decrypt_mail(element, email, params) {
	if (!params) var params = '';
	element.href = 'mailto:' + encodeURI(decryptThis(email)) + params;
}

function toggleLWKat(id) {
	if (document.getElementById('lw' + id).style.display == 'block') {
		document.getElementById('lw' + id).style.display = 'none';			
	} else {
		document.getElementById('lw' + id).style.display = 'block';
	}
}

function toggleSubform() {
	if (document.getElementById('subform').style.display == 'block') {
		document.getElementById('subform').style.display = 'none';			
	} else {
		document.getElementById('subform').style.display = 'block';
	}
}

/* Neues Fenster für Großbildanzeige */
var coverzoom = null;
function cz(img,titel,w,h) {
	var url = rpfad + "katalog/coverzoom.cfm?image=" + img + "&titel=" + titel;
	if (w && h) {
		url =  url + "&w=" + w + "&h=" + h;
		coverzoom = window.open(url,"Bigimage","width="+w+",height="+h+",top=0,left=0");
	} else {
		coverzoom = window.open(url,"Bigimage","width=150,height=30,top=0,left=0");
	}
	coverzoom.focus();
}

/* Popup Fenster für Flashclips oder sonstige Teaser und Links */
var lkgW_popup = null;
function openlkgWin(url,w,h,name,params) {
	if (!name) var name = "lkgpopupwin";
	if (!params) var params = "";
	lkgW_popup = window.open(url,name,"width="+w+",height="+h + params);
	lkgW_popup.focus();
}

function getPosX(obj) {
	var ret=0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			ret += obj.offsetLeft;
			obj = obj.offsetParent;
		}
	} else if (obj.x) {
		ret += obj.x;
	}
	return ret;
}
function getPosY(obj) {
	var ret=0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			ret += obj.offsetTop;
			obj = obj.offsetParent;
		}
	} else if (obj.y) {
		ret += obj.y;
	}
	return ret;
}

// Scroll to Anker (geht nur im Firefox, nicht mit Position:absolute)
function scroll2(anker,h) {
	if (!h) var h = -144;
	var obj = document.getElementsByName(anker)[0];
	var offset = getPosY(obj) + h;
	window.scrollTo(0,offset);
	return false;
}

// setActiveStyleSheet (alternate Stylesheet)
function setActiveStyleSheet(title) {
	var i, a, main;
	for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
		if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
			a.disabled = true;
			if(a.getAttribute("title") == title) a.disabled = false;
		}
	}
}
function getActiveStyleSheet() {
	var i, a;
	for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
		if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) return a.getAttribute("title");
	}
	return null;
}

// open Details
var ActiveISBNDetails = null;
function openISBN(id,detail) {
	var myObj = document.getElementById(id);
	var myDetail = document.getElementById(id + '_' + detail);
	if (myObj.style.display == 'none') {
		myObj.style.display='block';
		myObj.innerHTML = myDetail.innerHTML;
		ActiveISBNDetails = detail;
	} else {
		if (ActiveISBNDetails != detail) {
			myObj.innerHTML = myDetail.innerHTML;
			ActiveISBNDetails = detail;
		} else {
			myObj.style.display='none';
			ActiveISBNDetails = null;
		}
	}
}

// smooth div rollout
var smooth_timer;
function smoothHeight(id, curH, targetH, stepH, mode) {
	diff = targetH - curH;
	if (diff != 0) {
		newH = (diff > 0) ? curH + stepH : curH - stepH;
		document.getElementById(id).style.height = newH + "px";
		if (smooth_timer) window.clearTimeout(smooth_timer);
		smooth_timer = window.setTimeout( "smoothHeight('" + id + "'," + newH + "," + targetH + "," + stepH + ",'" + mode + "')", 6 );
	}
	if (!mode) document.getElementById(id).style.display='none';
}

// Pop-Up-Window für Mozilla bei Bug in Flash-Embeds
// Verwendung: onclick="return moz_pop(url, name, width, height);"
function moz_pop(url, name, width, height) {
	if (navigator.appName.indexOf('Internet Explorer') == -1) {
		window.open(url, name, 'width=' + width + ', height=' + height + ', resizable=no, status=no');
		return false;
	}
	return true;
}

function show_overlaybox1(boxdiv,w,h) {
	if (!boxdiv) {
		alert("Kein DIV Element zum einbinden angegeben");
	} else {
		if (!w) w = "400";
		if (!h) h = "400";
		if (!document.getElementById("overlaybox1")) {
			var overlay = document.createElement("div");
			overlay.setAttribute('id','overlaybox1');
			overlay.style.display = 'block';
			overlay.style.position = 'absolute';
			alert(rpfad);
			if (navigator.appName.indexOf("MSIE") && navigator.appVersion.substring(0,1) < 7) {
				overlay.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + rpfad + "_images/sprachkalender/teaser/overlay.png', sizingMethod='scale');";
			} else {
				overlay.style.backgroundImage = "url('../_images/sprachkalender/teaser/overlay.png')";
			}
			overlay.style.top = '0';
			overlay.style.left = '0';
			overlay.style.zIndex = '250';
		 	overlay.style.width = '100%';
			overlay.style.height = '100%';
			overlay.innerHTML = '<div id="overlaybox1_inside" style="position:absolute; left:50%; top:50%; margin-left:-' + w / 2 + '; margin-top:-' + h / 2 + 'px; width:' + w + 'px; height:' + h + 'px; background-color:white;">';
			overlay.innerHTML = overlay.innerHTML + document.getElementById(boxdiv).innerHTML
			overlay.innerHTML = overlay.innerHTML + '</div>';
			document.getElementsByTagName("body").item(0).insertBefore(overlay,document.getElementsByTagName("body").item(0).firstChild);	
		} else {
			document.getElementById("overlaybox1").style.display="block";
		}
	}
}

function close_overlaybox1() {
	document.getElementById("overlaybox1").style.display="none";
}

function AjaxObject(url) {                                           // This is the object constructor
	var that=this;                                                    // A workaround for some javascript idiosyncrocies
	var updating = false;                                             // Set to true if this object is already working on a request
	this.callback = function() {}                                     // A post-processing call -- a stub you overwrite.

	this.update = function(passData, method) {                        // Initiates the server call.
		if (!method) { method = "POST"; }
		if (updating==true) { return false; }                          // Abort if we're already processing a call.
//		updating=true;                                                 // Set the updating flag.
		var AJAX = null;                                               // Initialize the AJAX variable.
		try {
			AJAX = new XMLHttpRequest();
		} catch (trymicrosoft) {
			try {
				AJAX = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (othermicrosoft) {
				try {
					AJAX = new ActiveXObject("Microsoft.XMLHTTP");
				} catch (failed) {
					AJAX = null;
				}
			}
		}
		if (AJAX==null) {                                              // If we couldn't initialize Ajax...
			alert("Ihre Browser unterstützt kein AJAX.");               // Sorry msg.                                              
			return false                                                // Return false (WARNING - SAME AS ALREADY PROCESSING!)
		} else {
			AJAX.onreadystatechange = function() {                      // When the browser has the request info..
				if (AJAX.readyState==4 || AJAX.readyState=="complete") { //   see if the complete flag is set.
					if (AJAX.status == 200) {
						that.data = AJAX.responseText.replace(/^\s*([\S ]+)\s*$/,'$1'); //   It is, so put the new data in the object's layer
						that.responseText = AJAX.responseText;
						that.responseXML = AJAX.responseXML;
						delete AJAX;                                       //   delete the AJAX object since it's done.
						updating=false;                                    //   Set the updating flag to false so we can do a new request
						that.callback();                                   //   Call the post-processing function.
					} else {
						// AJAX response error
						alert("Fehler in AJAX Antwort.");
					}
				}                                                        // End Ajax readystate check.
			}                                                           // End create post-process fucntion block.
			var timestamp = new Date();                                 // Get a new date (this will make the url unique)
			if (method == 'POST') {
				AJAX.open(method, urlCall, true);                        // Open the url this object was set-up with.
				AJAX.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
				AJAX.setRequestHeader('Content-length', passData.length);
				AJAX.send(passData);
			} else {
				var uri=urlCall+'?'+passData+'&timestamp='+(timestamp*1); // Append date to url (so the browser doesn't cache the call)
				AJAX.open(method, uri, true);                             // Open the url this object was set-up with.
				AJAX.send(null);                                          // Send the request.
			}
			return true;                                                // Everything went a-ok.
		}                                                              // End Ajax setup aok if/else block                 
	}
	
	// This area set up on constructor calls.
	var urlCall = url;                                                // Remember the url associated with this object.
}                                                                    // End AjaxObject

$(document).ready(function() {
        $("#hideme").hide();
        $("#hideme2").hide();

	$(".ctip").each(function(){
		var mytip = $(this);
		setTimeout(function(){
			mytip.cluetip({
				showTitle: true,
				sticky: true,
				delayedClose: 0,
				dropShadow: false,
				mouseOutClose: true,
				closePosition: 'title',
				closeText: 'X',
				local: false,
				hideLocal: true,
				arrows: true,
				positionBy: 'fixed',
				leftOffset: -350,
				topOffset: 7,
				hoverIntent: {
					sensitivity: 3,
					interval:	300,
					timeout: 500
				},
				fx: {             
					open:       'slideDown', // can be 'show' or 'slideDown' or 'fadeIn'
					openSpeed:  ''
				},
		/*		onShow: function(ct,c) {
					ct.animate({ left: '-=5px' }, 250, 'swing', function() {});
				}, */
				cluetipClass: 'jtip'
			});
		}, 100);
	});
	
	$(".order_ajax").click(function(){
		var orderLink = $(this).attr("rel");
		tb_show(null,orderLink+"?width=300&height=250&modal=true",null);
		return false;
	});
	
	if (readCookie("LKG_WAKO_SUM") && readCookie("LKG_WAKO_SUM") > 0) {
		$.ajax({
			type: "GET",
			cache: false,
			url: "/warenkorb/overview_ajax.cfm",
			success: function(html){
				$("#wakoArtikelString").html(html);
				$("#crossSelling_Wako").show();
			}
		});
	}
	
})

