silentJS    = true;
debugJS		= false;
dataLog		= "";

switch (location.hash) {
	case '#debugJS': 	debugJS = true; silentJS = true;  break;
	case '#debugAlert': debugJS = true; silentJS = false; break;
}

//--[ UTIL:PROTOTYPES ]
Array.prototype.containsX       = function(search,strict)   { for(var i=0;i<this.length;i++) if((strict&&this[i]===search)||(!strict&&this[i].toLowerCase().indexOf(search.toLowerCase())!=-1)) return i; return null; }
String.prototype.startsWith     = function(prefix)    		{ return(this.substring(0, prefix.length)==prefix); }
String.prototype.endsWith       = function(suffix)    		{ return(this.substring(this.length-suffix.length)==suffix); }
String.prototype.trim           = function()    			{ return this.replace(/^\s+|\s+$/g,""); }
String.prototype.inArray        = function(match,delimiter,strict) { if(!strict)strict=false; if(typeof match=="string") match=match.split(((!delimiter)?",":delimiter));return match.containsX(this.trim(),strict); }
function typeobj(obj) 	{ return ((obj==null)?"null":(typeof(obj._object)!="undefined")?obj._object:((value = obj.constructor.toString().trim()).startsWith("function"))?value.substring(9,value.indexOf("(")):(typeof(obj)!="undefined")?typeof(obj):"undefined").trim().toLowerCase(); }
function getobject(id)  { return (typeof(id)=="object")?id:(typeof(id)!="string")?null:(document.getElementById)?document.getElementById(id):(document.all)?document.all[id]:(document.layers)?document.layers[id]:null; }

//--[ UTIL:EVENTS ]
function eventAdd(obj, eventType, func) { if (eventType.startsWith("on")) eventType=eventType.substring(2); if (obj.addEventListener) obj.addEventListener(eventType, func, false); else if (obj.attachEvent) obj.attachEvent("on"+eventType, func); }
function eventCancel(event)             { if (!event) event=window.event; if (event.preventDefault) event.preventDefault(); return event.returnValue = false; }
function eventObj(event)                { if (!event) event=window.event; return (event.srcElement)?event.srcElement:(event.target)?event.target:null; }

Init = new function() {
    var funcsArr    = Array();
    this.isDomReady = false;
    this.add        = function() { if (this.isDomReady) this.run.apply(this, arguments); else funcsArr.push(arguments); }
    this.run        = function() { var params=new Array(); for (var i=1; i<arguments.length; i++) params.push(arguments[i]); arguments[0].apply(this, params); }
    this.start      = function() { if (!Init.isDomReady) { this.isDomReady=true; while (funcsArr.length) { this.run.apply(this, funcsArr[0]); funcsArr.splice(0,1); } } }
    eventAdd(document, "readystatechange", function() { if (document.readyState=='complete') Init.start(); } );
    eventAdd(document, "DOMContentLoaded", function() { Init.start(); }, false);
    eventAdd(window,   "load",             function() { Init.start(); }, false);
}

var MM_contentVersion = 8;
var plugin = (navigator.mimeTypes && navigator.mimeTypes["application/x-shockwave-flash"]) ? navigator.mimeTypes["application/x-shockwave-flash"].enabledPlugin : 0;
if ( plugin ) {
		var words = navigator.plugins["Shockwave Flash"].description.split(" ");
	    for (var i = 0; i < words.length; ++i)
	    {
		if (isNaN(parseInt(words[i])))
		continue;
		var MM_PluginVersion = words[i]; 
	    }
	var MM_FlashCanPlay = MM_PluginVersion >= MM_contentVersion;
}
else if (navigator.userAgent && navigator.userAgent.indexOf("MSIE")>=0 
   && (navigator.appVersion.indexOf("Win") != -1)) {
	document.write('<SCR' + 'IPT LANGUAGE=VBScript\> \n'); //FS hide this from IE4.5 Mac by splitting the tag
	document.write('on error resume next \n');
	document.write('MM_FlashCanPlay = ( IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash." & MM_contentVersion)))\n');
	document.write('</SCR' + 'IPT\> \n');
}

