var	productImgWidth = 760;
var	productMaxImg = 3;
var activeFrame = 'leftFrame';
var contentType = 'index';
var productCategory = 'nothing';
var showNothingNavi = false;
var showEverythingNavi = false;
var isFaded = false;
var showIndexContent = true;
var activeImage = 0;
var preLink = "/product_info.php?info=";

function initPage() {

	$('#nothingTop').mouseenter(function() {
		if($('div.nothingCorrespondingNavi').html() == null) {
			slideNothingNavi();
		}
	}).mouseleave(function() {
		if(!showNothingNavi) {
			$('.nothingNavi').slideUp("fast"); 
		}
  });
	$('#everythingTop').mouseenter(function() { 
		if($('div.everythingCorrespondingNavi').html() == null) {
			slideEverythingNavi();
		}
	}).mouseleave(function() {
		if(!showEverythingNavi) {
			$('.everythingNavi').slideUp("fast"); 
		}
  });

	$('#loading').ajaxSend(function() {
		$('#loadingOverlay').delay(2000).css('opacity', 0.3).show();
		$(this).fadeIn(50);
	});

	$('#loading').ajaxComplete(function() {
		$('#loadingOverlay').hide().css('opacity', 0);
		$(this).fadeOut(50);
	});
	
	$('.rightFrame #everythingTop a:first').click();
	$('.everythingNavi').slideDown("fast"); 
	$('.nothingNavi').slideDown("fast"); 
	
	bg_blank = new Image();
	bg_blank.src = '/templates/blank/images/design/bg_mainframe_blank.jpg';
}

function activateFrame(frame, forceCType) {
	var duration = 800;
	var pSmallWidth = 190;
	var pLargeWidth = 762;
	
	//$('#debug').html('frame '+frame+' activeFrame: '+activeFrame+'<br />');
	
	switch(frame) {
		case 'leftFrame':
			//$('#debug').append('36: ' + frame + ' / '+ contentType + '<br />');
			$('.everythingNavi').slideUp("fast"); 
			$('div.everythingTeaserText').fadeOut(200);
			$('div#everythingContent').fadeOut(200);
			$('div.blankLogo').show();
			//$('div.rightFrameLogo').fadeOut(200);
			$('div.rightFrame').animate({ "width" : pSmallWidth+"px", "left" : pLargeWidth+"px"}, duration);
			$('#levelFrameRight').animate({"left" : (pLargeWidth-5)+"px"}, duration);
			$('div.generalFrame').animate({ "width" : pSmallWidth+"px", "left" : (pSmallWidth+pLargeWidth)+"px"}, duration);
			$('.generalFrame .frameNavi a').each(function() { $(this).removeClass('activeLink') });
			$('#levelFrameGeneral').animate({"left" : (pSmallWidth+pLargeWidth-5)+"px"}, duration);
			//$('.everythingTeaser').show();
			hideEverythingNavi();
			clearContent('everything');
			$('#generalContentBox').fadeOut();

			if(contentType == 'product' && showNothingNavi) {
				slideNothingNavi();
				//$('.nothingNavi:not(:animated)').slideDown("fast");
			}
			
			//alert($('div#nothingContent').html());
			$('div#nothingContent').delay(500).fadeIn(duration);
			//$('#nothingContent').css({"width":"0px"});
			//$('#nothingContent').animate({"width" : (pLargeWidth - pSmallWidth)+"px"}, duration);
			$('div.leftFrameLogo').delay(500).fadeIn(duration);

			break;
		case 'rightFrame':
			$('.nothingNavi').slideUp("fast"); 
			$('div#nothingContent').fadeOut(200);
			$('div.blankLogo').show();
			//$('div.leftFrameLogo').fadeOut(200);
			$('div.rightFrame').animate({ "width" : pLargeWidth+"px", "left" : pSmallWidth+"px"}, duration);
			$('div.generalFrame').animate({ "width" : pSmallWidth+"px", "left" : (pSmallWidth+pLargeWidth)+"px"}, duration);
			$('.generalFrame .frameNavi a').each(function() { $(this).removeClass('activeLink') });
			$('#levelFrameGeneral').animate({"left" : (pSmallWidth+pLargeWidth-5)+"px"}, duration);
			$('#levelFrameRight').animate({"left" : (pSmallWidth-5)+"px"}, duration);
			hideNothingNavi();
			clearContent('nothing');
			$('#generalContentBox').fadeOut();

			if(contentType == 'product' && showEverythingNavi) {
				slideEverythingNavi();
				//$('.everythingNavi:not(:animated)').slideDown("fast");
			}
			
			//$('div#everythingContent').delay(duration).fadeIn(duration/2);
			$('div#everythingContent').delay(duration/2).show();
			$('div.rightFrameLogo').delay(500).fadeIn(duration);

			break;
		case 'generalFrame':
			showMainBG();
			$('#generalContentBox').fadeIn();
			//$('#debug').append('74: ' + frame + ' / '+ contentType + '<br />');
			$('.nothingNavi').slideUp("fast"); 
			$('.everythingNavi').slideUp("fast"); 
			//$('div.everythingTeaserText').fadeOut(200);
			//$('div#everythingContent').delay(duration).fadeOut(200);
			//$('div#nothingContent').delay(duration).fadeOut(200);
			$('div#everythingContent').fadeOut(200);
			$('div#nothingContent').fadeOut(200);
			//$('div.leftFrameLogo').fadeOut(200);
			//$('div.rightFrameLogo').fadeOut(200);
			//$('div.blankLogo').fadeOut(200);
			$('#levelFrameRight').animate({"left" : (pSmallWidth-5)+"px"}, duration);
			$('#levelFrameGeneral').animate({"left" : (pSmallWidth*2-5)+"px"}, duration);
			$('div.rightFrame').animate({ "width" : pLargeWidth+"px", "left" : pSmallWidth+"px"}, duration);
			$('div.mainFrame').animate({ "width" : pSmallWidth+"px", "left" : "0px"}, duration);
			$('div.generalFrame').animate({ "width" : (pLargeWidth+50)+"px", "left" : (pSmallWidth*2)+"px"}, duration);
			$('.everythingTeaser').show();
			$('div.everythingCorrespondingNavi').remove();
			$('div.nothingCorrespondingNavi').remove();
			$('div.nothingCorrespondingImage').remove();
			hideNothingNavi();
			hideEverythingNavi();
				
			break;
	}	
}

