//theuntitled JavaScript Document

var lastTab   = 'tabs-1';
var headerID  = '';
var oRe       = new RegExp('%tag%', 'ig');
var sTemplate = '<a href="/default.aspx?ID=128&q=%tag%">%tag%</a>';
var alteKategorie = '';
var arr = new Array();

Event.observe(window, 'load', function() {
	$$('div.teaser_tags ul li').each(function(aktuell) {
		var sText = $(aktuell).innerHTML;
		var sHTML = sTemplate.replace(oRe, sText);
		$(aktuell).innerHTML = sHTML;
	});
	
	$$('#RELGRP1_rep a').each(function(element){
		element.href = element.href.replace('ID=129', 'ID=111');
	});
	$$('#RELGRP2_rep a').each(function(element){
		element.href = element.href.replace('ID=129', 'ID=140');
	});
	$$('#RELGRP3_rep a').each(function(element){
		element.href = element.href.replace('ID=140', 'ID=129');
    });

    // Hauptmenü Händlersuche
    hm_haendler_btn()
	
	// Send a Friend
	if ($("send-a-friend-status")) {
		sendAFriend();
	}
	updateTextbox();
	$('Message').observe('keydown', function() {
		updateTextbox();
	});
	$('Message').observe('keyup', function() {
		updateTextbox();
	});
	
	if ($$('.antwort')) {
		$$('.antwort').each(function(s, index) {
			if (!$(s).descendantOf('send-a-friend')) $(s).hide();
		});
	}
	
	if ($$('div.kategoriebox_Januar')) {
		$$('div.kategoriebox_Januar, div.kategoriebox_Februar, div.kategoriebox_Maerz, div.kategoriebox_April, div.kategoriebox_Mai, div.kategoriebox_Juni, div.kategoriebox_Juli, div.kategoriebox_August, div.kategoriebox_September, div.kategoriebox_Oktober, div.kategoriebox_November, div.kategoriebox_Dezember').each(function(element) {
			element.hide();
			$$('td.chkuhrzeit').each(function(element) {
				if (element.innerHTML == '00:00 Uhr') {
					element.innerHTML = '';
					element.previous(0).innerHTML = '';
				}
			});
			
			$$('td.chkmonthname').each(function(element) {
				element.innerHTML = element.innerHTML.gsub("Maerz", "März")
			});
		});
		var date = new Date();
		switch (date.getMonth()) {
			case 0:
				var aktMonth = 'Januar';
				break;
			case 1:
				var aktMonth = 'Februar';
				break;
			case 2:
				var aktMonth = 'Maerz';
				break;
			case 3:
				var aktMonth = 'April';
				break;
			case 4:
				var aktMonth = 'Mai';
				break;
			case 5:
				var aktMonth = 'Juni';
				break;
			case 6:
				var aktMonth = 'Juli';
				break;
			case 7:
				var aktMonth = 'August';
				break;
			case 8:
				var aktMonth = 'September';
				break;
			case 9:
				var aktMonth = 'Oktober';
				break;
			case 10:
				var aktMonth = 'November';
				break;
			case 11:
				var aktMonth = 'Dezember';
				break;
		}
		terminSwitchIn(aktMonth);
	}
	
	new FormValues();
	getFaqAnchor();
});

function getFaqAnchor() {
	hash = window.location.hash;
	if (hash) {
		if (hash.substr(1)) {
			element = hash.substr(1).replace('faq_', '');
			openCloseFaq(element);
		}
	}
}

function changeTab(element) {
	if ($(element)) {
		if (lastTab != element) {
			$(lastTab).hide();
			$(element).show();
			$('li_' + lastTab).className = '';
			$('li_' + element).className = 'aktiv';
			if (element == 'RELGRP1' || element == 'RELGRP2') {
				if ($(element + '_box')) {
					$(element + '_box').show();
				}
			}
			if (lastTab == 'RELGRP1' || lastTab == 'RELGRP2') {
				if ($(lastTab + '_box')) {
					$(lastTab + '_box').hide();
				}
			}
			if (element == 'tabs-4') {
				$('sidebar_container').hide();
			}
			if (lastTab == 'tabs-4') {
				$('sidebar_container').show();
			}
			lastTab = element;
		}
	}
	return false;
}