function displayFlash(which, color, width, height, param, alternative, id){
	logJS('\n=[ STARTING "displayFlash"]=');

	document.open();
	fl="";

	// split name/path to check which file was actually requested
	fileName = which;
	filePath = fileName.substring(0,fileName.lastIndexOf("/"));
	fileName = fileName.substring(fileName.lastIndexOf("/")+1);

	logJS('--\nValues are:\nwhich: '+which+'\ncolor: '+color+'\nwidth: '+width+'\nheight'+height+'\nparam: '+param+'\nalternative: '+alternative+'\n\nfileName: '+fileName+'\nfilePath: '+filePath+'\n--\n');

	// Try another method as they keep changing names what not really surprisingly leads to errors
	if (("teaser".inArray(which,"/",false)!=null || "homepage".inArray(which,"/",false)!=null) && fileName.endsWith(".jpg")) { //if (fileName.startsWith("teaser") && fileName.endsWith(".jpg")) {
		logJS('TEASER manually forced to No-Flash image:\n'+which);
		
		fl = '<img src="'+which+'" '+((!color)?'':'style="background-color:'+color+'"')+' border="0" width="'+((!width)?616:width)+'" height="'+((!height)?215:height)+'" alt="" />'
	}
	else {
		if (fileName=='number') {
			logJS('Pre-processing PRICE-FLASH parameters: '+param);

			var priceNum = null;
			var digitNum = null;
			var priceUrl = null;
			var paramArr = param.split("&");
			//nr=1.39&number_digits=4&link="
			for (var i=0; i<paramArr.length;i++) {
				var tmp = paramArr[i].substring(paramArr[i].indexOf("=")+1);

				if (paramArr[i].startsWith("nr=")) 		           priceNum = tmp;
				else if (paramArr[i].startsWith("number_digits=")) digitNum = tmp;
				else if (paramArr[i].startsWith("link=")) 		   priceUrl = tmp;
			}
			if (priceNum==null) { priceNum = "0.00"; paramArr[paramArr.length] = "nr="+priceNum; }
			if (digitNum==null) { digitNum = "6";	 paramArr[paramArr.length] = "number_digits="+digitNum; }
			if (priceUrl==null) { priceUrl = "";	 paramArr[paramArr.length] = "link="+priceUrl; }
			param = paramArr.join("&");

			logJS('Pre-processing PRICE-FLASH returned: '+param);
		}
		logJS('Check for CALCULATOR RESIZE: '+param);
   	 	if ("calculator".inArray(which,"/",false)!=null && "paint".inArray(which,"/",false)!=null && "doorpaint".inArray(which,"/",false)==null) {
   	 		logJS('Resizing PAINT CALCULATOR!');
   	 		height = 845;
   	 	}
   	 	else if ("calculator".inArray(which,"/",false)!=null && "wallpaper".inArray(which,"/",false)) {
   	 		logJS('Resizing WALLPAPER CALCULATOR!');
   	 		height = 845;
   	 	}
   	 	else if ("calculator".inArray(which,"/",false)!=null && "walltile".inArray(which,"/",false)) {
   	 		logJS('Resizing WALLTILE CALCULATOR!');
   	 		height = 845;
   	 	}
		if (MM_FlashCanPlay) {
		  logJS('\n=[ DISPLAYING FLASH ]=');

		  color = ""; // strip background colors or we will loose the ability to place other objects above flash
		  fl=fl+'<object '+((id)?'id="'+id+'"':'')+' classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="'+width+'" height="'+height+'">';
		  fl=fl+'<param name="movie" value="'+which+'.swf">';
		  fl=fl+'<param name="quality" value="high">';
		  fl=fl+'<param name="allowScriptAccess" value="always" />';
		  fl=fl+'<param name="swLiveConnect" value="true" />';
		  if(color=="") fl=fl+'<param name="wmode" value="transparent" />';
		  if(param!="") fl=fl+'<param name="FlashVars" value="'+param+'">';
		  if(color=="") { mode='wmode="transparent"';} else { mode="";}
		  fl=fl+'<param name="bgcolor" value="'+color+'" />';
		  fl=fl+' <embed '+((id)?'name="'+id+'"':'')+' src="'+which+'.swf" quality="high" '+mode+' pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" swLiveConnect="true" FlashVars="'+param+'" bgcolor="'+color+'" width="'+width+'" height="'+height+'" ></embed>';
		  fl=fl+'</object>';
		//alert(fl);

		  logJS('-Using the following generated source:\n'+(fl.split('>')).join('>\n'));
	   }
	   else {
	   		logJS('\n=[ NO FLASH DETECTED ]=');

	   		// is there alternative data?
	   		if (alternative) {
	   			logJS('-Displaying alternative provided with function call!');

		   		fl = alternative;
		   	}
		   	// no alternative
		   	else {
		   		logJS('-No alternative provided - looking for fallback definition:');

				// Try another method as they keep changing names what not really surprisingly leads to errors in detection
		   		if ("teaser".inArray(which,"/",false)!=null || "homepage".inArray(which,"/",false)!=null) { //if (fileName.startsWith('teaser')) {
		   			logJS('  Using fallback definition for: TEASER');

		   			fl = '<img src="'+which+'_noflash.jpg" border="0" width="'+((!width)?616:width)+'" height="'+((!height)?215:height)+'" alt="" />'; 
		   		}
		   		else if (fileName.startsWith('number')) {
		   			logJS('  Using fallback definition for: PRICE-FLASH');

		   			fl = '<div style="width:'+((!width)?616:width)+'; height:'+((!height)?215:height)+';"><a class="dark_blue_title" '+((priceUrl)?'href="'+priceUrl+'"':'')+'>'+priceNum+'</a></div>';
		   		}
				else if ("calculator".inArray(which,"/",false)!=null || "module".inArray(which,"/",false)) {
					logJS('  Using fallback definition for: calc='+("calculator=".inArray(which,"/",false))+' module='+("module=".inArray(which,"/",false))  );
					
					fl = '<table cellspacing="0" border="0" class="noflashcalctable" width="'+width+'"><tr>';
					fl+= '<td colspan="5"><img src="../../images/praktiker/spacer.gif" width="1" height="80" border="0" class="pic_no_border" /></td></tr>';
					fl+= '<tr><td><img src="../../images/praktiker/spacer.gif" width="20" height="1" border="0" class="pic_no_border" /></td>';
					fl+= '<td align="center"><div id="calculators_noflashmsg_content">In order to view our Interactive Tools and Material Calculators within our Design Centres, you need to ';
					fl+= '<a href="http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash" class="dark_blue_title" >download Adobe Flash Player.</a></div></td>';
					fl+= '<td><img src="../../images/praktiker/spacer.gif" width="80" height="1" border="0" class="pic_no_border" /></td>';
					fl+= '<td style="padding:0px!important" valign="bottom"><img src="../../images/praktiker/woman_noflash.jpg" style="border:0px!important" alt="" /></td><td><img src="../../images/praktiker/spacer.gif" width="20" height="1" border="0" class="pic_no_border" /></td></tr></table>';
		   		}
				else if ("specialinfo".inArray(which,"/",false)!=null || "special".inArray(which,"/",false)) {
					logJS('  Using fallback definition special info page. ');
					
					fl = '<a href="#" onclick="return jumpHomepage();"><img src="'+which+'_noflash.jpg" border="0" width="'+((!width)?656:width)+'" height="'+((!height)?703:height)+'" alt="" /></a>'; 
		   		}
		   		else logJS('  No fallback definition found for: '+fileName.toUpperCase());
		   	}
	   }
    }
   document.writeln(fl);
   document.close();
   logJS('');
}