function showProduct(url, cat) {
	//alert('produktseite: ' + url);
	contentType = 'product';
	productCategory = cat;
	showBlankBG();
	
	urlPart = url.split("info=");
	urlPart2 = urlPart[1].split("_");
	corrUrl = "/corresponding_products.php?pID="+urlPart2[0].substring(1);
	$('div.everythingCorrespondingNavi').remove();
	$('div.nothingCorrespondingNavi').remove();
	$('.everythingTeaser').hide();
	if(cat == 'everything' && activeFrame != 'rightFrame') {
		$('div.nothingCorrespondingImage').remove();
	}
	
	switch(cat) {
		case 'nothing':
			$('div.nothingCorrespondingImage').remove();
			contentBox = 'nothingContent';
			frame = 'leftFrame';
			showNothingNavi = true;
			//$('.everythingTeaser').show();
			$.ajax({
				url: corrUrl,
				success: function(data){
					$('.rightFrame div.frameNavi').append(data);
					slideNothingNavi();
				}
			})
			break;
		case 'everything':
			contentBox = 'everythingContent';
			frame = 'rightFrame';
			//$('.everythingTeaser').hide();
			showEverythingNavi = true;
			$.ajax({
				url: corrUrl,
				success: function(data){
					$('div.nothingCorrespondingImage').remove();
					$('.leftFrame div.frameNavi').append($(data).filter('.ncn'));
					if(activeFrame != 'rightFrame') {
						$('.leftFrame').delay(500).append($(data).filter('.nci'));
					} else {
						$('.leftFrame').append($(data).filter('.nci'));
					}
					slideEverythingNavi();
				}
			})
			break;
	}
	
	$.ajax({
		url: preLink+urlPart[1], 
		success: function(data){
			$('#'+contentBox).html($(data).find('.content'));
			$('.productImagesNavi').append('<script type="text/javascript">initGallery();</script>');
			activeImage = 0;
			//hideMainBG();
		}
  });
  handleLinks(url);
	if(activeFrame != frame) {
		activeFrame = frame;
		activateFrame(frame);
	}
	return false;
}

