$(document).ready(function() {

	// ------------------------------------------------------------------------------------------------------------------------------------------
	// Initialize 
	// ------------------------------------------------------------------------------------------------------------------------------------------
	var $tabLoadType='&type=101';
	
	var $subNavSpeed='normal';
	
	initLogo();
	initBG();
	initLinkBox();
	initPullDowns();
	initSubNav();
	initTabs();
	initImageGallery();
	initNewsBox();
	initRegistrationError();
	superScript();
	checkPrettyPhoto();
	zoomGfx();
	// ------------------------------------------------------------------------------------------------------------------------------------------
	// change logo link
	// ------------------------------------------------------------------------------------------------------------------------------------------	
	function initLogo(){
		$('#logo').parent().attr('href', 'http://www.brabender.de');
	}	
	// ------------------------------------------------------------------------------------------------------------------------------------------
	// create zoom graphic for zoomable Pictures
	// ------------------------------------------------------------------------------------------------------------------------------------------	
	function zoomGfx(){
		var $zoomPic="<span class='zoomPic'></span>";
		$("a[rel^='prettyPhoto']").prepend($zoomPic);
	}	
	// ------------------------------------------------------------------------------------------------------------------------------------------
	// check for PrettyPhoto Elements in ContentBox
	// ------------------------------------------------------------------------------------------------------------------------------------------	
	function checkPrettyPhoto(){
		if($('.contentBox').length>0){
			$("#mainContent a[rel^='prettyPhoto']").prettyPhoto();
			$('.contentBox:last-child').css('margin','0px 0px 0px 0px');
			$('.contentBox:last-child').css('border','none');
			$('.contentBox:last-child').css('padding','0px 0px 0px 0px');
		}
	}
	// ------------------------------------------------------------------------------------------------------------------------------------------
	// Superscript
	// ------------------------------------------------------------------------------------------------------------------------------------------	
	function superScript(){
		if($('#subNav').length>0){
			$('#subNav a').each(function(i) {   
	            $(this).html($(this).html().replace(unescape("%AE"),"<sup>"+unescape("%AE")+"</sup>"));    
	        });
		}
		if($('#breadCrumb').length>0){
			$('#breadCrumb li').each(function(i) {   
	            $(this).html($(this).html().replace(unescape("%AE"),"<sup>"+unescape("%AE")+"</sup>"));    
	        });
		}
		if($('.quickLink').length>0){
			$('.quickLink a').each(function(i) {   
	            $(this).html($(this).html().replace(unescape("%AE"),"<sup>"+unescape("%AE")+"</sup>"));    
	        });
		}
		
	}
	// ------------------------------------------------------------------------------------------------------------------------------------------
	// Set NewsBox
	// ------------------------------------------------------------------------------------------------------------------------------------------		
	function initNewsBox(){
		if($('.newsBox').length>0){
			$('.newsBox .newsItem:last-child').css('border','none');
		}		
	}
	// ------------------------------------------------------------------------------------------------------------------------------------------
	// Set BgImage
	// ------------------------------------------------------------------------------------------------------------------------------------------		
	function initBG(){
		if($('#subContent').length>0){
			$('#siteContainer').css('background-position', '0px 0px');
		}else if($('#subNav').length>0){
			$('#siteContainer').css('background-position', '-953px 0px');
		}else if($('#mainNav').length>0){
			$('#siteContainer').css('background-position', '-721px 0px');
		}else{
			$('#siteContainer').css('background-position', '-2000px 0px');
		}
	}
	// ------------------------------------------------------------------------------------------------------------------------------------------
	// init LinkBoxes
	// ------------------------------------------------------------------------------------------------------------------------------------------	
	function initLinkBox(){ 
		var $count=0;
		if($('.linkBox').length>0){
			$(".linkBox").each(function(intIndex){
				$count++;
				if($(this).hasClass('wide')){
					$count--;
				}else{
					if(($count/2)!=(Math.floor($count/2))){
						$(this).css("margin-right","15px");
					}			
				}
			});
		}
	}
	// ------------------------------------------------------------------------------------------------------------------------------------------
	// PullDown Menus
	// ------------------------------------------------------------------------------------------------------------------------------------------		
	function initPullDowns(){ 
		$('.linkBox span').mouseover(function(){
			$(this).css('background-position','-311px 0px');
			$(this).css('border-color','#005293');
			$(this).css('color','black');
			$(this).next().css('border-color','#005293');
		});
		$('.linkBox span').mouseout(function(){
			$(this).css('background-position','0px 0px');
			$(this).css('border-color','#c4c4c4');
			$(this).css('color','#646464');
			$(this).next().css('border-color','#c4c4c4');
		});
		$('.quickLink').mouseover(function(){
			$(this).prev().css('background-position','-311px 0px');
			$(this).prev().css('border-color','#005293');
			$(this).prev().css('color','black');
			$(this).css('border-color','#005293');
		});
		$('.quickLink').mouseout(function(){
			$(this).prev().css('background-position','0px 0px');
			$(this).prev().css('border-color','#c4c4c4');
			$(this).prev().css('color','#646464');
			$(this).css('border-color','#c4c4c4');
		});
		$(document).bind('click',function(e){ 
			var $clicked = $(e.target);
			if (!($clicked.is('#countrySelector') || $clicked.parents().is('#countrySelector'))) {
				$('#countrySelector li.country').css('display', 'none');
			}else if($('#countrySelector li.country').css('display')=='block'){
				$('#countrySelector li.country').css('display', 'none');
				if($clicked.parent().attr('class')=='activeCountry'){
					return false;
				}
			}else if($('#countrySelector li.country').css('display')=='none'){
				$('#countrySelector li.country').css('display', 'block');
				if($clicked.parent().attr('class')=='activeCountry'){
					return false;
				}
			}
			
			if (!($clicked.parents().hasClass('.linkBox')) || $clicked[0].tagName!='SPAN') {
				if($clicked.parent().hasClass('.hasChildren')){
					if($clicked.attr('href')=='#'){
						return false;
					}
				}else{
					$('.linkBox .active').css('display', 'none');
					$('.linkBox .active').removeClass('active');				
				}
			}else if($clicked.next().hasClass('active')){
				$clicked.next().css('display','none');
				$clicked.next().removeClass('active');
			}else if(!($clicked.next().hasClass('active'))){
				$('.linkBox .active').css('display', 'none');
				$('.linkBox .active').removeClass('active');
				$clicked.next().css('display','block');
				$clicked.next().addClass('active');
				$clicked .next().css('border-color','#005293');
			}
		});		
	}	
	// ------------------------------------------------------------------------------------------------------------------------------------------
	// SubNavigation
	// ------------------------------------------------------------------------------------------------------------------------------------------	
	function initSubNav(){ 	
		var $targetID='#subNav'; 		
		if($($targetID).length>0){
			$('ul li a', $targetID).each(function(i){
				var $backgroundLeftPos=parseInt($(this).css('padding-left'))-11;
				if($(this).parent().hasClass('hasChildren')){
					if($(this).hasClass('active')){
						$(this).css('background-position',$backgroundLeftPos-2+'px -276px');
					}else{
						$(this).css('background-position',$backgroundLeftPos+'px 8px');
					}
				}else if($(this).hasClass('active')){
					$(this).css('background-position',$backgroundLeftPos+'px -166px');
				}
				if($(this).hasClass('active') && $(this).next().is("ul")){
					$(this).next().show();
					$(this).addClass('open');
				}else{
					$(this).next().hide();
				}
			});
			$('li a', $targetID).click(clickHandlerSubNav);	
		}
	}	
	function clickHandlerSubNav(event) {
		$('#countrySelector li.country').css('display', 'none');
		$('.linkBox .active').css('display', 'none');
		$('.linkBox .active').removeClass('active');
		if($(this).next().is("ul")){	
			$path=$(this).parent().parent();			
			if($(this).hasClass('open')){
				$('li .open', $path).each(function(i){
					var $backgroundLeftPos=parseInt($(this).css('padding-left'))-11;
					if($(this).hasClass('active')){
						$(this).css('background-position',$backgroundLeftPos+'px -45px');
					}else{
						$(this).css('background-position',$backgroundLeftPos+'px 8px');
					}
				});
				$('li .open', $path).next().slideUp($subNavSpeed);
				$('li .open', $path).removeClass('open');
			}else{
				$('li .open', $path).each(function(i){
					var $backgroundLeftPos=parseInt($(this).css('padding-left'))-11;
					if($(this).hasClass('active')){
						$(this).css('background-position',$backgroundLeftPos+'px -45px');
					}else{
						$(this).css('background-position',$backgroundLeftPos+'px 8px');
					}
				});
				$('li .open', $path).next().slideUp($subNavSpeed);
				$('li .open', $path).removeClass('open');
				$(this).next().slideDown($subNavSpeed);	
				var $backgroundLeftPos=parseInt($(this).css('padding-left'))-11;
				if($(this).hasClass('active')){
					$(this).css('background-position',$backgroundLeftPos-2+'px -276px');
				}else{
					$(this).css('background-position',$backgroundLeftPos-2+'px -223px');
				}
				$(this).addClass('open');		
			}
			if($(this).attr('href')=='#'){
				return false;
			}
		}
	}		
	// ------------------------------------------------------------------------------------------------------------------------------------------
	// TabNavigation
	// ------------------------------------------------------------------------------------------------------------------------------------------	
	function initTabs(){ 	
		var $targetID='#tabNav';
		if($($targetID).length>0){
			$('li a', $targetID).click(clickHandlerTabNav);	
			$tabURL=$(':first-child a', $targetID).attr('href')+$tabLoadType;
			$(':first-child a', $targetID).addClass('active')
			$('#tabContent').load($tabURL);
			$("#tabContent").ajaxComplete(checkLoadedContent);			
			$("#tabContent").prepend("<div class='preloader'></div>");
			var $tabContentWidth=$("#tabContent").width();
			$(".preloader").css("width",$tabContentWidth);
		}
	}
	function checkLoadedContent(){ 
		$(".preloader").remove();	
		initBG();
		initImageGallery();	
		zoomGfx();
	}		
	function clickHandlerTabNav(event) {
		var $contentPos=$(".content").position();
		var $tabContentPos=$("#tabContent").position();
		var $footerPos=$("#footer").position();
		var $tabContentHeight=$footerPos.top-($contentPos.top+$tabContentPos.top);
		var $tabContentWidth=$("#tabContent").width();
		var $tempHeight = $("#tabContent").height();
		$("#tabContent").prepend("<div class='preloader'></div>");
		$(".preloader").css("width",$tabContentWidth);
		$(".preloader").css("height",$tempHeight);
		//$('#siteContainer').css('background-position', '-953px 0px');
		$path=$(this).parent().parent();
		$tabURL=($(this).attr('href'))+$tabLoadType;
		$('li .active', $path).removeClass('active');
		$(this).addClass('active');
		$('#tabContent').load($tabURL);
		$("#tabContent").ajaxComplete(checkLoadedContent);
		return false;
	}	
	// ------------------------------------------------------------------------------------------------------------------------------------------
	// ImageGallery
	// ------------------------------------------------------------------------------------------------------------------------------------------	
	function initImageGallery(){
		var $targetID='.imageGallery';
		if($($targetID).length>0){
			$(".imageGallery").before("<div class='picFull'></div>");
			$(".imageGallery .full").each(function(intIndex){
				$picTxt=$(this).attr("alt");
				$(this).next().attr('title', $picTxt);
				$(this).next().attr('rel','prettyPhoto[gallery1]');
				$(this).clone().prependTo($(this).next());
				$(this).next().appendTo('.picFull');
				$(this).remove();
			});
			$("<span class='imageDescription'></span>").appendTo(".picFull");
			$('.picFull a').css('display','none');
			$('.imageGallery img:first-child').addClass('active');
			$picTxt=$('.imageGallery img:first-child').attr('alt');
			$('.picFull a:first-child').css('display','block');
			$('.picFull .imageDescription').text($($('.imageGallery  img:first-child')).attr("alt"));
			$('.imageGallery .thumbnail:nth-child(4)').css("margin","7px 0px 0px 0px");
			$(".picFull a[rel^='prettyPhoto']").prettyPhoto();
			$('.picFull').css("display","block");
			$('.imageGallery').css("display","block");
			if($('.imageGallery').children().size()>1){
				$('.imageGallery .thumbnail').click(clickHandlerImageGallery);
			}else{
				$('.imageGallery .thumbnail').css('display','none')
			}
		}
	}	
	function clickHandlerImageGallery(event) {
		$index=$(this).parent().children().index(this);
		var $lastIndex=$('img','.imageGallery').index($('img.active','.imageGallery')); 
		$('.imageGallery img:nth-child('+($lastIndex+1)+')').removeClass('active');
		$('.picFull a:eq('+($lastIndex)+')').css('display','none');
		$('.picFull a:eq('+($index)+')').css('display','block');
		$('.picFull .imageDescription').text($(this).attr("alt"));		
		$(this).addClass('active');
	}
	// ------------------------------------------------------------------------------------------------------------------------------------------
	// QuickLink Box
	// ------------------------------------------------------------------------------------------------------------------------------------------	
	function initRegistrationError(){ 	 		
		if($('.tx-srfeuserregister-pi1-error').length>0){
			$(".tx-srfeuserregister-pi1-error").each(function(intIndex){
				if($('span',this).text()==''){
					//alert($('.tx-srfeuserregister-pi1-error span').text());
					$(this).css("display","none");
				}else{
					$('span',this).css("display","none");
					$(this).css("padding","5px 0px 5px 0px");
					$(this).css("width","32px");
					$(this).css("height","17px");
					$(this).mouseover(mouseoverRegistrationError);
					$(this).mouseout(mouseoutRegistrationError);
					$(this).next().focus(onFocusRegistrationError);
					$(this).next().blur(onBlurRegistrationError);			
				}			
			});
		}
	}	
	function mouseoverRegistrationError(event) {
		$(this).css("cursor","pointer");
		$(this).css("padding","5px 10px 5px 40px");
		$(this).css("width","205px");
		$(this).css("height","auto");
		$(this).css("z-index","7");
		$(this).css("height","180px");
		$(this).css("border","1px solid black");
		$('span',this).css("display","inline");
	}	
	function mouseoutRegistrationError(event) {
		$('span',this).css("display","none");
		$(this).css("padding","5px 0px 5px 0px");
		$(this).css("width","32px");
		$(this).css("height","17px");
		$(this).css("z-index","5");
		$(this).css("border","none");
	}
	function onFocusRegistrationError(event) {
		var $lastIndex=$(this).prev();
		$lastIndex.css("z-index","6");
		$lastIndex.css("border","1px solid black");
		$lastIndex.css("padding","5px 10px 5px 40px");
		$lastIndex.css("width","205px");
		$lastIndex.css("height","auto");
		$('span',$lastIndex).css("display","inline");
	}	
	function onBlurRegistrationError(event) {
		var $lastIndex=$(this).prev();
		$('span',$lastIndex).css("display","none");
		$lastIndex.css("z-index","5");
		$lastIndex.css("border","none");
		$lastIndex.css("padding","5px 0px 5px 0px");
		$lastIndex.css("width","32px");
		$lastIndex.css("height","17px");
	}	
});