/**
 * ON DOCUMENT READY
 */
$(document).ready(function() {
	try{
		// add Class with browser name to body							
		$('body').addClass(BROWSER);	
		//Initialize carousel: 'Company'
		if($('#hangar-life-car').length == 0)
			$(".jcarousel-skin-tango").jcarousel({wrap: 'circular', scroll: 1});
		$('#show-all').addClass('active');
		
		$('#car1-overlay-carousel, #car3-overlay-carousel').scrollable({
			circular: true
		});
		
		//Reset hidden box opacity IE fix
		/*$("div[class='hiddenbox']")
			.css("opacity", 0)
			.css("-ms-filter","'progid:DXImageTransform.Microsoft.Alpha(Opacity=0)'")
			.css("filter","alpha(opacity=0)");*/
		
		if(document.body.id != 'site'){
			moveContent();	
			window.onresize = function(event) {moveContent()}	
		}
		
		//temporary reel last image height issue 
		$('ul.thumbs li.last:first').find('img').css('height', '90px');
		$('ul.thumbs li.last:last').find('img').css('height', '64px');
		
		shortcut.add("alt+w",function() {
				showVideo('<iframe height="360" frameborder="0" width="480" src="http://www.youtube.com/embed/2WNrx2jq184?autoplay=1" allowfullscreen=""></iframe>');
		});
		
		shortcut.add("ctrl+l",function() {
				showHeroes();
				//showVideo('<iframe height="360" frameborder="0" width="480" src="http://www.youtube.com/embed/2WNrx2jq184?autoplay=1" allowfullscreen=""></iframe>');
		});
	}
	
	catch(e){
		//alert(e);//for dev only
	};


	/**
	 * 
	 */
	
	/**
	 * Modal Controller
	 * @options:
	 *   rel
	 * 	 data-file
	 * 	 data-resolution
	 *   data-container: Just for SWF files
	 * 	 data-fixed: if fixed or scroll with the contentn
	 * 
	 * @documentation [SWF example]:
	 * 	Markup:
	 * 		<a href="" rel="overlay-container" data-resolution="300x250" data-file="path/to/the/swf_file"></a>
	 *  Overlay container:
	 *  	<div id="overlay-container"></div>
	 *  Swf Container:
	 *  	<div id="swf-container"></div>
	 *  
	 *  @author: wrcs
	 * */
	var overlay_options = {
		mask: '#000000',
		fixed: true,
		close: '.close',
		top: 'center',
		onBeforeLoad: function() {
			var trigger 	= this.getTrigger();
			var overlay 	= this.getOverlay();
	
			var $elm		= $(trigger), 
				file		= $.trim($elm.attr('data-file')),
				ext			= file.substr((file.lastIndexOf('.') +1)),
				container	= $.trim($elm.attr('data-container')),
				resolution	= $.trim($elm.attr('data-resolution')).split('x'),
				w			= (resolution[0] && resolution[0]>0) ? resolution[0]+'px' : 'auto',
				h			= (resolution[1] && resolution[1]>0) ? resolution[1]+'px' : 'auto';
			
			//Open in Iframe
			if($elm.attr('data-type') === 'external') {
				var iframe = $(overlay).find('iframe');
				iframe.attr({
					'src': $elm.attr('href'),
					'width': w,
					'height': h
				});
			}
				
			//Change options on the fly	
			this.getConf().fixed = ($elm.attr('data-fixed') === 'false') ? false : true;			
			
			switch(ext) {
				//Case an SWF
				case "swf":					
					swfobject.embedSWF(file, container, w, h, "9.0.0");
				break;
			}
			
			$(overlay).css({
				'width': w,
				'height': h
			});
		}
	};
	$('.overlay[rel]').overlay(overlay_options);
	
	// -- Stor overlay_options
	$('body').data('overlay_options', overlay_options);
	
	//end Modal Controller	
 });


/**
 * Objects 
 */
