var othertitle;
$(document).ready(function() {
	browserHeight = getPageSize()[1];
	if ($(document).height() <  getPageSize()[1]) {
		$("#contentwrapper").height(browserHeight-135)
		$("#footer").css("margin-top",-75);
	}
	if (document.getElementById("slot2")) {
		if (!document.getElementById("slot2").hasChildNodes()) {
			$("#slot2").addClass("empty");
		}else{
			$("#slot2").addClass("full");
		}
	}
	
	// JavaScript enabled or not - give out error message
	$("#jserror").hide();

	var cookie = readCookie("style");
	var title = cookie ? cookie : getPreferredStyleSheet();
	if (title == "larger") othertitle = "smaller";
	else othertitle = "larger";
	setActiveStyleSheet(title);
});

/*prämienrechner functions*/

function prValidateCalculator(strFormId, strTargetId, strAjaxError, detailPrHandle, year, plz, webcode, language) {

	// Check if typed year is older than year now.
	var nowYear = (new Date()).getFullYear();
	if(year <= nowYear) {

		// Firefox 3 bug fix, 18.06.08, CONCORDIA/abm
		// set url and params ourself. don't let jquery.form.js set this values.
		var url = document.forms[strFormId].action + "?year="+year+"&calculatorplz="+plz;
		if (typeof(eval(document.forms[strFormId].choosecity))!='undefined'){
			url += "&choosecity="+document.forms[strFormId].choosecity.value;
		}

		// we use the plugin jquery.form.js
		var options = {
			url:       url,  // Firefox 3 bug fix, 18.06.08, CONCORDIA/abm
			target:    "#" + strTargetId,   // target element(s) to be updated with server response
			timeout:   5000,  // in mS
			error: function(obj,strError,strException) { $("#" + strTargetId + " #ajaxError").html(strAjaxError).slideDown('slow'); }
		};
		$("#" + strFormId).ajaxForm(options);

	} else {
 		// if year is older then open a popup

		// close Calculation on Promotionslot
		prHideCalculation();

		detailPrHandle = detailPrHandle + '?action=1&lang='+language+'&gebJahr=' + year + '&plz=' + plz + '&ort=' + '&webcode=' + webcode;

		//alert(detailPrHandle);
		window.open(detailPrHandle,'','width=584,height=660,scrollbars=yes,resizable=yes');
		return false;
	}
}
function prValidateWebcode(strFormId, strTargetId, strAjaxError, jsScriptHandle, detailPrHandle, strWebcodeId, strAjaxResponseId, language) {
	
	// Firefox 3 bug fix, 18.06.08, CONCORDIA/abm
	// set url and params ourself. don't let jquery.form.js set this values.
	var webcode = document.forms[strFormId].elements[strWebcodeId].value;
	var url = jsScriptHandle + "?webcode="+webcode;

	detailPrHandle = detailPrHandle + '?action=0&webcode='+webcode+'&lang='+language;

	// we use the plugin jquery.form.js
	var options = {
		async: false, // musst be asynchronous cause firefox and chrom's popup blocker
		url: url,  // Firefox 3 bug fix, 18.06.08, CONCORDIA/abm
		timeout: 5000,  // in mS
		success: function(responseData) {
			document.forms[strFormId].elements[strAjaxResponseId].value = responseData;
		},
		error: function(obj,strError,strException) {
			$("#" + strTargetId + " #ajaxError").html(strAjaxError).slideDown('slow');
		}
	};
	$("#" + strFormId).ajaxSubmit(options);
	var ajaxResponse = document.forms[strFormId].elements[strAjaxResponseId].value;
	if (ajaxResponse == 'OK') {
		$("#" + strTargetId + " #ajaxError").html('');
		window.open(detailPrHandle,'','width=584,height=660,scrollbars=yes,resizable=yes');
	}
	else {
		$("#" + strTargetId + " #ajaxError").html(ajaxResponse).slideDown('slow');
	}
	return false;
}
function prToggleCity() {
	$("#choosecity").toggle();
}
function prToggleCalculation() {
	$("#calculationwrapper").toggle();
	$("#praemienrechner .buttons").toggle();
}
function prHideCalculation() {
	$("#calculationwrapper").hide();
	$("#praemienrechner .buttons").hide();
}


