// set the page to the middle of the browser
var currentProductPage = 0;
var totalPages = 0;
$(document).ready(function()
{
	// lightbox
	$('#productDetailImage a[rel^=detailBox]').lightBox();
	$('div.techDocImage a').lightBox();
	// meta transparent
	$('#metaNavBG').css( {opacity:0.87} );
	// allgemein transparent
	$('div.transparentBackground').css( {opacity:0.87} );

	//bg
    setBackgroundSize();
    $(window).resize(function()  { setBackgroundSize(); });

	// ie6 und background.
	if ($.browser.msie == true) {
		if ($.browser.version < 7) {
			$('#background').css( { position:'absolute', top:'0px', left:'0px' });
		}
	}

	// teaser
    var t = $('div.teaser');
	if (t.length > 0) {
		for (i=0; i < t.length; i++) {
			teaser = t[i];
			$(teaser).data('url', $(teaser).find('a').attr('href'));
			$(teaser).mouseover(teaserOver);
			$(teaser).mouseout(teaserOut);
			$(teaser).click(teaserClick);
		}
	}
    //buttons
    var bt = $('div.button, div.buttonWide, div.buttonSpecial');
    $(bt).css( {opacity:0.6} );
    $(bt).mouseover(buttonOver);
    $(bt).mouseout(buttonOut);
	$('div.buttonInactive').css( {opacity:0.6} );
    
    //productnav
    var container = $('#productAnimateContainer div.overItem');
    if (container.length > 0) {
		var i;
		
		for (i=0; i < container.length; i++) {
			var item = container[i];
			// mouse over
			
			$(item).hover(itemOver, itemOut);
			$(item).find('div.overArea').css( {opacity:0.4} );
			$(item).find('div.overAreaActive').css( {opacity:0.4} );

			var link = $(item).find('a').attr('href');
			$(item).data('link', link);
			$(item).click( function () { document.location.href = $(this).data('link') } );
		}
		// arrows, paging
		$('#leftProdArrow').click(pageNavigationLeft);
		$('#rightProdArrow').click(pageNavigationRight);
		i = 0;
		
    	while ($('#productPage'+i).length > 0) {
    	 	i++;
		}
		totalPages = i;
		if (overrideProductPage > 0) {
			pageNavigation(overrideProductPage);
		}
		setPagingInfo();
	}
	var showAll = $('#showList').click(showProductList);

    //product categories
    container = $('#productCategories');
    if (container.length > 0) {
    	$('#productCategories a div.overItem').hover(itemOver, itemOut);
		$('#productCategories a div.overItem div.overArea').css( {opacity:0.4} );
	}

    // category mood image.
    var mood = $('#moodDetailImage');
    if (mood.length > 0) {
    	//$('#productCategories a div.overItem').hover(itemOver, itemOut);
		//$('#moodDetailImage').click(hideMoodImage);
	}
	
    // produkt detail bilder
    var detailIMG = $('#productDetailImage');
    if (detailIMG.length > 0) {
    	detailIMG.data('currentImage', 0);
		var thumbs = $(detailIMG).find('div.smallProductImage');
		var thumb;
		for (var i=0; i<thumbs.length; i++) {
			thumb = thumbs[i];
			$(thumb).data('number', i);
			$(thumb).data('container', detailIMG);
			$(thumb).click(showDetailImage);
		}

	}

	// faq ein aus
	$('div.faqQuestion a').click(showHideFAQ);
	
});

function itemOver() {
	$(this).find('div.overArea').css( {display:"block"} );
}
function itemOut() {
	$(this).find('div.overArea').css( {display:"none"} );
}

function buttonOver() {
	$(this).css( {opacity:1} );
}
function buttonOut() {
	$(this).css( {opacity:0.6} );
}

