// --- Teaser Boxes (start) ---

function hideTeaserBox(el) {
    $(el).parent().animate({
        marginTop: '175px'
    }, 1000);
    $(el).find('img.toggle').attr('src', IMG_TOGGLE);
}
function showTeaserBox(el) {
    $(el).parent().animate({
        marginTop: '0px'
    }, 1000);
    $(el).find('img.toggle').attr('src', IMG_TOGGLED);
}

// --- Teaser Boxes (products) ---

/**
 * Hides a single product teaser box
 *
 * @param el element div.headline
 */
function hideProdTeaserBox(el) {
    $(el).parent().animate({
        height: '35px'
    }, 1000);
    $(el).find('img.toggle').attr('src', IMG_TOGGLE);
}
/**
 * Shows a single product teaser box
 *
 * @param el object div.headline element
 */
/*
function showProdTeaserBox(el) {
    // unbind event handler and rebind in the animations callback handler
    // to avoid event flooding
//     $('div.prodBox div.headline').unbind('mouseover');
    $(el).parent().animate({
        height: '210px'
    }, 1000, 'linear', function() {
//          $('div.prodBox div.headline').bind('mouseover', mouseoverProdBox);
    });
    $(el).find('img.toggle').attr('src', IMG_TOGGLED);
}
*/
function showProdTeaserBox(el) {
    // unbind event handler and rebind in the animations callback handler
    // to avoid event flooding
    $('div.prodBox div.headline').unbind('mouseover');
    $(el).parent().animate({
        height: '210px'
    }, 1000, 'linear', function() {
        $('div.prodBox div.headline').bind('mouseover', mouseoverProdBox);
    });
    $(el).find('img.toggle').attr('src', IMG_TOGGLED);
}

/**
 * Hides all open product teaser boxes except the one with the passed id
 * (supposed to be the open one)
 *
 * @param curId string product box id (e.g. "prod-1")
 */
function hideOtherProdTeaserBoxes(curId) {
    $('div.prodBox').each(function(i) {
        if($(this).attr('id') != curId) {
           hideProdTeaserBox($(this).find('div.headline')[0]);
        }
    });
}
function mouseoverProdBox() {
    if($(this).find('img.toggle').attr('src') == IMG_TOGGLE) {
        showProdTeaserBox(this);
        hideOtherProdTeaserBoxes($(this).parent().attr('id'));
    }
}
function mouseoutProdBox() {
    $('div.prodBox').each(function(i) {
        hideProdTeaserBox($(this).find('div.headline')[0]);
    });
}

var hiddenField;
var numberBoxLayer;
var activeBoxLayer;