function showOverview(url, cat) {
	$('div.everythingCorrespondingNavi').remove();
	$('div.nothingCorrespondingNavi').remove();
	$('div.nothingCorrespondingImage').remove();
	$('.everythingTeaser').hide();

	contentType = 'overview';
	productCategory = cat;
	showBlankBG();
	switch(cat) {
		case 'nothing':
			contentBox = 'nothingContent';
			showNothingNavi = true;
			frame = 'leftFrame';
			//$('.everythingTeaser').show();
			break;
		case 'everything':
			contentBox = 'everythingContent';
			frame = 'rightFrame';
			//$('.everythingTeaser').hide();
			showEverythingNavi = true;
			break;
	}

	urlP = url.split('?');
	$.ajax({url: '/?'+urlP[1], 
		success: function(data){
			//$('#debug').html('contentBox: #'+contentBox+'<br />'+$('#'+contentBox).css('display')+'<br />'+$(data).find('#'+contentBox));
			//$('#'+contentBox).html($(data).find('#'+contentBox));
			$('#'+contentBox).html($(data).find('.productsOverviewBox'));
		}
  });
  handleLinks(url);
  
  if(activeFrame != frame) {
		activeFrame = frame;
		activateFrame(frame);
	}

	return false;
}

function showHomePage(cat) {
	$('div.everythingCorrespondingNavi').remove();
	$('div.nothingCorrespondingNavi').remove();
	$('div.nothingCorrespondingImage').remove();
	contentType = 'index';
	showMainBG();
	switch(cat) {
		case 1:
			contentBox = 'nothingContent';
			frame = 'leftFrame';
			$('.everythingTeaser').show();
			hideNothingNavi();
			break;
		case 2:
			contentBox = 'everythingContent';
			frame = 'rightFrame';
			$('.everythingTeaser').show();
			hideEverythingNavi();
			break;
	}
	link = '/?action=home&cat='+cat;
	$.ajax({url: link,
		success: function(data){
			if(cat == 1) {
				$('#'+contentBox).html($(data).find('#hc'));
			}
			if(cat == 2) {
				$('#'+contentBox).html($(data).find('#everythingContent'));
			}
		}
  });
  serverLink = document.location.protocol + '//' + document.location.hostname;
	handleLinks(serverLink+link);
	if(activeFrame != frame) {
		activeFrame = frame;
		activateFrame(frame);
	}
	return false;
}

function slideNothingNavi() {
	$('.nothingNavi:not(:animated)').slideDown("fast");
}

function slideEverythingNavi() {
	$('.everythingNavi:not(:animated)').slideDown("fast");
}

function setContentType(cType) {
	contentType = cType;
}

function setActiveFrame(frame) {
	activeFrame = frame;
}

function hideNothingNavi() {
	showNothingNavi = false;
	$('.nothingNavi').slideUp("fast");
}

function hideEverythingNavi() {
	showEverythingNavi = false;
	$('.everythingNavi').slideUp("fast"); 
}

function hideMainBG() {
	$('.leftFrame').css({'backgroundImage':'none'});
}

function mainBGhidden() {
	state = $('.leftFrame').css('backgroundImage');
	return (state == 'none') ? true : false;
}

function showMainBG() {
	$('.leftFrame').css({'backgroundImage':'url(/templates/blank/images/design/bg_mainframe_home.jpg)'});
	//$('.leftFrame').css({'backgroundImage':'url(/templates/blank/images/design/bg_canvas.gif)'});
}

function showBlankBG() {
	$('.leftFrame').css({'backgroundImage':'url(/templates/blank/images/design/bg_mainframe_blank.jpg)'});
	//$('.leftFrame').css({'backgroundImage':'url(/templates/blank/images/design/bg_canvas.gif)'});
}

function clearContent(cat) {
	switch(cat) {
		case 'nothing':
			$('div#nothingContent').html('');
			break;
		case 'everything':
			$('div#everythingContent').html('');
			break;
	}
}

