/* Author: M.Bulfair/ AWC 2011 */
$(document).ready(function() {

	


	// adds an effect called "wall" to the validator
	$.tools.validator.addEffect("wall", function(errors, event) {
	
	// get the message wall
	var wall = $(this.getConf().container).fadeIn();
		
	// remove all existing messages
	wall.find("p").remove();
		
	// add new ones
	$.each(errors, function(index, error) {
		wall.append(
			"<p><strong>" +error.input.attr("data-message")+ "</strong> </p>"
		);		
	});
		
	// the effect does nothing when all inputs are valid	
	}, function(inputs)  {
		
	});
	
	// Basic Form Validation for Careers
	$('.validate').validator({
		messageClass: 'error',
		offset: [-5, 10],
		singleError: false,
		position: 'center right'
		 	
	});
	
	// Advanced Validation for contact form
	$('.validate-adv').validator({
		position: 'top left', 
		offset: [-12, 0],
		message: '<div><em/></div>'
		/*
			effect: 'wall', 
		   container: '.error',
		   
		   // do not validate inputs when they are edited
		   errorInputEvent: null
		*/	
		});
	
	// Print
   $('.print').click(function() {
        window.print();
        return false;
    });
        
    
   //Product Images Cycle
   $('.product-cycle').cycle({pager:  '.product-cycle-nav', next:'.product-next', prev:'.product-previous' });
   
   //News Cycle 
   $('.news-items').cycle({fx:'scrollVert',next:'.down', prev:'.up',timeout:'5000', speed:'500'});
   
    //Light Box for active properties
	$('.product-properties li.active a').colorbox({innerHeight:'300'});
	
	// Light box on clicked properties inside the lightbox
	$('.product-properties-large li.inactive a').live('click',function(){
				
			$.colorbox({
		        open: true,
		        href: this.href,
		        innerHeight:'300'
		    });
			
			return false;
	});
	
	// Link Prevention on Certain Properties
	 $('.product-properties-large li.active a, .product-properties li.inactive a').live('click',function(){  
	 
	 	return false;
	 });
	 
	// news - filter 12 months
	$('#filter-news').change(function(){
		var loc = window.location.href;
		var url_parts = loc.split('?');
		loc = url_parts[0]+'?lastYearOnly='+$(this).val();
		window.location = loc;
	});
	
	// news - filter financial results
	$('#filter-results').change(function(){
		var loc = window.location.href;
		var url_parts = loc.split('?');
		loc = url_parts[0]+'?year='+$(this).val();
		
		window.location = loc;
	});
	
	// Hover Function for Active Properties
	$('.product-properties li.active a').hover(
		  function () {
		   var html = '<span class="tool-tip"></span>';
		   var property = $(this).prop('rel');
		   $('.right-column').append(html);
		   $('.tool-tip').text(property);
		  },
		  function () {
		  	$('.tool-tip').remove();
		  }
	);
	
	//Career Map Selector
	$('.career-select-region').live('hover',function(){
		/* Grab the 'rel' property */
		var region = $(this).prop('rel');
		
		/* Remove All Classes */
		$('#career-map').removeClass();
		
		/* Set the Class of the link */
		$('#career-map').addClass(region);
		
	});
	
	
	//Accordion
	$('.spring li strong').live('click',function(){
		
		
		$(this).next().slideToggle();
		if($(this).parent().hasClass('open')) {
			
			$(this).parent().removeClass('open').addClass('closed');
			//$('.spring li.open').removeClass().addClass('closed');
			//$(this).parent().addClass('closed');
		
		} else {
		
			//$('.spring li.closed').removeClass();
			$(this).parent().removeClass('closed').addClass('open');
			
		
		}
	
	});
	
	//Dropdowns
	$(".dropdown").hoverIntent({over:openMenu,timeout: 250, out:closeMenu
	
	});
	
	function openMenu()
	{ 
		$(this).find('ul').slideDown('fast');
	}
	
	function closeMenu()
	{
		$(this).find('ul').fadeOut('fast');
	} 
	
	
	//Prevent Clicks
	$('.no-click').live('click',function(){
		return false;
	});
	
	
	//Tabs
	//When page loads...
	$(".tab_content").hide(); //Hide all content
	$("ul.tabs li:first").addClass("active").show(); //Activate first tab
	$(".tab_content:first").show(); //Show first tab content

	//On Click Event
	$("ul.tabs li").click(function() {

		$("ul.tabs li").removeClass("active"); //Remove any "active" class
		$(this).addClass("active"); //Add "active" class to selected tab
		$(".tab_content").hide(); //Hide all tab content

		var activeTab = $(this).find("a").attr("href"); //Find the href attribute value to identify the active tab + content
		$(activeTab).fadeIn(); //Fade in the active ID content
		
		
		return false;
	});
	
	// 2 Tab Events for both sections
	//When page loads...
	var uLang = $.cookie('user_lang');
	if ( ! uLang) {
		uLang = 'eu';
	}
	$(".tab_content_bottom").hide(); //Hide all content
	$("ul.tabs_bottom li."+uLang+":first").addClass("active").show(); //Activate first tab
	
	if ( uLang == 'ja') {
	
			$("#japan").show(); //Show japan tab content

	} else {
			//Equal Heights
	$(function(){
			var H = 0;
			
			$('#americas .equality').each(function(i) {
				var h = $('#americas .equality').eq(i).height();
			  if(h > H) {
			  	H = h;
			  } 
			});
		  $('#americas .equality').height(H);
		});

			$(".tab_content_bottom:first").show(); //Show first tab content

	}
	
	
	
	//On Click Event 
	$("ul.tabs_bottom li").click(function() {

		$("ul.tabs_bottom li").removeClass("active"); //Remove any "active" class
		$(this).addClass("active"); //Add "active" class to selected tab
		$(".tab_content_bottom").hide(); //Hide all tab content

		var activeTab = $(this).find("a").attr("href"); //Find the href attribute value to identify the active tab + content
		$(activeTab).fadeIn(300, function(){
			    
			   
			    //Equal Heights
				$(function(){
						var H = 0;
						
						$(activeTab+' .equality').each(function(i) {
							var h = $(activeTab+' .equality').eq(i).height();
						  if(h > H) {
						  	H = h;
						  } 
						});
					  $(activeTab+' .equality').height(H);
					});
		}); //Fade in the active ID content
		return false;
	});
	
	//Global Network
		$('#gn-thumbs').jcarousel({
						scroll:8,
						wrap: "circular",
						easing: "easeInOutBack",
						animation: 500
				});
				
		
		
		/*Innovation Larger Image*/
		/* Defaults first car to selected */
		$('#gn-thumbs li:first').addClass('current');
		
		$('#gn-thumbs li').hover(
			function () {
				
				$(this).addClass("hover");
				$(this).css({cursor:"pointer"});
				
			},
			function () {
				$(this).removeClass("hover");
			}
			
		);
		
		$('#gn-thumbs li a').click(function(){
			var getLarge = $(this).attr('href');
			
	    $('.img-holder').fadeOut(500,function(){
		    $('.img-holder').attr('src',getLarge);
		    $('.img-holder').css('display','block');
		    $('.img-holder').fadeIn();
	    });
	    
			$('#gn-thumbs li').removeClass('current');
			$(this).parents('li').addClass('current');
			
			return false;
		});	
});
























