document.documentElement.className = 'js'; 

$(document).ready(function(){
	//Search Preload Resultcount
	jQuery('a.resultcounter').each(function(){

		var t = jQuery(this);
		var url = t.attr('rel');
		t.children('span.counter').load(url, function(){
			jQuery(this).children('img.loader').remove();
			//For external service:
			var ext = jQuery(this).children('ca_item_count');
			if(ext.length > 0) {
				var val = '('+jQuery(this).text().replace(/[^0-9]/gi,'')+')';
				ext.remove();
				jQuery(this).text(val);
			}
		});
	});
	
	//Set font size
	fontResizer('1em','1.1em','1.2em');
	
	//Set staging slider
	jQuery.fn.Horinaja = jQuery.fn.Horinaja || function(a){};
	$('#demo').Horinaja({capture:'demo',delai:0,duree:8,pagination:true});

	checkForShopCookie();

	//Tabs
	$('div.tab-content>div').hide();
	$('div.tab-content>div:first').show();
	// $('ul.tab-headline li:first').addClass('active');

	/* @Deprecated: Switching tabs with JavaScript is not needed anymore because page will be reloaded.
	 * @Date: MARI 14.04.2011 
	$('ul.tab-headline li a').click(function(){ 
		$('ul.tab-headline li').removeClass('active');
		$(this).parent().addClass('active'); 
		var currentTab = $(this).attr('href'); 
		$('div.tab-content>div').hide();
		$(currentTab).show();
		return false;
	});
	*/
	
	//External Link leave notice
	$('a[target=_blank]').leaveNotice();

	//Equal div height
	$('div.sitemap > div').syncHeight();
	
	//Form branch vals
	function branchVals() {
    var branchArea = $("select#branch-area").val();
		if(branchArea==0){
			$("div#branch-location input").val("- - -");
			$("div#branch-email input").val("vertrieb@thgeyer.de");
			$("div#branch-plz").show();
		}
		if(branchArea==1){
			$("div#branch-location input").val("Niederlassung Höxter");
			$("div#branch-email input").val("rohstoffe@thgeyer.de");
			$("div#branch-plz").hide();
		}  			  		
    }
    $("select#branch-area").change(branchVals);
    branchVals();

	// Tooltip
	$("form.search-field input[alt]").qtip({
	   position: {
		  corner: {
			 tooltip: 'topLeft', // Use the corner...
			 target: 'rightBottom' // ...and opposite corner
		  }
	   },
	   style: {
		  border: {
			 width: 1,
			 radius: 3
		  },
		  padding: 3, 
		  textAlign: 'center',
		  tip: true, // Give it a speech bubble tip with automatic corner detection
		  name: 'light' // Style it according to the preset 'cream' style
	   }
	});
});	

// Language Selector
function changeCountry(cSel) {
	
  if( cSel != null ) {
    var selVal = cSel.options[cSel.selectedIndex].value;
    var host = 'http://home.thgeyer';
    // var host = 'http://geyer.webdevel.mm-bit.com';
    // var oldCookie = $.cookie("dmcgeoip_country");

    switch (selVal) {
      case "0":
        // alert("http://www.thgeyer.de/");
	// $.cookie("dmcgeoip_country", "DE", { path: '/', expires: 365 });
        var str=host + '.de/?cchange='+'DE';
        //var str=host + '/?cchange='+'DE';
	window.location=str;
        break;
      case "1":
        // alert("http://www.thgeyer.com/");
	// $.cookie("dmcgeoip_country", "EN", { path: '/', expires: 365 });
	var str=host + '.com/?cchange='+'EN';
        //var str=host + '/?cchange='+'EN';
	window.location=str;
        break;
      case "2":
        // alert("http://www.thgeyer.dk/");
	// $.cookie("dmcgeoip_country", "DK", { path: '/', expires: 365 });
	var str=host + '.dk/?cchange='+'DK';
	window.location=str;
        break;
    }
  }
}



/* ------------------------------------------- */
/*             SHOP ANMELDUNG                  */
/* ------------------------------------------- */
var bites;
if(document.cookie){
 bites = document.cookie.split("; ");
} else {
 bites = new Array();
}
var cookieName = "thgeyerShop";
var today = new Date();
var expiry = new Date(today.getTime() + 365 * 24 * 60 * 60 * 1000); // plus 365 days
var expired = new Date(today.getTime() - 28 * 24 * 60 * 60 * 1000); // less 28 days
 
function getCookie(name) {
 for (i=0; i < bites.length; i++) {
  nextbite = bites[i].split("="); // break into name and value
  if (nextbite[0] == name){
   return unescape(nextbite[1]);
  }
 }
 return null;
}

function setCookie(name, value) {
 if (value != null && value != ""){
  document.cookie=name + "=" + escape(value) + ";path=/; expires=" + expiry.toGMTString();
  bites = document.cookie.split("; "); // update cookie bites
 }
}

function deleteCookie(name) { // use: deleteCookie("name");
 document.cookie=name + "=null; expires=" + expired.toGMTString(); // delete cookie
 bites = new Array();
}

function checkForShopCookie()
{
  if(document.shoplogin != null && navigator.cookieEnabled) {
    if(getCookie(cookieName) == null) {
      return;
    }
    cookieValueArray=getCookie(cookieName).split("!");
    if(!isNaN(cookieValueArray.length)) {
        document.shoplogin.kunde.value=cookieValueArray[0];
        document.shoplogin.login.value=cookieValueArray[1];
        document.shoplogin.storedata.checked=true;
        document.shoplogin.passwort.focus();
    }
  }
}

function setShopCookie() {
  if(document.shoplogin.kunde.value != "KUNDENNUMMER" && document.shoplogin.kunde.value != "") {
    if(document.shoplogin.storedata.checked) {
      // COOKIE SPEICHERN
      kundennummer=document.shoplogin.kunde.value;
      if(document.shoplogin.login.value != "BENUTZERNAME" && document.shoplogin.login.value != "") {
        benutzername=document.shoplogin.login.value; 
      } else {
        document.shoplogin.login.value="";
        // benutzername="BENUTZERNAME";
        benutzername="";
      }
      cookieValue=kundennummer+"!"+benutzername;
      setCookie(cookieName,cookieValue);
    } else {
      // COOKIE LÖSCHEN
      if (document.cookie) {
        deleteCookie(cookieName);
        if(document.shoplogin.login.value == "BENUTZERNAME") {
          document.shoplogin.login.value="";
        }
      }
    }
  } else {
    // alert("Geben Sie bitte Ihre Kundennummer ein");
    return false;
  }
  return true;
}