var doShowFlyout = false;
function showFlyout(div,text) {
	doShowFlyout = true;
	window.setTimeout(function(){
		showShowFlyout(div,text)
	},200);
}
function showShowFlyout(div,text) {
	if (doShowFlyout) {
		$(".flyout div").hide();
		$(".flyout ." + text).show();
		$("#praemienrechner .flyout").css("top",$(div).offset().top-$("#praemienrechner").offset().top)
		$("#praemienrechner .flyout").show();	
		if($.browser.msie) {
			$("select").each(function(i){
				if ($(this).offset().top > $("#praemienrechner .flyout").offset().top && $(this).offset().left > $("#praemienrechner .flyout").offset().left) {
					$(this).hide();
				};
			});
		}
	}
}
function hideFlyout() {
	doShowFlyout = false;
	window.setTimeout("hideHideFlyout()",200);
}
function hideHideFlyout() {
	if (!doShowFlyout) {
		$("#praemienrechner .flyout").hide();
		if($.browser.msie) {
			$("select").each(function(i){
				if ($(this).offset().top > $("#praemienrechner .flyout").offset().top && $(this).offset().left > $("#praemienrechner .flyout").offset().left) {
					$(this).show();
				};
			});
		}
	}
}

//show franchisenrechner bubble
var doShowBubble = false;
function showBubble(div,text) {
	doShowBubble = true;
	window.setTimeout(function(){
		showShowBubble(div,text)
	},200);
}
function showShowBubble(div,text) {
	if (doShowBubble) {		
		$(".franchisenrechner #descriptionbubble .praemie").hide();
		$(".franchisenrechner #descriptionbubble .mehrkosten").hide();
		$(".franchisenrechner #descriptionbubble .gesamtkosten").hide();
		$(".franchisenrechner #descriptionbubble .einsparungen").hide();
		$(".franchisenrechner #descriptionbubble ." + text).show();
		$(".franchisenrechner #descriptionbubble").css("left",$(div).offset().left-$(".franchisenrechner").offset().left-100);
		$(".franchisenrechner #descriptionbubble").show();
		$(".franchisenrechner #descriptionbubble").css("top",-$(".franchisenrechner #descriptionbubble ." + text).height()-9);
	}
}
function hideBubble() {
	doShowBubble = false;
	window.setTimeout("hideHideBubble()",200);
}
function hideHideBubble() {
	if (!doShowBubble) {
		$(".franchisenrechner #descriptionbubble").hide();
		$(".franchisenrechner #descriptionbubble .praemie").hide();
		$(".franchisenrechner #descriptionbubble .mehrkosten").hide();
		$(".franchisenrechner #descriptionbubble .gesamtkosten").hide();
		$(".franchisenrechner #descriptionbubble .einsparungen").hide();
	}
}


//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
//
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;
}

function trim(s) {
  while (s.substring(0,1) == ' ') {
    s = s.substring(1,s.length);
  }
  while (s.substring(s.length-1,s.length) == ' ') {
    s = s.substring(0,s.length-1);
  }
  return s;
}

/* GS-Suche functions */

$(document).ready(function() {	
	var cookie = readCookie("gsaddress");
	
	if(cookie != null) {
		// Set html code
		var address = cookie;
		/*address += "<div class=\"form\"><div class=\"btnend\"></div><input class=\"submit\" onclick=\"remGsCookie()\" type=\"submit\" value=\"neu w&auml;hlen\"/><br class=\"lineheight10\"/>&nbsp;</div>";*/

		$("#geschaeftsstellensuche .form").html(address);
	}
});

function setGsCookie(address) {
	createCookie('gsaddress', address, '2');
}

function remGsCookie()
{
  	//deletes cookie by setting expiration date before current date
	document.cookie = "gsaddress=; expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/";
   	location.reload();
}