function colorMyTables(where) {
	if ($(where)) {
		$(where).css("background-color", "#e5e8b2");
	}
}

function zoomImage(imagepath, destination) {
	$(destination).src = imagepath;
}

function zoomTitledImage(imagepath, destination, imageTitle, titleDestination) {
	$(destination).src = imagepath;
	var title = $(titleDestination);
	if (title)
		title.innerHTML = imageTitle;
}

function showCase(dir) {
	elementheight   = $$('div.showcase ul li')[0].getDimensions().height;
	lastElement     = $$('div.showcase ul li').last().getDimensions().height;
	offsetFromTop   = $$('div.showcase ul')[0].positionedOffset().top;
	fullheight      = $$('div.showcase ul')[0].getDimensions().height;
	containerheight = $$('div.showcase')[0].getDimensions().height;
	grenzWertUnten  = (-(fullheight-containerheight) + elementheight);
	bewegungsRadius = -(fullheight-containerheight);
	bewegung        = false;
	if (dir == 'up') {
		if (offsetFromTop > bewegungsRadius) {
			if (offsetFromTop > grenzWertUnten) {
				bewegung = -elementheight;
			} else {
				bewegung = -((offsetFromTop - grenzWertUnten) + lastElement);
			}
		}
	} else if (dir == 'down') {
		if (offsetFromTop < 0) {
			if (offsetFromTop <= -elementheight) {
				bewegung = elementheight;
			} else {
				bewegung = -offsetFromTop;
			}
		}
	}
	if (bewegung) {
		try {
			new Effect.Move('scroller', {
				x: 0,
				y: bewegung,
				mode: 'relative',
				duration: 0.5,
				transition: Effect.Transitions.sinoidal
			});
		} catch (ex) {
			//do nothing :)
		}
	}
}

function openCloseFaq(element) {
	if ($('antwort' + element).style.display == 'none') {
		$('antwort' + element).style.display = 'block';
		$('frage' + element).className = 'pfeil_offen';
	} else {
		$('antwort' + element).style.display = 'none';
		$('frage' + element).className = 'pfeil';
	}
}

function terminSwitch(kategorie) {
	if (!arr.length == 0) {
		arr.clear();
	}
	if ($$('div.kategoriebox_' + alteKategorie) && alteKategorie) {
		count = $$('div.kategoriebox_' + alteKategorie).length;
		if (count) {
			$$('div.kategoriebox_' + alteKategorie).each(function(element) {
				new Effect.Fade(element, {
					duration: 0.3,
					afterFinish: function() {
						terminSwitchcollect(element, arr, count, kategorie);
					}
				});
			});
		} else {
			terminSwitchIn(kategorie);
		}
	} else {
		terminSwitchIn(kategorie);
	}
}

function terminSwitchcollect(element, array, count, kategorie) {
	arr.push(element);
	if (arr.length == count) {
		terminSwitchIn(kategorie);
	}
}

function terminSwitchIn(kategorie) {
	if ($$('div.kategoriebox_' + kategorie)) {
		$$('div.kategoriebox_' + kategorie).each(function(element) {
			new Effect.Appear(element, {
				duration: 0.3
			});
		});
		alteKategorie = kategorie;
	}
}

