window.onerror = Fehlerbehandlung;

/**
 *  jQuery.observeHashChange (Version: 1.0)
 *
 *  http://finnlabs.github.com/jquery.observehashchange/
 *
 *  Copyright (c) 2009, Gregor Schmidt, Finn GmbH
 *
 *  Permission is hereby granted, free of charge, to any person obtaining a
 *  copy of this software and associated documentation files (the "Software"),
 *  to deal in the Software without restriction, including without limitation
 *  the rights to use, copy, modify, merge, publish, distribute, sublicense,
 *  and/or sell copies of the Software, and to permit persons to whom the
 *  Software is furnished to do so, subject to the following conditions:
 *
 *  The above copyright notice and this permission notice shall be included in
 *  all copies or substantial portions of the Software.
 *
 **/
eval(function(p,a,c,k,e,d){e=function(c){return(c<a?'':e(parseInt(c/a)))+((c=c%a)>35?String.fromCharCode(c+29):c.toString(36))};if(!''.replace(/^/,String)){while(c--){d[e(c)]=k[c]||e(c)}k=[function(e){return d[e]}];e=function(){return'\\w+'};c=1};while(c--){if(k[c]){p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c])}}return p}('(2($){$.i.h=2(i){$(a).w("d.h",i);s x};$.f=2(j){4 5=$.y({},$.f.r,j);9(m()){l()}v{t(5)}};4 1=c;4 b=c;4 3=0;$.f.r={3:D};2 m(){s E a.k!==\'C\'}2 l(){1=7.6.8;a.k=n}2 n(e,B){4 g=1;1=7.6.8;$(a).q("d.h",{u:g,o:1})}2 t(5){9(1==c){1=7.6.8}9(b!=c){z(b)}9(3!=5.3){b=A(p,5.3);3=5.3}}2 p(){9(1!=7.6.8){4 g=1;1=7.6.8;$(a).q("d.h",{u:g,o:1})}}$.f()})(d);',41,41,'|locationHash|function|interval|var|opts|location|document|hash|if|window|functionStore|null|jQuery||observeHashChange|oldHash|hashchange|fn|options|onhashchange|nativeVersion|isHashChangeEventSupported|onhashchangeHandler|after|checkLocationHash|trigger|defaults|return|setIntervalVersion|before|else|bind|this|extend|clearInterval|setInterval|data|undefined|500|typeof'.split('|'),0,{}))



function Fehlerbehandlung (Nachricht, Datei, Zeile) {
	if (getQuerystring('TEMPORARY_TEMPLATE') == "75") {
		
		show_advice(Nachricht);
	}
	return true;
}

var xpos;
var ypos;
var timeoutId;

function track(e){
    if(!e) e = window.event;
    var body = (window.document.compatMode && window.document.compatMode == "CSS1Compat") ?
    window.document.documentElement : window.document.body || null;

    var mouse = {};

    // Position im Dokument
    mouse.top = e.pageY ? e.pageY : e.clientY + body.scrollTop;
    mouse.left = e.pageX ? e.pageX : e.clientX + body.scrollLeft;
    xpos = mouse.left;
    ypos = mouse.top;
}

document.onmousemove = track;

function show_advice(qs_error){
	var d = document.getElementById('advice');
	var q = qs_error.search(/window.quickscan_basket.PopScan.+/);
	window.clearTimeout(timeoutId);
	if ((q != -1) || UserName == "anonymous" ) {
		if (mvLangId  == "11") {
			d.innerHTML = '<div class=\"advice_head_error\">Indication</div><div class=\"advice_text_error\">Veuillez vous connecter pour commander.</div>';
		} else {
			d.innerHTML = '<div class=\"advice_head_error\">Hinweis</div><div class=\"advice_text_error\">Für Bestellungen müssen Sie sich einloggen.</div>';
		}
	} else {
		//if (getQuerystring('V_LANG_ID' ) == "11") {
		if (mvLangId == "11") {
			d.innerHTML = '<div class=\"advice_text\">L\'article a été ajouté.</div>';
		} else {
			d.innerHTML = '<div class=\"advice_text\">Der Artikel wurde erfolgreich hinzugefügt.</div>';
		}
	}
	
	d.style.left = xpos + 20 + 'px';
	d.style.top = ypos + 'px';
	d.style.display = 'block';
	timeoutId = window.setTimeout("hide_advice()", 2000);
}

