var baseUrl = "http://www.airshuttle.es/";
//var baseUrl = "http://localhost/shuttle/site/";

jQuery(document).ready(function()
{
	AS.setActiveMenu();
	swfobject.embedSWF(baseUrl + "/img/" + "banner.swf", 
					   "shuttleBannerP", 
					   "856", "221", 
					   "7", 
					   "../img/expressInstall.swf",
					   false,
					   { wmode: "transparent"}
					   );
	var parts = window.location.pathname.split("/");
	var page = parts[parts.length - 1].toLowerCase();
	switch(page)
	{
		case "behindthescenes.html":
		case "landscapes.html":
		case "streets.html":
		case "roads.html":
		case "indoor.html":
		case "outdoor.html":
		case "beaches.html":
			AS.initGallery();break;
	}
	
});

var AS = {
	menuShown: [],
	setActiveMenu : function(){
		
		var parts = window.location.pathname.split("/");
		var page = parts[parts.length -1].toLowerCase();
		var language = parts.length > 1 ? parts[parts.length - 2].toLowerCase() : "en";
		// set default language
		if (language.length != 2) { language = "en"; }
	
		// remove current language link and first sepearator line
		jQuery("#languageNav li a[href='" + language +"']").parent().remove();
		jQuery("#languageNav li:first").addClass("first");
		jQuery("#languageNav li a").each(function(){
			var a = jQuery(this);
			a.attr("href", "../" + a.attr("href") + "/" + page);
		});
		
		// set current menu
		if(page != "home.html"){
			var current = jQuery("#mainNav a[href='" + page +"']");
			current.parents("li").addClass("current");
		}
		// fix ie li:hover bug
		if(jQuery.browser.msie){
			var elements = jQuery("#mainNav > li");
			elements.mouseover(function(){ 
										// god bless IE7, wich does not hide the menu when the style is changed
										AS.menuShown[this.uniqueID] = true;
										jQuery(this).addClass("hover");
									    jQuery(this).removeClass("unhover");
										});
			elements.mouseout(function(){
										// god bless IE7, wich calcultes the menuUL height according to normal line height(too small)
									   AS.menuShown[this.uniqueID] = false;
									   var target = this;
									   window.setTimeout(function()
										{
										if(!AS.menuShown[target.uniqueID]){
											jQuery(target).addClass("unhover");
											jQuery(target).removeClass("hover");
										}
										},500);
									   });
		}

	},
	aboutUsTabs : ["management", "staff", "clients"],
	showAboutUsTab : function(source, newMenu){
		jQuery("#subMenu a.current").removeClass("current")
		jQuery(source).addClass("current");

		jQuery.each(AS.aboutUsTabs, function(index, item){ jQuery("#" + item).hide(); });
		jQuery("#" + newMenu).show();
	},
	showVideo: function(e, videoFile){

		var flashvars = {config: "{videoFile:'" + baseUrl + "img/video/" + videoFile + ".flv'," + 
								 "autoPlay:true, loop:false, showMenu:false, showFullScreenButton: false, timeDisplayFontColor:'0x666666'}"};
		swfobject.embedSWF(baseUrl + "img/flowplayerDark.swf",
						   "videoReplacement", 
						   "266", "198", 
						   "7", 
						   "../img/expressInstall.swf",
						   flashvars,
						   { wmode: "transparent", loop:false, autoPlay:true }
						   );
		jQuery("#videoList a.current").removeClass("current")
		jQuery(e).addClass("current");
		
		jQuery(".videoComment").hide();
		jQuery("#videoComment" + videoFile.replace(/ /g,"_")).show();

	},
	
	initGallery : function(){
		
		var popup = jQuery("#galleryFullImageContainer");
		popup.dialog(
					{ 
					modal: true, 
					show : "linear",
					draggable:false,
					resizeable:false,
					width:"640px",
					height:"480px",
					autoOpen:false,
					overlay: { 
						opacity: 0.5, 
						background: "black" 
					} 
				});
		
		popup.click(function() { popup.dialog("close");	});
		
		/* add images */
		var container = jQuery("#galleryImages");
		var counter = 0;
		jQuery.each(galleryImages, function(index, item){
											
			if(counter == 0){
				container.append(jQuery('<div style="clear:left;">'));
			}
			container.append(jQuery('<div class="galleryImage"><img src="' + galleryStorage + item+ '" /></div>'));
			if(counter == 2)
			{
				counter = -1;
				container.append(jQuery("</div>"));
			}
			counter++;
			
		});
		
		/* add image navigation */
		AS.maxPage = Math.ceil(galleryImages.length / 9);
		var nav = jQuery("#pageNav");
		nav.append(jQuery('<a onclick="AS.gotoPage(this,0)">&lt;&lt;</a>' + 
						  '<a onclick="AS.gotoPage(this,\'previous\')">&lt;</a>'));
		for(var index = 0; index < AS.maxPage ; index++){
			nav.append(jQuery('<a onclick="AS.gotoPage(this,' + index +')">' + (index + 1) + '</a>'));
		}

		nav.append(jQuery('<a onclick="AS.gotoPage(this,\'next\')">&gt;</a>' + 
						  '<a onclick="AS.gotoPage(this,' + (AS.maxPage-1) + ')">&gt;&gt;</a>'));

		jQuery(jQuery("a", nav)[2]).addClass("current");
		
		jQuery("#galleryImages img").bind("load", function(e){
			var img = jQuery(e.currentTarget || e.target),
			 	container = img.parent(),
			 	maxHeight = 135,
			   	maxWidth = 180,
				width = img.width(),
				height = img.height();
				
			if(width == maxWidth && height == maxHeight) return true;
			
			if(height < maxHeight){
				var margin = (maxHeight - height) / 2 + "px";
				img.css("marginTop", margin);
				img.css("marginBottom", margin);
			}
			if(height > maxHeight){
				var margin = (maxWidth - (maxHeight / height  * width)) / 2 + "px";
				img.css("height", maxHeight + "px");
				img.css("marginLeft", margin);
				img.css("marginRight", margin);
			} 
			if(width > maxWidth){
				var margin = (maxHeight - (maxWidth / width  * height)) / 2 + "px";
				img.css("width", maxWidth + "px");
				img.css("marginTop", margin);
				img.css("marginTop", margin);
			}
			
	   	});
		
		/* dialog handling */
		jQuery("#galleryImages img").bind("click", function(e)
			{
				var instance = this;
				var img = jQuery("#galleryFullImage");
				img.attr("src", "../img/loading.jpg");
window.setTimeout(function(){
				var imageSource = jQuery(instance).attr("src").replace("/thumbnails/","/images/");
				img.attr("src", imageSource);
				
				jQuery("#galleryFullImageContainer").dialog("open");
						   },300);
			});
		
	},
	
	currentPage:0,
	maxPage: 1,
	gotoPage: function(e, index){
		
		if(isNaN(parseInt(index))){
			if(index == "previous" && AS.currentPage > 0 ) {AS.currentPage--;}
			else if(index == "next" && AS.currentPage < AS.maxPage -1 ) {AS.currentPage++;}
		}
		else {AS.currentPage = index;}
		
		jQuery("#pageNav a").removeClass("current");
		jQuery(jQuery("#pageNav a")[AS.currentPage + 2]).addClass("current");

		jQuery("#galleryImages").animate(
			{"top": -AS.currentPage * jQuery("#galleryContainer").height() + "px"},
			"slow",
			"swing");
	},
	
	checkForm: function(){
		jQuery("form").submit();
	}

};

