$(function() {
	$("#buttons .port").mouseenter(function() {
		$(this).animate({
			width: "+=4"
		}, 200);
	}).mouseleave(function() {
		$(this).animate({
			width: "-=4"
		}, 200);
	});
	
	// Slideshow
	slideShow.run();
	slideShow2.run();
	
	$("#prev, #next").click(function(e) {
		e.preventDefault();
		
		if ($(this).attr("id") == "next") {
			slideShow.move(true, true);
		} else {
			slideShow.move(false, true);
		}
	});
	
	$("#slideshow_container").mouseenter(function() {
		slideShow.stop();
		
		This = $(this);
		Img = This.find("img:visible");
		
		Hover = $("<a>");
		Hover.attr("src", "#").attr("class", "hover");
		Hover.css("opacity", 0);
		
		Alt = Img.attr("alt").split("-");
		Alt = $.trim(Alt[0])+"<small>"+$.trim(Alt[1])+"</small>";
		
		Info = $("<div>");
		Info.html(Alt).slideUp();
		Hover.append(Info);
		
		This.append(Hover);
		Hover.animate({
			opacity: 1
		}, 400);
		Info.slideDown(200);
		
	}).mouseleave(function() {
		Hover = $(this).find("a.hover");
		Hover.animate({
			opacity: 0
		}, 300, function() {
			$(this).remove();
		});
		
		slideShow.timer = setTimeout("slideShow.run()", 2000); // Start the slideshow
	});
	
	$(".hover").live("click", function() {
		slideShow.stop();
		
		Img = This.find("img:visible");
		ImgSrc = Img.attr("src").split(".");
		ImgSrc = ImgSrc[0]+"_big."+ImgSrc[1];
		
		Alt = Img.attr("alt").split("-");
		Alt = $.trim(Alt[0])+"<small>"+$.trim(Alt[1])+"</small>";
		
		Overlay = $("#overlay");
		Overlay.find("h4").html(Alt);
		
		$("#img_container > img").each(function(index, e) {
			if($(this).attr("src") == ImgSrc) {
				slideShow2.next = index;
				slideShow2.move(true);
			}
		});
		
		$("#lights_off").css("display", "block").animate({
			opacity: 0.92
		}, 300, function() {
			$("#overlay").show(300);
		});
	});
	$(".index #lights_off").click(function() {
		$(this).animate({
			opacity: 0
		}, 300, function() {
			$(this).hide();
		});
		$("#overlay").hide(300);
		$("#img_container img").hide();
	});
	$("#img_container").scrollview({
	    grab:"images/openhand.cur",
	    grabbing:"images/closedhand.cur"
	  });
	$("#overlay a.next").click(function(e) {
		e.preventDefault();
		
		slideShow2.move(true);
	});
	$("#overlay a.prev").click(function(e) {
		e.preventDefault();
		
		slideShow2.move();
	});
	
	// Contact Form
	// Fake Select
	$("#select li.item").click(function() {
		This = $(this);
		Input = $("#budget");
		
		// If the item clicked is already selected, return
		if (This.hasClass("selected")) {
			return;
		}
		
		// Move this item to the top and mark as selected
		This.css({
			zIndex: 8
		}).animate({
			top: 0
		}, 200, function() {
			$("#select li.item").removeClass("selected");
			This.addClass("selected").css({zIndex: "4"});
			
			// Change input#budget value
			Input.val("$"+This.html());
		});
	});
	$("#select").click(function() {
		// Toggle class, CSS3 takes care of the transitions
		$(this).toggleClass("active");
	});
	// Fake Checkbox
	$("#contact_form #copy").click(function() {
		$(this).toggleClass("active");
		
		if ($(this).hasClass("active")) {
			$("#contact_form #copy input")[0].checked = true;
		} else {
			$("#contact_form #copy input")[0].checked = false;
		}
	});
	$("#contact_form").submit(function (e){
		e.preventDefault();
		
		// Send form by AJAX
		This = $(this);
		Contact_form = $("#contact_form > form");
		
		$.post(Contact_form.attr("action"), Contact_form.serialize(), function(data) {
			if (data == "Email Sent.") {
				Contact_form.html(data);
			} else {
				Div = $("<div>");
				Div.addClass("form");
				Div.html(Contact_form.html());
				Contact_form.html(Div);
				Div.hide(300);
				
				Div2 = $("<div>");
				Div2.addClass("data");
				Div2.html(data);
				Div2.append('<br /><a href="#" id="return">Return</a>');
				Contact_form.append(Div2);
			}
		});
	});
	$("#return").live("click", function(e) {
		e.preventDefault();
		
		$("#contact_form > form").find("div.form").show(300);
		$("#contact_form > form").find("div.data").hide(300);
	});
	
	// Footer Tab
	$("#footer_tab").click(function(e) {
		e.preventDefault();
		
		var icon = $("#footer_tab .icon"),
		about = $("#about_us"),
		iconPositions = [0, 1, 2, 3, 4, 5, 6],
		iconPositionsReverse = [13, 12 ,11, 10, 9, 8, 7];
		closeSpeed = Math.round(200 / iconPositions.length),
		run = function() {			
			var positions = iconPositions.slice(0);
			
			if (about.css("display") == "block") {
				var positions = iconPositionsReverse.slice(0);
				console.log(positions + " visible");
			}
			
			$(positions).each(function(i, position) {
				var offset = position * -20;

				setTimeout(function() {
					icon.css({backgroundPosition: "0 " + offset + "px"});
				}, i * closeSpeed);
			});
		};
		
		run();
		$("#about_us").slideToggle(200);
	});
	
	// Portfolio Contact Us Button
	$(".portfolio .contact").click(function(e) {
		e.preventDefault();
		
		$("#lights_off").animate({
			opacity: 0.75
		}, 300).css("display", "block");
		
		$("#contact_form").slideDown(300);
	});
	$(".portfolio #lights_off").click(function() {
		Lo = $(this);
		Lo.animate({
			opacity: 0
		}, 300, function() {
			Lo.css("display", "none");
		});
		$("#contact_form").slideUp(300);
	});
});

