$(document).ready(function () {
	//if(navigator.plugins["Shockwave Flash"]) {
		embedFlash();
		window.setTimeout(removeFlashBG, 4000);
	//} else {
	//	$('#flash_holder').remove();
	//}
	
	if($(".lightbox").length > 0) {
		$("a.lightbox").lightbox();
	} 
		
	window.setTimeout(Content.toggle, 200);
	Submenu.init();
	Gallery.init();
	Interior.init();
	Util.initSlideables();
});

Content= {
	toggle: function() {
		if($("#page .content .text").html().length>15) {
			var bottom= parseInt($("#page .content").css("bottom"));
			var height= $("#page .content").height()
		
			if(bottom==0) {
				$("#page .content").animate({
					bottom: "-"+height+"px"
				}, 400, "swing");
			} else {
				$("#page .content").animate({
					bottom: "0px"
				}, 400, "swing");
			}
		}
	}
}

Submenu= {
	init: function() {
		var lang= $("#language").html();
		$.ajax({
			url: '/'+lang+'/helper/aircrafts',
			type: 'GET',
			dataType: 'html',
			timeout: 5000,
			success: function(html) {
				$("#submenu").html(html);
				if($(".navigation .charter").hasClass('active')){
					$("#submenu").show();
					var current = $('h1').attr('id');
					$("a."+current).addClass('active');					
				}else{
					$("#submenu").hover(function() {
						$("#submenu").show();
					}, function() {
						$("#submenu").hide();
					});
					$(".navigation .charter").hover(function() {
						$("#submenu").show();
					}, function() {
						$("#submenu").hide();
					});
				}
					}
				});
		
		
		

	}
}

Gallery= {
	current: 1,
	eternity: null,
	sleep: 2000,
	counter: 0,
	init: function() {
		Gallery.counter= $("#mygallery .thumbnails img").size();
		$("#mygallery .thumbnails img").click(function() {
			$("#mygallery .gallery img").hide();
			var myclass= $(this).attr("class");
			Gallery.current= myclass.replace(/image/, '');
			$("#mygallery .gallery ."+myclass).show().css("margin-left", (766-$("#mygallery .gallery ."+myclass).width())/2).css("margin-top", (446-$("#mygallery .gallery ."+myclass).height())/2);
			window.clearTimeout(Gallery.eternity);
		});
		$("#mygallery .gallery img:first").css("margin-left", (766-$("#mygallery .gallery img:first").attr("width"))/2).css("margin-top", (446-$("#mygallery .gallery img:first").attr("height"))/2);
		$("#mygallery .close").click(function() {
			Gallery.hide();
		});
		$("#shadow").click(Gallery.hide);
	},
	show: function() {
		$("#shadow").show();
		$("#mygallery").show();
		Gallery.eternity= window.setTimeout(function() {
			Gallery.next();
		}, Gallery.sleep);
	},
	next: function() {
		Gallery.current++;
		if(Gallery.current>10 || Gallery.current>Gallery.counter) {
			Gallery.current= 1;
		}
		$("#mygallery .gallery img").hide();
		$("#mygallery .gallery .image"+Gallery.current).show().css("margin-left", (766-$("#mygallery .gallery .image"+Gallery.current).attr("width"))/2).css("margin-top", (446-$("#mygallery .gallery .image"+Gallery.current).attr("height"))/2);
		window.setTimeout(function() {
			Gallery.next();
		}, Gallery.sleep);
	},
	hide: function() {
		$("#shadow").hide();
		$("#mygallery").hide();
		window.clearTimeout(Gallery.eternity);
	}
}

Interior= {
	current: 1,
	eternity: null,
	sleep: 500,
	counter: 0,
	init: function() {
		Interior.counter= $("#myinterior .thumbnails img").size();
		$("#myinterior .thumbnails img").click(function() {
			$("#myinterior .gallery img").hide();
			var myclass= $(this).attr("class");
			Interior.current= myclass.replace(/image/, '');
			$("#myinterior .gallery ."+myclass).show().css("margin-left", (766-$("#myinterior .gallery ."+myclass).width())/2).css("margin-top", (446-$("#myinterior .gallery ."+myclass).height())/2);
			window.clearTimeout(Interior.eternity);
		});
		$("#myinterior .gallery img:first").css("margin-left", (766-$("#myinterior .gallery img:first").attr("width"))/2).css("margin-top", (446-$("#myinterior .gallery img:first").attr("height"))/2);
		$("#myinterior .close").click(function() {
			Interior.hide();
		});
		$("#shadow").click(Interior.hide);
	},
	show: function() {
		$("#shadow").show();
		$("#myinterior").show();
		Interior.eternity= window.setTimeout(function() {
			Interior.next();
		}, Interior.sleep);
	},
	next: function() {
		Interior.current++;
		if(Interior.current>10 || Interior.current>Interior.counter) {
			Interior.current= 1;
		}
		$("#myinterior .gallery img").hide();
		$("#myinterior .gallery .image"+Interior.current).show().css("margin-left", (766-$("#myinterior .gallery .image"+Interior.current).attr("width"))/2).css("margin-top", (446-$("#myinterior .gallery .image"+Gallery.current).attr("height"))/2);
		window.setTimeout(function() {
			Interior.next();
		}, Interior.sleep);
	},
	hide: function() {
		$("#shadow").hide();
		$("#myinterior").hide();
		window.clearTimeout(Interior.eternity);
	}
}

Util = {
	initSlideables: function() {
		$("#page .slideable").each(function() {
			var slideable= $(this);
			var classes= slideable.attr("class").split(" ");
			var height= slideable.height();
			for(var index=0; index<classes.length; index++) {
				if(classes[index].match("height")) {
					height= classes[index].replace(/height/g, '');
				}
			}
			if(height<slideable.height()) {
				/* set slideable */
			    slideable.html('<div class="slideable_content" style="width: '+(slideable.width()-10)+'px;">'+slideable.html()+'</div><div class="slideable_slider" style="height: '+height+'px;"></div>');	
				var overflow= $('.slideable_content', slideable).height()-height; 
				$(".slideable_slider", slideable).slider({ 
					slide: function (event, ui) {
					 	var top= parseFloat(overflow*ui.value/100);
						$(".slideable_content", slideable).css('margin-top', '-' + top + 'px');
					}, 
					stop: function (event, ui) {
						var top= overflow*ui.value/100;
						$(".slideable_content", slideable).animate({ 'margin-top' : '-' + top + 'px' }, 500, 'linear');
					}
				});
				
				/* set frame */
				slideable.css("width", slideable.width()).css("height", height);
			}
		});
	}
}

function embedFlash() {
	var h = 650;
	var w = 1000;
	var flashvars = {
		h:		h,
		w:		w,
		assetPath: "/_flash/assets/"
	};	
	var params = {
		wmode: "transparent"
	};
    swfobject.embedSWF("/_flash/ajet.swf", "flash", w, h, "9.0.0", "_js/swfobject/expressInstall.swf", flashvars, params );
}

function removeFlashBG() {
	$('#flash_holder').css("background-color","transparent");
}

function removeFlash() {
	$('#flash_holder').remove();
}

function showPage() {
	//alert('show HTML Page!');
	$('#page').css("display","inherit");	
}