function createRightBoxes() {
	output = ''
	if (wordpartners.length) {
		output += '<div class="right_ds_box"><div class="weber_world_partner">Weber World Partner</div><div class="content">';
		wordpartners.each(function(element, index) {
			if (index < 2) {
					
				if (!element[6]) {
					element[6] = '&nbsp;';
				} else {
					element[6] = '<a href="' + element[6] + '">zur H&auml;ndlerhomepage</a>';
				}
				if (index == 1) {
					output += '<p>&nbsp;</p>';
				}
				output += '<table width="216" border="0" cellspacing="0" cellpadding="0"><tr><th scope="col" width="160" align="left"><strong><a href="#" onclick="return infoFensterTriggern(\'' + element[10] + '\');">' + element[1] + '</a></strong></th><td width="56">Entfernung:</td></tr><tr><td>' + element[2] + '</td><td>' + element[7] + ' km</td></tr><tr><td>' + element[3] + ' ' + element[4] + '</td><td>&nbsp;</td></tr><tr><td>Telefon: ' + element[5] + '</td><td>&nbsp;</td></tr><tr><td>' + element[6] + '</td><td>&nbsp;</td></tr></table>';
			
			}
		});
		output += '</div></div><p>&nbsp;</p>';
	}
	if (partnerstores.length) {
		output += '<div class="right_ds_box"><div class="weber_partner_store">Weber Partner Store</div><div class="content">';
		partnerstores.each(function(element, index) {
			if (index < 2) {
					
				if (!element[6]) {
					element[6] = '&nbsp;';
				} else {
					element[6] = '<a href="' + element[6] + '">zur H&auml;ndlerhomepage</a>';
				}
				if (index == 1) {
					output += '<p>&nbsp;</p>';
				}
				output += '<table width="216" border="0" cellspacing="0" cellpadding="0"><tr><th scope="col" width="160" align="left"><strong><a href="#" onclick="return infoFensterTriggern(\'' + element[10] + '\');">' + element[1] + '</a></strong></th><td width="56">Entfernung:</td></tr><tr><td>' + element[2] + '</td><td>' + element[7] + ' km</td></tr><tr><td>' + element[3] + ' ' + element[4] + '</td><td>&nbsp;</td></tr><tr><td>Telefon: ' + element[5] + '</td><td>&nbsp;</td></tr><tr><td>' + element[6] + '</td><td>&nbsp;</td></tr></table>';
				
			}
		});
		output += '</div></div><p>&nbsp;</p>';
	}
	if (fachhaendler.length) {
		output += '<div class="right_ds_box"><div class="fachhandels_partner">Fachhandels-Partner</div><div class="content">';
		fachhaendler.each(function(element, index) {
			if (index < 2) {
					
				if (!element[6]) {
					element[6] = '&nbsp;';
				} else {
					element[6] = '<a href="' + element[6] + '">zur H&auml;ndlerhomepage</a>';
				}
				if (index == 1) {
					output += '<p>&nbsp;</p>';
				}
				output += '<table width="216" border="0" cellspacing="0" cellpadding="0"><tr><th scope="col" width="160" align="left"><strong><a href="#" onclick="return infoFensterTriggern(\'' + element[10] + '\');">' + element[1] + '</a></strong></th><td width="56">Entfernung:</td></tr><tr><td>' + element[2] + '</td><td>' + element[7] + ' km</td></tr><tr><td>' + element[3] + ' ' + element[4] + '</td><td>&nbsp;</td></tr><tr><td>Telefon: ' + element[5] + '</td><td>&nbsp;</td></tr><tr><td>' + element[6] + '</td><td>&nbsp;</td></tr></table>';
				
			}
		});
		output += '</div></div>';
	}
	
	$('rightDsBox').innerHTML = output;
}

var FormValues = function() {
	this.init();
}

FormValues.prototype = {
	init: function() {
		obj = this;
		$$('form').each(function(element) {
			element.getInputs('text').each(function(element) {
				obj.observer(element, element.value);
			});
		});
	},
	observer: function(element, value) {
		Event.observe(element, 'focus', function(event) {
			if (value == element.value) {
				element.value = '';
			}
		});
		Event.observe(element, 'blur', function(event) {
			if (element.value == '') {
				element.value = value;
			}
		});
	}
}