function hide_advice() {
	document.getElementById('advice').style.display = 'none';
}

function getQuerystring(key, default_)
{
  if (default_==null) default_="";
  key = key.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regex = new RegExp("[\\?&]"+key+"=([^&#]*)");
  var qs = regex.exec(window.location.href);
  if(qs == null)
    return default_;
  else
    return qs[1];
} 






function JSrot13(text) {text = text.replace(/%/, "@");var rot13text_rotated = ""; /* the function will return this string */;for (i = 1 ; i < (text.length + 1); i++) {k = text.charCodeAt(i-1);if (k >= 97 && k <= 109) {k = k + 13;} else if (k >= 110 && k <= 122) {k = k - 13;} else if (k >= 65 && k <= 77) {k = k + 13;} else if (k >= 78 && k <= 90) {k = k - 13;}rot13text_rotated = rot13text_rotated + String.fromCharCode(k);}return rot13text_rotated;}function Securemail(maillink){var maillink_output;maillink_output = JSrot13(maillink);location.href= 'mailto:' + maillink_output;}function displaymailaddress(linktext){var linktext_output;linktext_output = JSrot13(linktext);linktext_output = linktext_output.replace(/@/, "<span style='display:none'> *secure E-Mailaddress* <\/span>@");document.write(linktext_output);}

function init(){
	if (document.getElementById("prod_title")){
		document.getElementById("title").innerHTML = document.getElementById("prod_title").innerHTML + "<img src='/0.gif' width='11' height='30' alt='' class='color_spacer'>";
	}
	stripeTables();
	fCheckWidthOfTable();
}


$(document).ready(function() {

tooltip();
setDimensions();

$('<img src="/paulforrer/media/template/neu_art.png" class="neuheitenlink png"><span>&nbsp;&nbsp;&nbsp;&nbsp;</span>').prependTo("#sub_navi_section_3689");


$(".thumb_link").click(function(){
	setDimensions();
})


$(window).hashchange(function(){
	//if (document.location.hash == '#login-changed'){ 

	$(document).append('<script src="http://quickscan.paul-forrer.ch/index.php/qs-in-a-box/userinfo?callback=foobar"></script>);');

	//}        
});

$("#quickscan_basket").bind('change',function(){
	//if (document.location.hash == '#login-changed'){ 
	$(document).append('<script src="http://quickscan.paul-forrer.ch/index.php/qs-in-a-box/userinfo?callback=foobar"></script>);');
	//}        
});


});


//Linkscripts für Produkte

var mvGallery =false

function fGoLink(lvUrl){
	if (mvGallery == false){
		document.location.href=lvUrl;
	}
	else{
		mvGallery = false;
	}

}

function stripeTables() {
	var tables = document.getElementsByTagName("table");
	var addClassName = " row_dark";
	for (var i=0; i<tables.length; i++) {
		if (tables[i].className.search(/edit_Farbige_Tabelle/)!=-1) {
			var rows = tables[i].getElementsByTagName("tr");
			for (var j=0; j<rows.length; j=j+1) {
		if (rows[j].className.search(/nocolor/)==-1) {
					rows[j].onmouseover=function(){
						this.className += " ruled";return false
					}
					rows[j].onmouseout=function(){
						this.className = this.className.replace("ruled", ""); return false
					}
					rows[j].className = rows[j].className + addClassName;
					if (addClassName == " row_dark") {addClassName = ""} else {addClassName = " row_dark"}
				} else {
					rows[j].className = "transp";
				}
			}
		}
	}


//färben von Boxen IE 6
	var content = document.getElementById("content");
	var boxdiv = content.getElementsByTagName("div");
	var addClassName = " row_dark";
	for (var i=0; i<boxdiv.length; i++) {
		if (boxdiv[i].className.search(/box\b/)!=-1) {
					boxdiv[i].onmouseover=function(){
						this.className += " ruledbox";return false
					}
					boxdiv[i].onmouseout=function(){
						this.className = this.className.replace("ruledbox", ""); return false
					}
				}
			}







}

