/**/

var icubic = {};

icubic.init = function(){
	// define language in js
	icubic.currentLanguage = lang ;
	icubic.searchValue = (icubic.currentLanguage === 'en' ? 'search term ...' : 'Suchbegriff ...');
	
	$('#edit-search-block-form-1').val(icubic.searchValue).bind('click focus',function(){
		if($('#edit-search-block-form-1').val() === icubic.searchValue){
			$('#edit-search-block-form-1').val('');
		}
	});
	
	// init navigation
	icubic.navigation();
	icubic.footer();
	
	
	
	// init management 
	if($('#theManagement').length === 1 ){
		icubic.management();
	};
	
	// init home products
	if($('#homeProducts').length === 1 ){
		icubic.products.home();
	};
	
	// init home products
	if($('#productsDetail').length === 1 ){
		icubic.products.details.init();
	};
	
	// init contact forms
	if($('#webform-client-form-162,#webform-client-form-163').length === 1 ){
		icubic.contact();
	};
	
	// init news
	if($('theNews').length === 1){
		icubic.news();	
	}
};
	
icubic.products = {};
icubic.products.details = {};
icubic.products.details.currentlyShown = 0;
icubic.products.details.moveLeft = function(){
	
};
icubic.products.details.moveRight = function(){
	
};
icubic.products.details.init = function(){
	$('#productsDetail div.buttonForward a, #productsDetail div.buttonBack a').addClass(icubic.currentLanguage);
	
	icubic.products.details.count = $('#productsDetail ul li').length - 1;
	
	if(icubic.products.details.count === -1){
		$('#productsDetail div.buttonForward').hide();
	}
	
	if(this.currentlyShown === 0){
		$('#productsDetail div.buttonBack').hide();
	}
	
	$('#productsDetail div.buttonForward a').click(function(){
		
		$('#productsDetail div.buttonForward a').hide();
	
		$('#productsDetail ul').animate({
			left: $('#productsDetail ul').position().left-735
			},'slow',function(){
				$('#productsDetail div.buttonForward a').show();		
		});
		icubic.products.details.currentlyShown++;
		icubic.products.details.move();	
		return false;
	});
	
	$('#productsDetail div.buttonBack a').click(function(){
		
		$('#productsDetail div.buttonBack a').hide();
					
		$('#productsDetail ul').animate({
			left: $('#productsDetail ul').position().left+735
			},'slow',function(){
			$('#productsDetail div.buttonBack a').show();
		});
		icubic.products.details.currentlyShown--;
		icubic.products.details.move();	
		return false;
	});
};
icubic.products.details.move = function(){
	
	if(this.currentlyShown === 0){
		$('#productsDetail div.buttonBack').hide();
		$('#productsDetail div.buttonForward').show();
	} else if (this.currentlyShown < this.count) {
		$('#productsDetail div.buttonBack').show();
		$('#productsDetail div.buttonForward').show();
	} else if (this.currentlyShown == this.count) {
		$('#productsDetail div.buttonBack').show();
		$('#productsDetail div.buttonForward').hide();
	}
};
icubic.products.home = function(){
	$('#homeProducts li>div').corner();
	
	openedElement = '';
	positionStart = new Array(740,-358,740,-358,740,-358);
	positionEnd = new Array(386,33,386,33,386,33);
	
	$('#homeProducts div.buttonProduct a').each(function(e){
		
		$(this).click(function(){
			myself = this;
			
			if( openedElement !== '' && openedElement !== e){
			
				// if an opened element is found close it and set the button state to default
				$('#homeProducts li:eq('+openedElement+')> div.productShortView').animate({ backgroundColor: '#dbecd2' }, 'fast', function(){
							
							$('#homeProducts li:eq('+openedElement+') div.buttonProduct a').toggleClass('active');
							$('#homeProducts li:eq('+openedElement+') div.buttonProduct a').attr('title','Detailansicht anzeigen');
								
							$('#homeProducts li:eq('+openedElement+') div.productLongView').animate({ left: positionStart[openedElement] }, 'fast', function(){
								$('#homeProducts li:eq('+openedElement+') div.productLongView').hide();
								
								openedElement = '';
								
								$('#homeProducts li:eq('+e+')>div').animate({ backgroundColor: '#d0d9d9' }, 600, function(){
									$('#homeProducts li:eq('+e+') div.productLongView').show().animate({ left: positionEnd[e] }, 800);
									
										openedElement = e;	
								});
								
								$(myself).attr('title','Detailansicht schliessen');
								$(myself).toggleClass('active');
							});
							
					});
					
					return false;
					
			} else {
			
				if( $('#homeProducts li:eq('+e+') div.productLongView:visible').length === 0 ){
						openedElement = e;
						$('#homeProducts li:eq('+e+')>div').animate({ backgroundColor: '#d0d9d9' }, 600, function(){
								$('#homeProducts li:eq('+e+') div.productLongView').show().animate({ left: positionEnd[e] }, 800);	
						});
						$(this).attr('title','Detailansicht schliessen');
						
				} else {
						openedElement = '';
						$('#homeProducts li:eq('+e+')> div.productShortView').animate({ backgroundColor: '#dbecd2' }, 600, function(){
								$('#homeProducts li:eq('+e+') div.productLongView').animate({ left: positionStart[e] }, 800, function(){
									$('#homeProducts li:eq('+e+') div.productLongView').hide();
								});
						});
						$(this).attr('title','Detailansicht anzeigen');
				}
				$(this).toggleClass('active');
				return false;
			}
		});
	});
};

icubic.news = function(){
	$('#theNews').accordion({
		header: 'div.node-header'
	});
};