function banner_DoFSCommand(command, param) {
	var obj = getobject('banner_container');
	if (obj) {
        switch (command.toLowerCase()) {
            case 'bringtofront': obj.style.clip = (param.toLowerCase()!='true')?'rect(auto auto 50px auto)':'rect(auto auto 450px auto)';
                break;
        }
    }
    return true;
}


function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function getWinHeight(){
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		WinHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		WinHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		WinHeight = document.body.clientHeight;
	}
	return WinHeight;
}


function expandCollapse() {
	for (var i=0; i<expandCollapse.arguments.length; i++) {
		var element = document.getElementById(expandCollapse.arguments[i]);
		element.style.display = (element.style.display == "none") ? "block" : "none";
	}
}

brochuresArr = Array();
brochuresBigArr = Array();
BrochuresNavigate = new function() {
    // private member vars
    var showClass    = "show";
    var hideClass    = "";
    var nodeArray    = null;
    var navigateNode = null;
    var displayImg   = null; 
    var loaded       = false;
    var current      = 0;
    // public constructor method
    this.create = function create(showimage, navigation, initial, itemArray, itemBigArray, showbigimagelink) {
        if (itemArray) nodeArray = itemArray;
        if (itemBigArray) bigImageArray = itemBigArray;
        if (showimage) displayImg = document.getElementById(showimage);
        if (showbigimagelink) displayBigImgLink = document.getElementById(showbigimagelink);
        if (displayImg) {
            //nodeArray    = containerObj.getElementsByTagName("div");
            if (nodeArray && (navigateNode = document.getElementById(navigation))) {
                if (nodeArray.length > 1) {
                    var nodeLink = document.createElement("a");
                    nodeLink.setAttribute("href", "javascript:BrochuresNavigate.prev();");
                    nodeLink.innerHTML = "&lt;&lt;";
                    navigateNode.appendChild(nodeLink);
                    for (var i=0; i<nodeArray.length; i++) {
                        var nodeLink = document.createElement("a");
                        nodeLink.setAttribute("href", "javascript:BrochuresNavigate.show("+i+");");
                        nodeLink.innerHTML = (i+1);
                        
                        if (i==19 || i==39) {
                        	var nodeBreak = document.createElement("br");
                        	nodeLink.style.marginRight = '24px';
	                        navigateNode.appendChild(nodeLink);
                        	navigateNode.appendChild(nodeBreak);
                       	}
                       	else navigateNode.appendChild(nodeLink);
                    }
                    var nodeLink = document.createElement("a");
                    nodeLink.setAttribute("href", "javascript:BrochuresNavigate.next();");
                    nodeLink.innerHTML = "&gt;&gt;";
                    navigateNode.appendChild(nodeLink);
                }
            }
        }

        if (!displayImg || !nodeArray || !navigateNode) {
            displayImg   = null;
            nodeArray    = null;
            navigateNode = null;
        }
        else this.show(initial);
    }
    // public methods
    this.show = function show(num) {
        if (displayImg!=null && nodeArray!=null && navigateNode!=null)
            if (num>=0 && num<nodeArray.length) {
                var obj = navigateNode.getElementsByTagName("a");
                if (current!=null && current!=num) {
                    if (obj) obj[current+1].className = hideClass;
                    //nodeArray[current].className = hideClass;
                }
                if (nodeArray.length!=1) {
                if (obj) obj[num+1].className = showClass;
                }
                //nodeArray[num].className = showClass;
                var imgUrl = nodeArray[num].replace(/(\&amp\;)/, "&");
                displayImg.src = imgUrl;
                
                //Link fuer grosses image
                var imgUrlBig = imgUrl;
                if (bigImageArray != null && bigImageArray[num] != null && bigImageArray[num] != '')
                	imgUrlBig = bigImageArray[num].replace(/(\&amp\;)/, "&");
                
                displayBigImgLink.href = imgUrlBig;       
                
                current = num;
                self.focus();
            }
    }
    this.prev = function prev() {
        if (nodeArray!=null && navigateNode!=null) {
            var num = (current>0)?(current-1):0;
            this.show(num);
        }
    }
    this.next = function next() {
        if (nodeArray!=null && navigateNode!=null) {
            var num = (current<nodeArray.length-1)?(current+1):(nodeArray.length-1);
            this.show(num);
        }
    }
}