$(document).ready(function(){

	$("#search").attr({ autocomplete:"off" });
	
	$(".dragImg").css("z-index","999");
	$(".dragImg").draggable({
		appendTo:'#hiddenLayer',
		zIndex:1000,
		revert:true
	});

$(".quadrat").droppable({
	accept: ".dragImg",
	//accept: "#gz_radio,#gz_auto,#gz_einkauf,#gz_flakon,#gz_kamm,#gz_tee,#gz_kalender,#gz_birne,#gz_lupe,#gz_computer,#gz_fueller,#gz_geschenk",
	over: function(ev, ui) {
		$(this).css("background","#009DE0");
	},
	out: function(ev, ui) {
		$(this).css("background","#fff");
		if($(ui.draggable).attr("id") == $(this).children("img").attr("id") ) {
			hiddenField = $(this).children("input");
		}
	},
		drop: function(ev, ui) {
			if(!$(this).children("img").attr("id")) {
				$(ui.draggable).remove().appendTo($(this));
				$(ui.draggable).css("position","absolute");
				$(ui.draggable).css("top","0px");
				$(ui.draggable).css("left","0px");
				$(ui.draggable).css("z-index","999");
				$(ui.draggable).draggable({
					appendTo: '#hiddenLayer',
					zIndex:1000,
					revert:true
					});
				$(hiddenField).val("");
				$(this).children("input").val($(ui.draggable).attr("src"));
			}
	}
});	


	/*
	$("#gz_radio").draggable({appendTo: 'body',zIndex:1500,revert:true});
	$("#gz_auto").draggable({appendTo: 'body',zIndex:1500,revert:true});
	$("#gz_einkauf").draggable({appendTo: 'body',zIndex:1500,revert:true});
	$("#gz_flakon").draggable({appendTo: 'body',zIndex:1500,revert:true});
	$("#gz_kamm").draggable({appendTo: 'body',zIndex:1500,revert:true});
	$("#gz_tee").draggable({appendTo: 'body',zIndex:1500,revert:true});
	$("#gz_kalender").draggable({appendTo: 'body',zIndex:1500,revert:true});
	$("#gz_birne").draggable({appendTo: 'body',zIndex:1500,revert:true});
	$("#gz_lupe").draggable({appendTo: 'body',zIndex:1500,revert:true});
	$("#gz_computer").draggable({appendTo: 'body',zIndex:1500,revert:true});
	$("#gz_fueller").draggable({appendTo: 'body',zIndex:1500,revert:true});
	$("#gz_geschenk").draggable({appendTo: 'body',zIndex:1500,revert:true});
	*/

	
	// ----- Teaserboxes -----

    /*IMG_TOGGLE  = '/media/layout/img/boxen/toggle.gif';
    IMG_TOGGLED = '/media/layout/img/boxen/toggled.gif';*/
    IMG_TOGGLE  = '/media/layout/img/boxen/toggle_210.gif';
    IMG_TOGGLED = '/media/layout/img/boxen/toggled_210.gif';
	
	
	// Boxen auf der Startseite werden nacheinander eingeblendet -
	// Reihenfolge:
	// 1. rechte Boxen
	// - rechts oben
	// - rechts unten
	// 2. Slider von rechts nach links
	alleBoxen = $('#rechteBox1').children();
	objekt1 = $(alleBoxen[0]);
	setTimeout("objekt1.slideDown('slow')", 1000);
	objekt2 = $(alleBoxen[1]);
	setTimeout("objekt2.slideDown('slow')", 2000);
	setTimeout("$('body#start .boxLayer .box').slideDown('slow')", 7000);

    setTimeout("$('body#start div#teaserBox-3').slideDown('fast')", 3000);
    setTimeout("$('body#start div#teaserBox-2').slideDown('fast')", 4000);
    setTimeout("$('body#start div#teaserBox-1').slideDown('fast')", 5000);
	

	// wenn im Homeobjekt der Boxwechsel aktiviert ist
	if( $('#rechteBox2').length ) {
		activeBoxLayer = 1;
		if( $('#rechteBox2').length ) {
			numberBoxLayer = 2;
		}
		if( $('#rechteBox3').length ) {			
			numberBoxLayer = 3;
		}
		setTimeout("boxLayerRotation()", 10000);
	}

    // --- Start / 1stLvlNav
	// Slidereffekt auf der Startseite deaktiviert - um wieder zu aktivieren: if/else-Bedingung rausnehmen -- fz-09-12-16
	// if( $('#start div.teaserBox div.headline').length == 0 ) {
		$('div.teaserBox div.headline').mouseover(function() {
	        if($(this).find('img.toggle').attr('src') == IMG_TOGGLE) {
	            if($(this).parent().attr('id') == 'teaserBox-1') {
	                showTeaserBox(this);
	                hideTeaserBox($('div#teaserBox-2').find('div.headline'));
	                hideTeaserBox($('div#teaserBox-3').find('div.headline'));
	            } else if($(this).parent().attr('id') == 'teaserBox-2') {
	                showTeaserBox(this);
	                hideTeaserBox($('div#teaserBox-1').find('div.headline'));
	                hideTeaserBox($('div#teaserBox-3').find('div.headline'));
	            } else if($(this).parent().attr('id') == 'teaserBox-3') {
	                showTeaserBox(this);
	                hideTeaserBox($('div#teaserBox-1').find('div.headline'));
	                hideTeaserBox($('div#teaserBox-2').find('div.headline'));
	            }
	        }
		});
	/* }
	else {
		showTeaserBox($('div#teaserBox-1'));
		showTeaserBox($('div#teaserBox-2'));
		showTeaserBox($('div#teaserBox-3'));
		$('div#teaserBox-1').removeClass("teaserBoxToggled");
		$('div#teaserBox-2').removeClass("teaserBoxToggled");
		$('div#teaserBox-3').removeClass("teaserBoxToggled");
	} */
	
    // --- Products

    $('div.prodBox div.headline').mouseover(mouseoverProdBox);
    // hide open box on mouseout
//     $('div.prodBox div.body').mouseout(mouseoutProdBox);

	// ----- Toolbox -----

    // Increase/decrease font size
    var init = true;
    $('img#incFontsize').click(function() {
        // ie em bug: reports wrong initial font-size for content
        // 9px instead of 12px, add the difference on page init
        var curFontSize = $('div#content').css('font-size');
        if($.browser.msie && init) {
            curFontSize = (parseInt(curFontSize) + 3) + 'px';
            init = false;
        }
        var curFontSizeNum = parseInt(curFontSize, 10);
        var newFontSize = curFontSizeNum + 1;
        $('div#content').css('font-size', newFontSize);
        return false;
    });
    $('img#decFontsize').click(function() {
        var curFontSize = $('div#content').css('font-size');
        var curFontSizeNum = parseInt(curFontSize, 10);
        var newFontSize = curFontSizeNum - 1;
        $('div#content').css('font-size', newFontSize);
        return false;
    });
    // Print
    /*
    $('img#print').click(function() {
        window.print();
        return false;
    });
    */

	// ----- Search -----

    // Clear search text
    $('input.searchText').click(function() {
        if($(this).attr('value') == 'Suchbegriff eingeben') {
            $(this).attr('value', '');
        }
    });
    // Search suggest
    $('#search').keyup(function() {
        var searchText = encodeURIComponent($('input.searchText').attr('value'));
        //var searchText = $('input.searchText').attr('value');
        if( searchText.length > 1 ) {
            $.get('/de/meta/suche/ajaxsuche.html', { q: searchText, js: 1 }, function(data) {
                $('div#searchSuggest').html(data);
                //$('div#searchSuggest').text(data);
            });
            if($('div#searchSuggest').css('display') == 'none') {
                $('div#searchSuggest').css('display', 'block');
            }
        }
    });
    $('body').click(function() {
        if($('div#searchSuggest').css('display') == 'block') {
            $('div#searchSuggest').css('display', 'none');
        }
    });

	// ----- Misc -----

	// Recommend product
    $('a.prod-recommend').click(function() {
        $('div#prod-recommend-container').toggle();
        if($('div#prod-recommend-container').css('display') == 'block') {
            $('div#prod-recommend-container input').removeAttr('disabled');
            $('div#prod-recommend-container textarea').removeAttr('disabled');
        }
		
		transLayer = document.createElement('div');
		var width = document.body.clientWidth;
		var height = document.body.clientHeight;

		leftPosTransLayer = (992 - document.body.clientWidth)/2;

		// inline css fuer IE:
		$(transLayer).css("left",leftPosTransLayer+"px");
		$(transLayer).css("width",width);
		$(transLayer).css("height",height);
		$(transLayer).css("background-color","#000");
		$(transLayer).css("-moz-opacity","0.3");
		$(transLayer).css("opacity",".3");
		$(transLayer).css("filter","alpha(opacity=30)");
		$(transLayer).css("display","none");
		$(transLayer).addClass("transLayer");
		$("#contentLayer").append($(transLayer));		
		$(transLayer).css("display","block");
		
        return false;
    });
	
    $('input#prod-recommend-close').click(function() {
        $('div#prod-recommend-container').hide();
        $('form#prod-recommend').clearForm();
        $('div#prod-recommend-container p#prod-recommend-output').removeClass('prod-recommend-error');
        $('div#prod-recommend-container p#prod-recommend-output').html('Bitte f&uuml;llen Sie das Formular vollst&auml;ndig aus.');
		$(transLayer).css("display","none");
		$(transLayer).remove();
        return false;
    });
	
    $('input#prod-recommend-close-z').click(function() {
        $('div#prod-recommend-container').hide();
        $('form#prod-recommend').clearForm();
        $('div#prod-recommend-container p#prod-recommend-output').removeClass('prod-recommend-error');
        $('div#prod-recommend-container p#prod-recommend-output').html('Bitte f&uuml;llen Sie das Formular vollst&auml;ndig aus.');
		$(transLayer).css("display","none");
		$(transLayer).remove();
		$('#theForm').css("display","block");
		$('#theCloseForm').css("display","none");
        return false;
    });

    // Product recommend form
    $('form#prod-recommend').ajaxForm({
        target:       'div#prod-recommend-container p#prod-recommend-output', // target element(s) to be updated with server response
        beforeSubmit: validate, // pre-submit callback
        success:      showSuccess, // post-submit callback
        clearForm:    true
    });
    function validate(formData, jqForm, options) {
        var form = jqForm[0];
        if(!form['rec-firstname-sender'].value || !form['rec-lastname-sender'].value || !form['rec-email-sender'].value ||
           !form['rec-firstname-recipient'].value || !form['rec-lastname-recipient'].value || !form['rec-email-recipient'].value
        ) {
            $('div#prod-recommend-container p#prod-recommend-output').addClass('prod-recommend-error');
            return false;
        }
    }
    function showSuccess(responseText, statusText)  {
		$('#theForm').css("display","none");
		$('#theCloseForm').css("display","block");
        $('div#prod-recommend-container p#prod-recommend-output').removeClass('prod-recommend-error');
        $('div#theForm input').attr('disabled', true);
        $('div#theForm textarea').attr('disabled', true);
        $('div#theForm input#prod-recommend-close').removeAttr('disabled');		
    }

    // Contact form
    /*$('input#contactCancel').click({
        //$('input#contactCancel input').attr('value', '');
        //return false;
    });*/
});