function fCheckWidthOfTable(){
	if (document.getElementById('infotable')){
	   	var contentwidth = document.getElementById('content_padding').offsetWidth;
	   	var tablewidth = document.getElementById('infotable').offsetWidth;

		if (contentwidth > tablewidth){
			document.getElementById('show_info').style.visibility = "hidden";
		}
	}
}



function setDimensions(){
    var maxWidth = 200; // Max width for the image
    var maxHeight = 180;    // Max height for the image
    var ratio = 0;  // Used for aspect ratio

    $("#zoom1 .big_image:first").css("height","auto");   // Set new height
    $("#zoom1 .big_image:first").css("width","auto");    // Scale width based on ratio


    var width = $("#zoom1 .big_image:first").width();    // Current image width
    var height = $("#zoom1 .big_image:first").height();  // Current image height
    // Check if the current width is larger than the max
    if(width > maxWidth){
        ratio = maxWidth / width;   // get ratio for scaling image
        $("#zoom1 .big_image:first").css("width", maxWidth); // Set new width
        //$("#zoom1 .big_image:first").css("height", height * ratio);  // Scale height based on ratio
        height = height * ratio;    // Reset height to match scaled image
    }

    // Check if current height is larger than max
    if(height > maxHeight){
        ratio = maxHeight / height; // get ratio for scaling image
        $("#zoom1 .big_image:first").css("height", maxHeight);   // Set new height
        //$("#zoom1 .big_image:first").css("width", width * ratio);    // Scale width based on ratio
        width = width * ratio;    // Reset width to match scaled image
    }

}

$(document).ready(function() {
	$("#prd_anfrage_link").click(function() {
	 $("#prd_anfrage").slideDown('slow', function() {
    // Animation complete.
  });

	});
    $(".nyroModalHaendler").nyroModal({width:830,height:600});
	
	var traderlink = $("#trader_search .nyroModalHaendler:first").attr("href");

	$("#trader_search #search_field").keyup(function(event){
	  if(event.keyCode == 13){
		$(".nyroModalHaendler:first").attr('href',traderlink + '&PLZ_HAENDLER=' + $('#search_field').val()).click();
	  }
	});


	

});

//Quickscan Anbindung

var BasketApi = null;
var UserInfos = ""
var UserName = ""

function htmlDecode(value){ 
	return $('<div/>').html(value).text(); 
}

function htmlEncode(value){ 
	return $('<div/>').text(value).html(); 
}


function foobar(user){
	var arLen=user.advcrit.length;
	for ( var i=0, len=arLen; i<len; ++i ){
		UserInfos = UserInfos + user.advcrit[i] + ",";		
	}
	UserInfos = user.loginname + "|" +UserInfos;
	UserName = user.loginname;
	UserInfos = escape(UserInfos);
	var lvUrl = 'http://www.paul-forrer.ch/default.asp?V_ITEM_ID=3389&V_LANG_ID='+ mvLangId  +'&AktUser='+ UserInfos;

	$.get(lvUrl, function(data) {
		$('#aktions_links').html(htmlDecode(data));
	});
	
}

function setApi(a){
	BasketApi = a.getApi('quickscan_basket');
}

//Tooltip Funktion für Neue Produkte
this.tooltip = function(){	
		xOffset = -15;
		yOffset = 10;		
	$("a.tooltip").hover(function(e){											  
		this.t = this.title;
		this.title = "";									  
		$("body").append("<div id='tooltip'>"+ this.t +"</div>");
		$("#tooltip")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px")
			.fadeIn("fast");		
    },
	function(){
		if (this.t != undefined){
			this.title = this.t;		
		}
		$("#tooltip").remove();
    });	
		
};