function manuipulateSearch(mit, id) {
	/*if ($(id)) {
		$(id).value += mit;
	}*/
	if (!isNaN($(id).value)) {
		$(id).value += ' Schweiz';
	}
	return true;
}

/* Send a Friend */
function sendAFriend() {
	if ($("send-a-friend").getStyle("display") == "none") {
		Effect.BlindDown('send-a-friend', {
			duration: 0.5
		});
	} else {
		Effect.BlindUp('send-a-friend', {
			duration: 0.5
		});
	}
	setTimeout(function() {
		Effect.ScrollTo($("footer_container"));
	}, 600);
}

function newSendAFriend(reopen) {
	Effect.BlindUp("send-a-friend", {
		duration: 0.5,
		queue: {
			position: "end",
			scope: "sendafriend"
		},
		afterFinish: function() {
			$("send-a-friend-success").hide();
			$("send-a-friend-form").show();
		}
	});
	if (reopen) {
		Effect.BlindDown("send-a-friend", {
			duration: 0.5,
			queue: {
				position: "end",
				scope: "sendafriend"
			}
		});
	}
	setTimeout(function() {
		Effect.ScrollTo("footer_container", {
			afterFinish: function() {
				$("Recipient").value = "";
				$("Recipient").focus();
			}
		});
	}, (reopen) ? 1100 : 600);
	
}

function updateTextbox() {
	var maxLength = parseInt($("send-a-friend-maxlength").innerHTML);
	if ($("Message").value.length > maxLength) {
		$("Message").value = $('Message').value.substring(0, maxLength)
	}
	$('send-a-friend-messagelength').update('(' + $('Message').value.length + ' von ' + maxLength + ' Zeichen)');
}

function hm_haendler_btn() {
    var List = $("hauptnavigation_haendlersuche");
    if (!List) return;
    var Link = $("haendlersuche_btn");
    if (!Link) return;
    var ListEl = List.down(0);
    if (!ListEl) return;
    var DropDown = $("haendlersuche_dropdown");
    if (!DropDown) return;

    var HoverMain = false;
    var HoverSub = false;

    Link.observe("mouseover", function () {
        HoverMain = true;
        ListEl.addClassName("hover");
    });
    DropDown.observe("mouseover", function () {
        HoverSub = true;
    });

    Link.observe("mouseout", function () {
        HoverMain = false;
        setTimeout(function () {
            if (!HoverSub && !HoverMain) ListEl.removeClassName("hover");
        }, 100);
    });
    DropDown.observe("mouseout", function () {
        HoverSub = false;
        setTimeout(function () {
            if (!HoverSub && !HoverMain) ListEl.removeClassName("hover");
        }, 100);
    });
}

var OnlineHaendler = function () {
    if ($$("div.partneritem").length > 0) {
        this.initializePartnerItems();
    }

    if ($$(".onlinehaendlerrouting_produktseite").length > 0) {
        this.initializeRoutingProd();
    }

    if ($$(".onlinehaendlerrouting_uebersichtsseite").length > 0) {
        this.initializeRoutingGrp();
    }
}