// Boxwechsel rechte Boxen Startseite
function boxLayerRotation() {
	$('#rechteBox'+this.activeBoxLayer).css("display","none");
	if( this.activeBoxLayer ==  this.numberBoxLayer ) {
		this.activeBoxLayer = 1;
	}
	else {
		this.activeBoxLayer = this.activeBoxLayer+1;
	}
	$('#rechteBox'+activeBoxLayer).fadeIn("slow");
	setTimeout("boxLayerRotation()", 5000);
}


function addEvents(){
	var dasElement = document.getElementById("mainnav").childNodes;
	for (i = 0; i < dasElement.length; i++){
		if(dasElement[i].tagName == "UL"){
			dieLiKinder = dasElement[i].childNodes;
			for (j = 0; j < dieLiKinder.length; j++){
				if(dieLiKinder[j].tagName == "LI"){
					dieUlEnkel = dieLiKinder[j].childNodes;
					$(dieLiKinder).mouseover( function() { zeige(this,1,1); } );
					$(dieLiKinder).mouseout( function() { zeige(this,0,1); } );
					for (k = 0; k < dieUlEnkel.length; k++){
						if(dieUlEnkel[k].tagName == "UL"){
							dieLiEnkel = dieUlEnkel[k].childNodes;
							for (l = 0; l < dieLiEnkel.length; l++){
								if(dieLiEnkel[l].tagName == "LI"){
									$(dieLiEnkel).mouseover( function() { zeige(this,1,2); } );
									$(dieLiEnkel).mouseout( function() { zeige(this,0,2); } );
								}
							}
						}
					}
				}
			}
		}
	}
}