function teaserOver() {
	$(this).attr('class', 'teaserOver');
}
function teaserOut() {
	$(this).attr('class', 'teaser');
}
function teaserClick() {
	document.location.href = $(this).data('url');
}
function hideMoodImage() {
	//$('#categoryMoodInfo').fadeOut(200);
	$(this).fadeOut(200);
}
function showDetailImage() {
	var current = $(this).data('container').data('currentImage');
	var myNumber = $(this).data('number');
	$('#bigImage'+current).hide(0);
	$('#smallImage'+current).show(0);
	$('#bigImage'+myNumber).show(0);
	$('#smallImage'+myNumber).hide(0); 
	$(this).data('container').data('currentImage', myNumber);	
}


function showProductList() {
	$(this).hide(0);
	$('#allProductList').fadeIn(400);
	directPageNavigation(currentProductPage);
	return true;	
}
function pageNavigationLeft() {
	pageNavigation(currentProductPage - 1);
}
function pageNavigationRight() {
	pageNavigation(currentProductPage + 1);	
}
function pageNavigation(newPage) {
	if (currentProductPage < 0) {
		currentProductPage = 0;	
	}
	var item = $('#productPage'+newPage);  
	if (item.length > 0) {
		currentProductPage = newPage;
		var x = - item.position().left;
		$('#productAnimateContainer').animate( { left:x+"px" }, { queue:false, duration:500 });
		setPagingInfo();
	}
}
function directPageNavigation(newPage) {
	if (currentProductPage < 0) {
		currentProductPage = 0;
	}
	var item = $('#productPage'+newPage);
	if (item.length > 0) {
		currentProductPage = newPage;
		var x = - item.position().left;
		$('#productAnimateContainer').css({ left:x+"px" });
		setPagingInfo();
	}
}
function setPagingInfo() {
	$('#pagingInfo').html((currentProductPage+1) + " / " + totalPages);
	$('#rightProdArrow').fadeIn(250);
	$('#leftProdArrow').fadeIn(250);
	if (totalPages < 2) {
		$('#leftProdArrow').stop(1,1).fadeOut(250);
		$('#rightProdArrow').stop(1,1).fadeOut(250); 
	} else if (currentProductPage == 0) {
		$('#leftProdArrow').stop(1,1).fadeOut(250);
	} else if (currentProductPage >= totalPages - 1) {
		$('#rightProdArrow').stop(1,1).fadeOut(250);
	}
}

/*
function setHomeOpacity()
{
    //console.log($("#news .quickLinks").innerHeight());
    $("#news .rightBoxBG").height($("#news .quickLinks").innerHeight()).css({opacity:0.7});
    $("#quickLinksHome .rightBoxBG").height($("#quickLinksHome .quickLinks").innerHeight()).css({opacity:0.7});
}
*/

var main_image_original_width = 0;
var main_image_original_height = 0;
var pic_width = 0;
var pic_height = 0;


function setBackgroundSize() {
	var winWidth = $(window).width();
	var winHeight = $(window).height();
	var winAspectRatio = winWidth / winHeight;

	var imgWidth = $('#background-image').width();
	var imgHeight = $('#background-image').height();
	var imgAspectRatio = imgWidth / imgHeight;
	
	if (winAspectRatio > imgAspectRatio) {
		var height = (winWidth / imgWidth) * imgHeight;
		var width = winWidth;
	} else {
		// scale pic to fit window height		
		var height = winHeight;	
		var width = (winHeight / imgHeight) * imgWidth;
	}
	
	var left = (winWidth - width) / 2;
	var top = (winHeight - height) / 2;

	$('#background').css({left: left+'px', top:top+'px'});
	$('#background-image').css({height: height, width: width});
}


function getPageSize(){
	var de = document.documentElement;
	var w = window.innerWidth || self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
	var h = window.innerHeight || self.innerHeight || (de&&de.clientHeight) || document.body.clientHeight;
	arrayPageSize = [w,h];
	return arrayPageSize;
}

function resizeHomeImage()
{
	var size = getPageSize();
	$("img#bgImageHome").css("width", (size[0])+"px");
	$("img#bgImageHome").css("height", (size[1]-42)+"px");
}

function showHideFAQ(aElement) {
	var faq = $('div#faqAnswer-'+this.id);
	if ($(faq).css('display') != 'none') {
		$(faq).fadeOut(200);
	} else {
		$(faq).fadeIn(200);
	}

	return false;
}