OnlineHaendler.prototype = {
    initializePartnerItems: function () {
        $$("div.partneritem").each(function (element) {
            element.observe("click", this.track.bindAsEventListener(this));
        }, this);
        $$("div.worldpartneritem").each(function (element) {
            element.observe("click", this.track.bindAsEventListener(this));
        }, this);
    },
    initializeRoutingProd: function () {
        $$(".onlinehaendlerrouting_produktseite .haendler").each(function (element) {
            element.observe("click", function (event) {
                var parts = element.down("span").innerHTML.split("|");

                this.fireGaEvent(element, 'onlinehaendler:produkt');

                if (!parts[0] || !parts[1]) return;

                this.generatePixel(parts[0], parts[1]);
            } .bind(this));
        }, this);
    },
    initializeRoutingGrp: function () {
        $$(".onlinehaendlerrouting_uebersichtsseite .haendler").each(function (element) {
            element.observe("click", function (event) {
                var parts = element.down("span").innerHTML.split("|");

                this.fireGaEvent(element, 'onlinehaendler:produktgruppe');

                if (!parts[0] || !parts[1]) return;

                this.generatePixel(parts[0], parts[1]);
            } .bind(this));
        }, this);
    },
    fireGaEvent: function (element, eventname) {
        if ($("awinfo_isvisitor")) {
            //safeTarget = element.href.toString().replace("http://", "");
            //safeTarget = escape(safeTarget).gsub("/", "%2F");
            safeTarget = element.title;
            pageTracker._trackEvent("aw-out", element.href, awkeyword, 2);
        }
        pageTracker._trackEvent("Onlinehaendler", eventname, element.title);
    },
    generatePixel: function (id, label) {
        iapixel('4323231313236323131303', 'le', '0', { custom1: label });
    },
    track: function (event) {
        var target = $(event.target);

        if (!target.href) {
            if (!target.up(0).href) return;
        }
        if (target.up("div.onlinehaender")) {
            var element = (target.hasClassName("worldpartneritem")) ? target : target.up("div.worldpartneritem");
            var Parameter = element.down("span.infos").innerHTML.split("|");
        } else {
            var element = (target.hasClassName("partneritem")) ? target : target.up("div.partneritem");
            var Parameter = element.down("span.partnerparams").innerHTML.split("|");
        }

        this.fireGaEvent(element.down('.gaevent'), 'onlinehaendler:seite');

        if (!Parameter[0] || !Parameter[1]) return;
        this.generatePixel(Parameter[0], Parameter[1]);
    }
}

function iapixel(cId, cvType, cvValue, opts) {
    if (cId && cvType && cvValue) {
        var merge = function (d, s) { for (var p in s) { if (d.hasOwnProperty(p)) { d[p] = s[p]; } } return d; };
        var options = merge({
            currency: 'EUR',  // conversion currency code(ACCORDING ISO 4271 i.e. 'EUR' or 'USD') [optional
            orderId: '', // orderId [optional]
            age: '', // user age [optional]
            dob: '', // user birth date (format: YYYYMMDD)
            gender: '', // user gender (possible values: m or w)
            zip: '', // user zip code (5 digit zip code, i.e. 12345)
            city: '', // user city (i.e. Berlin)
            region: '', // user region (i.e. Bayern)
            country: '', // user country (possible values: 2 char coutry code, i.e. de) [optional]
            custom1: '', // custom value 1 [optional]
            custom2: '', // custom value 2 [optional]
            custom3: '', // custom value 3 [optional]
            custom4: ''  // custom value 4 [optional]
        }, opts || {});
        var varMap = {
            currency: 'vv', orderId: 'po', age: 'sh', dob: 'nf', gender: 'hr', zip: 'uo', city: 'vo', region: 'tr',
            country: 'vp', custom1: 'c1', custom2: 'c2', custom3: 'c3', custom4: 'c4'
        };
        var proto = (document.location.toString().indexOf('https://') != -1) ? 'https' : 'http';
        var uri = 't23.intelliad.de/tc.php';
        var url = '?cl=' + encodeURIComponent(cId) + '&v=' + encodeURIComponent(cvValue) + '&vz=' + encodeURIComponent(cvType) + '&';
        for (var v in varMap) { if (varMap.hasOwnProperty(v)) { url += varMap[v] + '=' + encodeURIComponent(options[v]) + '&'; } }
        var imgsrc = proto + '://' + uri + url + '&rand=' + Math.floor(Math.random() * 11111139435231);
        var img = new Image(); img.setAttribute('width', 1); img.setAttribute('height', 1); img.src = imgsrc;
        document.getElementsByTagName('body')[0].appendChild(img);
    }
    return true;
}