/*
function addEvents(){
	$("#mainnav ul li").mouseover( function() { zeige(this,1,1); } );
	$("#mainnav ul li").mouseout( function() { zeige(this,0,1); } );
	$("#mainnav ul li ul li").mouseover( function() { zeige(this,1,2); } );
	$("#mainnav ul li ul li").mouseout( function() { zeige(this,0,2); } );
}
*/

function toggleLayer(objekt) {
	divToggleLayer = $(objekt).parent("h2").parent("div").parent("div");
	divChild = $(objekt).parent("h2").parent("div").parent("div").children("div.toggleBody");
	if(! divToggleLayer.hasClass("noexpand")) {
		divChild.slideToggle("slow",function(){
			divToggleLayer.addClass("noexpand");
			if(divToggleLayer.parent("div").attr("id") == "contentMain" ) {
				divToggleLayer.animate({marginBottom:10},"fast");
			}
		});		
	}
	else {
		if(divToggleLayer.parent("div").attr("id") == "contentMain" ) {
			divToggleLayer.animate({marginBottom:0},"fast");
		}
		divChild.slideToggle("slow",function(){
			divToggleLayer.removeClass("noexpand");
		});
	}	
}

var timer;
var ulObjekt;

function zeige(objekt,aktion,ebene,mausaktion) {
/*
	if( ebene == 2 && aktion == 1 ) {
		$(objekt).children("a").addClass("overSecond");
	}
	if( ebene == 2 && aktion == 0 ) {
		$(objekt).children("a").removeClass("overSecond");
	}
*/
	if( ebene == 1 && aktion == 1 ) {
		$(objekt).children("a").addClass("overFirst");
	}
	if( ebene == 1 && aktion == 0 ) {
		$(objekt).children("a").removeClass("overFirst");
	}
	ulObjekt = objekt;
	if(timer) {
		clearTimeout(timer);
	}
	if(aktion == 1){
		$(objekt).children("ul").css("display","block");
	}

	if( aktion == 0 ){
		if($(objekt).children('ul'))$(objekt).children('ul').css('display','none');
		//timer = setTimeout("verstecke(ulObjekt);", 100);
	}
}