icubic.management = function() {
	$('#theManagement ul li div.buttonForward a,#theManagement ul li div.buttonBack a').addClass(icubic.currentLanguage);
	
	
	$('#block-block-6').hide();
	$('#block-block-7').show();
	$('#block-block-8').hide();
	
	$('#theManagement ul li div.buttonForward a').click(function(){
		$('#theManagement ul').animate({
		left: $('#theManagement ul').position().left-735
		},'slow');
		
		$('#block-block-6').show();
		$('#block-block-7').hide();
		$('#block-block-8').hide();
		return false;
	});
	
	$('#theManagement ul li div.buttonBack a').click(function(){
		
		$('#theManagement ul').animate({
		left: $('#theManagement ul').position().left+735
		},'slow');
		
		$('#block-block-6').hide();
		$('#block-block-7').show();
		$('#block-block-8').hide();
		return false;
	});
};



icubic.contact = function() {
	$('span.form-required').parent('label').css('font-weight','bold');
	
	// remove label from zustimmung
	$('#webform-component-zustimmung label[class!=option]').html('');
	$('#webform-component-agreement label[class!=option]').html('');
	
	if($('#webform-client-form-162').length === 1 ){
	// englisch
		$("#webform-client-form-162").validate({
			errorContaier : 'div.errorContainer',
			errorLabelContainer: 'div.errorContainer ul',
			wrapper: 'li',
			invalidHandler: function(form, validator) {
			  var errors = validator.numberOfInvalids();
			  if (errors) {
				$("div.errorContainer").show();
			  } else {
				$("div.errorContainer").hide();
			  }
			},
			rules: {
				'submitted[salutation]': 'required',
				'submitted[first_name]': 'required',
				'submitted[last_name]': 'required',
				'submitted[agreement][Agreement]':'required',
				'submitted[e_mail]' : 'required'
			},
			messages: {
				'submitted[salutation]': 'Please select your salutation',
				'submitted[first_name]': 'Please enter your firstname',
				'submitted[last_name]': 'Please enter your lastname',
				'submitted[e_mail]': 'Please enter a valid email adress',
				'submitted[agreement][Agreement]': 'Please agree to store the submitted data'
			}
		});
		
	} else if($('#webform-client-form-163').length === 1 ){
	// deutsch
		$("#webform-client-form-163").validate({
			errorContaier : 'div.errorContainer',
			errorLabelContainer: 'div.errorContainer ul',
			wrapper: 'li',
			invalidHandler: function(form, validator) {
			  var errors = validator.numberOfInvalids();
			  if (errors) {
				$("div.errorContainer").show();
			  } else {
				$("div.errorContainer").hide();
			  }
			},
			rules: {
				'submitted[anrede]': 'required',
				'submitted[vorname]': 'required',
				'submitted[nachname]': 'required',
				'submitted[e_mail]': {
			required: true,
			email: true
			},
				'submitted[zustimmung][Zustimmung]': 'required'
			},
			messages: {
				'submitted[anrede]': 'Bitte w&auml;hlen Sie eine Anrede aus',
				'submitted[vorname]': 'Bitte geben Sie Ihren Vornamen an',
				'submitted[nachname]': 'Bitte geben Sie Ihren Nachnamen an',
				'submitted[e_mail]': 'Bitte geben Sie eine g&uuml;ltige E-Mail Adresse an',
				'submitted[zustimmung][Zustimmung]': 'Bitte stimmen Sie der Speicherung der Daten zu'
			}
		});
	}
};

icubic.footer = function(){
	$('#block-menu-secondary-links ul.menu li:first').css('border-left','none');
};
		
icubic.navigation = function(){
	// remove left border of first navigation item for ie 6
	$('#block-menu-primary-links ul.menu li:first').css('border-left','none');
	$('#block-menu-primary-links ul.menu li:first a').css('text-indent','35px');
	
	$('#block-menu-primary-links ul.menu li ul.menu li:first-child').css('border-top','none');
	$('#block-menu-primary-links ul.menu li ul.menu li a').css('text-indent','10px');
	
	
	
	$('li#Home>a, li#Unternehmen>a, li#Produkte>a, li#Nachrichten>a, li#Kontakt>a, li#Kundenbereich>a, li#Company, li#Products, li#News, li#Contact, li#CustomerArea').each(function(e){
	
	  $(this).mouseover(function(){
				
				// remove highlights and dropdowns
				$('#block-menu-primary-links ul.menu>li:not(:eq('+e+')) a').removeClass('active');
				$('#block-menu-primary-links ul.menu>li>ul.menu').hide();
				
		  // highlight current and show dropdown
		  $(this).addClass('active');
		  $('#block-menu-primary-links ul>li:eq('+e+')>ul.menu').show();
			
			// add layer
		  $('#navLayer').css({
			height: $(window).height()}
		  ).show().mouseover(function(){
					$('#block-menu-primary-links ul.menu>li>ul.menu').hide();
					$('#navLayer').hide();
					$('#block-menu-primary-links ul.menu li:eq('+e+') a').removeClass('active');
				});
							
	  }); 
	  $(this).mouseout(function(){
								//alert("test");
		$(this).removeClass('active');					
	  }); 
	});
};		

icubic.reference = function(){
	$('#references ul li div.reference img').each(function(e){
		$(this).css('margin-top',(65-$(this).height())/2);
		
		$(this).hover(function(){
			$('#references ul li:eq('+e+') div.referenceText p').slideDown('fast');
		},function(){
			$('#references ul li:eq('+e+') div.referenceText p').slideUp('fast');
		});
	});
};