slideShow = {
	imgs: $("#slideshow_container > img"),
	next: 1,
	firstRun: true,
	run: function() {
		if (this.firstRun == true) {
			// If this is the first run
			$(this.imgs[0]).fadeIn(200);
			this.firstRun = false;
		} else {
			this.move(true);
		}
		
		this.timer = setTimeout("slideShow.run()", 2000);
	},
	move: function(next, timeout) {
		clearTimeout(this.timer);
		if (next) {
			// Move to the next image
			$("#slideshow_container > img:visible").fadeOut(500);
			$(this.imgs[this.next]).fadeIn(500);
			this.next = (this.next+1);
		} else {
			// Move to the previous image
			prev = this.next-2;
			if (this.next <= 0) {prev = (this.imgs.length-2)}
			if (prev < 0) {prev = (this.imgs.length-1)}
			
			$("#slideshow_container > img:visible").fadeOut(500);
			$(this.imgs[prev]).fadeIn(500);
			
			this.next = (prev+1);
		}
		
		this.verifyNext();
		
		if (timeout) {
			this.timer = setTimeout("slideShow.run()", 2000);
		}
	},
	verifyNext: function() {
		if (this.next > (this.imgs.length-1)) {
			this.next = 0;
		}
	},
	stop: function() {
		clearTimeout(this.timer); // Stop the slideshow
	}
};

slideShow2 = {
	imgs: $("#img_container > img"),
	next: 1,
	firstRun: true,
	run: function() {
		if (this.firstRun == true) {
			// If this is the first run
			$(this.imgs[0]).fadeIn(200);
			this.firstRun = false;
		} else {
			this.move(true);
		}
		
		this.timer = setTimeout("slideShow2.run()", 2000);
	},
	move: function(next, timeout) {
		clearTimeout(this.timer);
		if (next) {
			// Move to the next image
			$("#img_container > img:visible").fadeOut(500);
			$(this.imgs[this.next]).fadeIn(500);
			this.next = (this.next+1);
		} else {
			// Move to the previous image
			prev = this.next-2;
			if (this.next <= 0) {prev = (this.imgs.length-2)}
			if (prev < 0) {prev = (this.imgs.length-1)}
			
			$("#img_container > img:visible").fadeOut(500);
			$(this.imgs[prev]).fadeIn(500);
			
			this.next = (prev+1);
		}
		
		this.verifyNext();
		
		if (timeout) {
			this.timer = setTimeout("slideShow2.run()", 2000);
		}
		
		console.log(this.next);
	},
	verifyNext: function() {
		if (this.next > (this.imgs.length-1)) {
			this.next = 0;
		}
	},
	stop: function() {
		clearTimeout(this.timer); // Stop the slideshow
	}
};

function slideShow() {
	imgs = $("#slideshow_container > img");
	current = 0;
	next = current+1;
	newNext = current+2;
	
	if (next > imgs.length) {
		next = 0;
		newNext = 1;
	}
	
	setTimeout("slideShow()", 5000);
}
