var on = 0;

$(document).ready(function() {

	var speed = 300;
	$("#mainNavi").lavaLamp({
        fx: "backout",
        speed: 1400,
        click: function(event, menuItem) {
			return false;
        }
    });
	

	$("#content").css("left","-630px").animate({left:"-630px"},speed).animate({left:"+=630"},speed);
	$("#contentAdd").css("left","+950px").animate({left:"+950px"},speed).animate({left:"-=290"},speed);
	
	$("#mainNavi li a").click(function() {
		
		window.clearTimeout(on);
		var page = this.text;
		
		$("#contentAdd").animate({left:"+=290"},speed,function() {
			$(this).empty();
		});

		$("#content").animate({left:"-=630px"},speed,function() {
			$(this).empty(); 
			
			$("#background").animate({opacity:"0"},speed/2,function() {
				
				$.post("../php/getRandomBackground.php", function(backgroundImage) {
					$("#background").css("background","url(../images/back/"+backgroundImage+") no-repeat left top");						
				});	
				
				$(this).css("background","url(../images/back/contentBack"+page+".jpg) no-repeat left top");
				
				$.ajax({
					url: "./html/" + page.toLowerCase() + ".php",
					cache: false,
					success: function(html) {
						$.ajax({
							url: "./html/add/add" + page + ".php",
							cache: false,
							success: function(addHtml) {
								
							$("#background").stop().animate({opacity:"1"},speed*3,function() {
									
									if(page!="Contact" && page!="Extra" && page!="Disclaimer") {
										$("#contentAdd").append(addHtml);
										$("#contentAdd").show();
									} else {
										$("#contentAdd").hide();
									}
									
									
									$(".flyer").hide();
										if (page=="Home") $(".flyer").show();
									
									$("#contentAdd").animate({left:"-=290"},speed,function() {
										
										$("#content").append(html).animate({left:"+=630"},speed);

										if (page=="Equipment") getFlyer("equipment/");
										if (page=="Dates") getFlyer("flyer/");
										if (page=="Clients") getFlyer("clients/");
										
										if (page=="Media") {
											
											$("#subNavi li a").attr("onClick","return false;").click(function() {
												var media = this.href;
												media = media.split('=');
												$("#content").animate({left:"-=630px"},speed,function() {
													$("#content .media").empty();
													$.ajax({
														url: "../media/" + media[2] + ".html",
														cache: false,
														success: function(html) {
															$("#content .media").append(html);
															$("#content").animate({left:"+=630"},speed*2);
														}
													});
												});
											});
										}
									});
								});								
							}
						});
					}
				});
			
			});
		});

	});
});

function getFlyer(path) {
	$.post("../php/getFlyer.php", {imagePath:"../images/"+path}, function(data) {
		data = data.split(',');
		rotateFlyer(data,0,path);
	});		
}

function rotateFlyer(flyer,i,source) {
	path = source;
	data = flyer;
	index = i;
	$(".flyer").hide().attr("src","./images/" + path + data[index]).fadeIn(1500);
	index ++;
	if (index > data.length-1) index = 0;
	on = window.setTimeout('rotateFlyer(data,index,path)',5000);
}

function stopRotate() {
	window.clearTimeout(on);	
}