function verstecke(objekt) {
	//$(objekt).children('ul').css('display','none');
	setTimeout("$(objekt).children('ul').css('display','none';", 150);
}

function showLayer(aktion,layer) {
	if( $("#"+layer).css("display") == "block" && (aktion == 1) ) {
		document.getElementById(layer).style.display = "none";
	}
	else if(aktion==1){
		document.getElementById(layer).style.display = "block";
	}
	else if(aktion==0){
		document.getElementById(layer).style.display = "none";
	}
}

function showOverlayLayer(action) {
    arrayPageDimesion = getPageSize();
	arrayScrollDimesion = getPageScroll();
	if (action == 1) {
		$("body").prepend("<div class=\"popup_overlay\" id=\"layerPopBG\"></div>");
		$(".popup_overlay").css({
			height	: 	arrayPageDimesion[1],
			opacity	:	0.3
		});
		$(".popup_overlay").fadeIn(300, function(){
			//showLayer(action,layer);
		});
	}
	if (action == 0) {
		$(".popup_overlay").fadeOut("slow", function(){
	        $(this).remove();    
	    });
	}		
}

/**
 * getPageSize() by quirksmode.com
 *
 * @return Array Return an array with page width, height and window width, height
 */
function getPageSize() {
	var xScroll, yScroll;
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		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
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			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 = xScroll;		
	} else {
		pageWidth = windowWidth;
	}
	
	// pageWidth = document.body.clientWidth;
	// pageHeight = document.body.clientHeight;

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
	return arrayPageSize;
}

/**
 * getPageScroll() by quirksmode.com
 *
 * @return Array Return an array with x,y page scroll values.
 */
function getPageScroll() {
	var xScroll, yScroll;
	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
		xScroll = self.pageXOffset;
	} else if (document.documentElement && document.documentElement.scrollTop) {	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
		xScroll = document.documentElement.scrollLeft;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
		xScroll = document.body.scrollLeft;	
	}
	arrayPageScroll = new Array(xScroll,yScroll);
	return arrayPageScroll;
}




function testStart(layer,nexteSeite,anzeigeDauer) {
	document.getElementById(layer).style.display = "block";
	document.getElementById(layer).style.visibility = "visible";
	document.getElementById("testStart").style.display = "none";
	document.getElementById("testStart").style.visibility = "hidden";
	//alert(anzeigeDauer*1000);
	window.setTimeout( "window.location.href = '?seite="+nexteSeite+"'" , (anzeigeDauer*1000) );
}

function arrange(action) {
	if(action=='parallel') {
		document.getElementById("layerNummer").style.position = "absolute";
		document.getElementById("layerGegenstand").style.position = "absolute";
		document.getElementById("layerNummer").style.top = "-10px";
		document.getElementById("layerGegenstand").style.top = "-10px";
		document.getElementById("layerNummer").style.left = "-202px";
		document.getElementById("layerGegenstand").style.left = "202px";
		document.getElementById("layerNummer").style.padding = "10px 5px 10px 10px";
		document.getElementById("layerGegenstand").style.padding = "10px 5px 10px 10px";
		document.getElementById("linkNebeneinander").style.display = "none";
		document.getElementById("linkUntereinander").style.display = "inline";
	}
	if(action=='among') {
		document.getElementById("layerNummer").style.position = "relative";
		document.getElementById("layerGegenstand").style.position = "relative";
		document.getElementById("layerNummer").style.top = "0px";
		document.getElementById("layerGegenstand").style.top = "0px";
		document.getElementById("layerNummer").style.left = "0px";
		document.getElementById("layerGegenstand").style.left = "0px";
		document.getElementById("layerNummer").style.padding = "0px 0px 0px 0px";
		document.getElementById("layerGegenstand").style.padding = "0px 0px 0px 0px";
		document.getElementById("linkNebeneinander").style.display = "inline";
		document.getElementById("linkUntereinander").style.display = "none";		
	}
}


$(function() {
    $('#zoomPic a').lightBox();
    $('#zoomPicIcon a').lightBox();	
    $('.layerKampTn a').lightBox();

});