Array.prototype.inArray = function(search) {
    for (var i = 0; i < this.length; i++) {
        if (this[i]===search) return i;
    }
	return null;
}

/**
 * Object DomHelper
 * Creates helper object and prototypes methods performing DOM functionality.
 * Mirrors the original DOM object properties and methods to DomHelper.DOM maintaining standard accessibility thereby.
 * The object (keep in mind JS object != DOM object) can be assigned by passing a DOM reference or a valid id.
 * However it will always create an object to minimize run-time errors while trying to query undefined properties.
 * Check for DomHelper._defined to test if there really was a DOM reference assigned.
 */
function DomHelper(obj) {
    this._object  = "DomHelper";
    this._defined = false;
    this.DOM      = new Object();
    if (    (typeof obj=="object")
        ||  (typeof obj=="string"
             && (obj = document.getElementById(obj))) ) {
        this.DOM = obj;
        this._defined = true;
    }
}
/**
 * void appendClassName(String className)
 * Append className to the list of CSS class refs assigned to the object.
 */
DomHelper.prototype.appendClassName = function(className) {
    if (this._defined && this.DOM.className.split(" ").inArray(className)==null) {
        this.DOM.className = this.DOM.className.split(" ").concat(className).join(" ");
    }
}
/**
 * boolean hasClassName(String className)
 * Test if a CSS reference named className iss assigne to the object.
 */