var theCrew = {
		imgWidth: '89px',
		moveToLeft:	function(obj){
			$(obj).css('background-position', '-' + this.imgWidth);
		},
		removeImgStyles: function(obj){
			$(obj).css('background-position', '0');
		}, 	
		moveBackground: function (obj){
			$(obj).mouseenter(function() {theCrew.moveToLeft(obj);})
			      .mouseleave(function() {theCrew.removeImgStyles(obj);});				
		},
		showEmployee: function(id, scriptUrl){
			$.ajax({
				  url: scriptUrl + "/company/ajaxEmployee/id/" + id,
				  cache: false,
				  success: function(html){
					setOverlay(true, document.getElementById("company").offsetHeight);
					$("#company").append(html);	
					setTimeout('',5000);
					centerBox('#employee-popup');
					$('#employee-popup').fadeIn('slow');
					$(document).keyup(function(e) {
					  if (e.keyCode == 27) {
						  try{
								$('#employee-popup').fadeOut('10', function() {
									$('#employee-popup').remove();
									setOverlay();
								  });
						  	}
						  catch(e){}
						  }   
					});
				  }
				});
		},
		orderBy: function(scriptUrl, category,id){	
			$('.employee').removeClass('active');
			$('.inactive-layer').css({opacity: '0.75'});
			$('.inactive-layer').css({filter: 'alpha(opacity=75);'});
			$('.employee').removeClass('active');
			var filter = '.'+category+id;
			if(category=='showAll'){
				$('.employee').addClass('active');	
			}
			else{
				$(filter).addClass('active');
			}
			
			$('.employee').each(function(e){
				if(!($(this).hasClass('active'))){
					$(this).children('.inactive-layer').fadeIn();					
				}
				else{
					$(this).children('.inactive-layer').fadeOut();
				}
				
				
			});
			
			/**
			 * Use Only for ajax call, due the new funcionality, this ajax call isn't neccessary
			 */
			/*$.ajax({
				  url: scriptUrl + "/company/ajaxReq/"+category+"/"+id,
				  cache: false,
				  success: function(html){
					var imgObj = $('.imgContainer'); 
					$("#employees-content").html(html);
					$('.employee').removeClass('active');
					$('.inactive-layer').css({opacity: '0.75'});
					$('.inactive-layer').css({filter: 'alpha(opacity=75);'});
					if(category=='showAll'){
						$('.employee').addClass('active');	
					}
					else{
						$('.'+category+id).addClass('active');
					}
				  }
				});*/
		},
		setActive: function(){
			$('.crew-filter>a').each(function(){
				$(this).click(function(){
					$('.crew-filter>a').removeClass('active');
					$(this).addClass('active');
				});
			});			
		}
}

var careers = {
		addSelected:function(){
			$('.available-positions>a').each(function(e){
				$(this).click(function(){
					$('.available-positions>a').removeClass('active');
					$(this).addClass('active');
					$('.positions-list>li').removeClass('active');
					$('.positions-list>.pos-' + (e+1)).addClass('active');
					//$('.positions-list>li').hide();
					//$('.positions-list>.pos-' + (e+1)).show('slow');
				});
			})
		},
		setDefault:function(e){
			$('.available-positions>a.pos-' + e).addClass('active');
			$('.positions-list>.pos-' + (e)).addClass('active');
		}
}

/**
 * Global Functions
 */
function centerBox(element){		
	left_ = ($(window).width() - $(element).outerWidth())/2;		
	top_ =  ($(window).height() - $(element).outerHeight())/2;
	if (top_<10){top_=10}
	$(element).css({
	     position:'fixed',
	     left: left_,
	     top: ( top_)
	});
}	

function closeVideoOverlay(){
	$("#video").fadeOut("10", function() {
		$("#video").remove();
		setOverlay();
	 }); 
}	 

function showVideo(iframeUrl){
	var exposedMask = $('#exposeMask').css('display');
	var overlay = $('#overlay').css('display');
	if(exposedMask!='block' && overlay != 'block'){
		setOverlay(true);
		var html = '<div class="simple_overlay_v" id="video" style="z-index:9999; width:480px; height:360px"><div class="close" style="visibility: visible;"><script type="text/javascript">$("#video>.close").click(function(){closeVideoOverlay()}); </script></div></div>';
		var iframe = iframeUrl;
		$('body').append(html);
		$('#video').append(iframe);
		centerBox('#video');				
		$(document).keyup(function(e) {
			  if (e.keyCode == 27) {
				  try{
					  closeVideoOverlay();
				  	}
				  catch(e){}
				  }   
		});
	}
}
		
function setOverlay(show, height){
	if(show){
		$('#overlay').css({opacity: '0.8'});
		$('#overlay').css({filter: 'alpha(opacity=80);'});
		$('#overlay').css({height: document.body.offsetHeight + 'px'});
		$('#overlay').show();
	}
	else{
		$("#overlay").removeAttr("style");
		$('#overlay').hide();
	}
}

function detectWidth() {
	  var myWidth = 0, myHeight = 0;
	  if( typeof( window.innerWidth ) == 'number' ) {
	    //Non-IE
	    myWidth = window.innerWidth;
	    myHeight = window.innerHeight;
	  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
	    //IE 6+ in 'standards compliant mode'
	    myWidth = document.documentElement.clientWidth + 21;
	    myHeight = document.documentElement.clientHeight + 21;
	  } 
	 return myWidth;
	}