function showGeneralContent(link) {
	/*
	setContentType('index');
	if(mainBGhidden()) {
		showMainBG();
	}
	*/
	$.ajax({url: link, 
		success: function(data){
			//$('#'+contentBox).html($(data).filter('div.content'));
			$('.generalContent').html($(data).find('.generalContent'));
		}
  });
  frame = 'generalFrame';
	if(activeFrame != frame) {
		activeFrame = frame;
		activateFrame(frame);
	}
  handleLinks(link);
}

function activateLink(obj) {
	$('.productInfoLinks a.activeLink').removeClass('activeLink');
	$(obj).addClass('activeLink');
	$(obj).blur();
}

function deactivateIndexContent() {
	showIndexContent = false;
}

function initGallery() {
	//alert('initGallery');
	activeImage = 0;
	$('.productImagesNavi a').unbind('click');
	$('.productImagesNavi a:nth-child(3)').click(function() { nextProductImage();return false; });
}

function nextProductImage() {
	activeImage++;
	var imgCount = $('.productImagesWrapper img').length;
	if(activeImage > 0 && $('.productImagesNavi a:first').hasClass('inactive')) {
		$('.productImagesNavi a:first').removeClass('inactive')
			.click(function() { prevProductImage();return false; });
	}
	if(activeImage == (imgCount-1) && !$('.productImagesNavi a:first').hasClass('inactive')) {
		$('.productImagesNavi a:nth-child(3)').addClass('inactive')
			.unbind('click')
			.click(function() { return false; });
	}
	//$('#debug').append('activeImage: ' + activeImage + '<br />');
	$('.productImagesWrapper').animate({'marginLeft':(-1*activeImage*productImgWidth)+'px'}, 600);
}

function prevProductImage() {
	activeImage--;
	var imgCount = $('.productImagesWrapper img').length;
	if(activeImage < 1) {
		$('.productImagesNavi a:first').addClass('inactive').unbind('click');
	}
	if(activeImage < (imgCount-1) && $('.productImagesNavi a:nth-child(3)').hasClass('inactive')) {
		$('.productImagesNavi a:nth-child(3)').removeClass('inactive')
			.click(function() { nextProductImage();return false; });
	}
	//$('#debug').append('activeImage: ' + activeImage + '<br />');
	$('.productImagesWrapper').animate({'marginLeft':(-1*activeImage*productImgWidth)+'px'}, 600);
}

function setActiveNavigation(frame) {
	switch(frame) {
		case 'nothing':
			showNothingNavi = true;
			showEverythingNavi = false;
			break;
		case 'everything':
			showEverythingNavi = true;
			showNothingNavi = false;
			break;
	}
}

function handleLinks(url) {
	if(url == undefined) {
		url = document.location.href
	}
	if(url == 'reset') {
		url = '';
	}
  $('.slideFrame a').each(function() {
  	if(this.href == url) {
	  	$(this).addClass('activeLink');
	  } else if ($(this).hasClass('activeLink')) {
	  	$(this).removeClass('activeLink');
	  }
  });
}

function popWindow(url, popup_width, popup_height, scroll_value) {
	popup_values = { width:popup_width, height:popup_height, left:0, top:0, scroll:scroll_value, resize:(scroll_value == "yes" ? "yes" : "no") };

	try {
		this.popup_win.close(); //Versuchen eventuelle offenes Fenster wieder zu schliessen
	} catch(e) {
		//alert(e);
	}

	//Opera
	if(window.innerWidth < popup_values.width) popup_values.width = window.innerWidth;
	else popup_values.left = Math.round((window.innerWidth - popup_values.width) / 2);
	if(window.innerHeight < popup_values.height) popup_values.height = window.innerHeight;
	else popup_values.top = Math.round((window.innerHeight - popup_values.height) / 2);

	settings = 'height='+popup_values.height+',width='+popup_values.width+',top='+popup_values.top+',left='+popup_values.left+',resizable='+popup_values.resize+',scrollbars='+popup_values.scroll;
	try {
		this.popup_win = window.open(url,'',settings);
	} catch (e) {
		return true;
	}

	try {
		this.popup_win.focus();
	} catch (e) {
		//alert(e);
	}
	return false;
}

function hoverFormButton(event, element) {
	if(event.type == 'mouseover') {
		element.style.backgroundPosition = '100% 0';
	} else {
		element.style.backgroundPosition = '0 0';
	}
}