DomHelper.prototype.hasClassName = function(className) {
    return (this._defined)?this.DOM.className.split(" ").inArray(className):null;
}
/**
 * void removeClassName(String className)
 * Remove CSS reference className from the object.
 */
DomHelper.prototype.removeClassName = function(className) {
    var pos;
    if (this._defined && (pos = this.DOM.className.split(" ").inArray(className))!=null) {
        var temp = this.DOM.className.split(" ");
        temp.splice(pos, 1);
        this.DOM.className = temp.join(" ");
    }
}
/**
 * void replaceClassName(String className, String newClass)
 * Replace CSS reference className of the object by the new reference newClass.
 * Calling this method with newClass undefined has the same effect as calling removeClassName.
 * In case className is not a known reference this method will act like appendClassName.
 */
DomHelper.prototype.replaceClassName = function(className, newClass) {
    if (!newClass) this.removeClassName(className);
    else if (this._defined) {
        var temp = this.DOM.className.split(" ");
        var pos = this.DOM.className.split(" ").inArray(className);
        if (pos==null) pos = temp.length;
        temp.splice(pos, 1, newClass);
        this.DOM.className = temp.join(" ");
    }
}
/**
 * void setClassName(String className)
 * Overwrite any existing CSS references with className.
 */
DomHelper.prototype.setClassName = function(className) {
    this.DOM.className = className;
}

function toggleCat(obj) {
    if (obj.hasClassName("show")) obj.removeClassName("show");
    else                          obj.appendClassName("show");
    self.focus();
    return false;
}

function logJS(msg) {
  if (debugJS) {
	if (!msg) {
		if (!silentJS) alert("FINISHED!");
		else		   alert(dataLog +'\n\nFINISHED !!!');
		dataLog = "";
	}
	else {
		if (!silentJS) alert(msg);
		dataLog += msg +'\n';
	}
  }
}