function moveContent(){
	var diffWidth = (1600-(detectWidth()));
	document.documentElement.style.overflowX = 'hidden';
	if(diffWidth>0 && diffWidth<630){
		var actualWidthSt_pos = (1600-diffWidth) + 'px';
		$('#page').css('width', actualWidthSt_pos);
	}
	if(diffWidth<=0){
		$('#page').css('width', '1600px');
	}
	if(diffWidth>=630){
		$('#page').css('width', '960px');
		document.documentElement.style.overflowX = 'visible';
	}
}

/**
 * Plugins
 * CSS: Usa por defecto carousel-item-hover.
 * Si se quiere cambiar, sobreescribir por pagina(ej: #vegas .carousel-item-hover).
 */
(function($) {
	$.fn.innerfade = function(options) {
		return this.each(function() {   
			$.innerfade(this, options);
		});
	};
	
	$.innerfade = function(container, options) {
		
		var settings = {
			'speed':            'normal',
			'containerHeight':  '491px',
			'containerWidth':  '800px',
			'containerTop': 0,
			'containerBottom': 0,
			'containerLeft': 0,
			'cssClass': '' //Aņade una nueva clase al div, en caso de requerir algun estilo extra.
		};
		if (options) $.extend(settings, options);
		
		$(container).append('<div class="carousel-item-hover" />').each(function () {
			var div = $('> div.carousel-item-hover', this).css('opacity', 0),
				$img = $(this).find('>img');
			
			$(div).html('<a href="#"></a>');
			if(settings.cssClass !== '') $(div).addClass(settings.cssClass);
			
			$(div).css({
				'position': 'absolute',
				'width': settings.containerWidth,
				'height': settings.containerHeight,
				'left': settings.containerLeft,
				'top': settings.containerTop
			});
			
			//Add Overlay behavior
			var overlay_options	= $('body').data('overlay_options');
			if(typeof overlay_options !== 'undefined') {				
				div.find('>a').attr({
					'rel' : $img.attr('rel'),
					'data-fixed': $img.attr('data-fixed')					
				})
				.overlay(overlay_options);
			}
			
			$(div).hover(function() {
				//on hover
				$(this).stop().fadeTo(settings.speed, 0.5);
			}, function() {				
				//off hover
				$(this).stop().fadeTo(settings.speed, 0);
			});
			$(div).find('>a').click(function() {				
				var $li 			= $(this).parents('li'),
					$img 			= $li.find('>img');
				
				var $overlay_carousel 	= $($img.attr('rel')),
					api					= $overlay_carousel.find('.scrollable').data('scrollable'),
					index				= $li.attr('data-item');
				
				if(api !== undefined && api !== null) {
					api.seekTo(index);
				}
				
				return false;
			});
		});
		
	};
})(jQuery);

/*
 * Mask on hover plugin
 * */
(function($) {
	
	$.fn.maskHover = function() {
		return this.each(function() {
			var base 	= this,
				items 	= $(this).children();
			
			/**
			 * @method: init
			 * */
			this.init = function() {
				this.hoverItems();
			}			
			/**
			 * @method: hoverItems
			 * */
			this.hoverItems = function() {
				items.hover(
					//On
					function() {
						base.maskItem($(this));
						$(this).addClass('on');
					},
					//Off
					function() {
						base.unMaskItem($(this));
						$(this).removeClass('on');
					}
				);
			};			
			/**
			 * @method: maskItem
			 * */
			this.maskItem = function(current) {				
				var $hover = current.find('.hover').attr({'style':'position:relative;'});
				var $mask = $('<div />');
				$mask
					.addClass('mask')
					.css({
						'position': 'absolute',
						'top': '0',
						'left': '0',
						'width': '100%',
						'height': '100%',
						'cursor': 'pointer'
					})
					.appendTo($hover);
			};
			/**
			 * @method: unMaskItem
			 * */
			this.unMaskItem = function(current) {
				current.find('.mask').remove();
			};
			
			this.init();
		});
	}
	
})(jQuery);

if(BrowserDetect.OS=='Mac'){
	var BROWSER = BrowserDetect.OS +  ' ' +BrowserDetect.OS +'-'+ BrowserDetect.browser + ' ' + BrowserDetect.browser + BrowserDetect.version;
}
else{
	var BROWSER = BrowserDetect.browser + BrowserDetect.version;	
}
