/* Vimeo */
$(function() {
	$(".playIcon").css("opacity",0.4).mouseover(function() {
		$(this).animate({"opacity":1});
	}).mouseout(function() {
		$(this).animate({"opacity":0.2});
	}).click(function() {
		$(".video_player").remove();
		var player = $("<div class='video_player'><div class='close'><a class='close'></a></div><div class='video'></div></div>").hide().appendTo(document.body).fadeIn("fast");
		var video_id = $(this).parents(".item:first").attr("rel");
		setTimeout(function() {
			player.find(".video").append("<iframe src=\"http://player.vimeo.com/video/"+video_id+"?autoplay=true\" width=\"600\" height=\"339\" frameborder=\"0\"></iframe>");
		},1000);
		player.css({
			"top": $(window).height()/2 - player.outerHeight()/2 + $(window).scrollTop(),
			"left": $(window).width()/2 - player.outerWidth()/2  + $(window).scrollLeft()
		});
		player.find(".close a").click(function() {
			player.remove();
		})
		return false;
	})
});
/* .photoContainer */
$(function() {
	$(".photoContainer").each(function() {
		var container = $(this);
		var z = 20;
		/* Click handler */
		var items = $(this).find("img[rel], a[rel]").click(function() {
			var clicked = this;
			var selected = "";
			var firstShow = true;
			// remove existing windows
			$(document.body).children(".picture_player").remove();
			
			// add new window
			var modal = $("<div class='picture_player'></div>").appendTo(document.body);
			var close = $("<a href='javascript:;' class='close'></a>").appendTo(modal).click(function() {
				modal.fadeOut(function() {
					modal.remove();
				})
				return false;
			});
			var info = $("<div class='picture_info'><span></span></div>").appendTo(modal);
			var next = $("<div class='arrow arrow_next'></div>").appendTo(modal);
			var back = $("<div class='arrow arrow_back'></div>").appendTo(modal);
			var show = function(src, i) {
				selected = src;
				var img = $("<img />").css("z-index",z++).hide().load(function() {
					if(firstShow) {
						modal.append(img.show());
					} else {
						modal.find("img").fadeOut(function() {
							$(this).remove();
						});
						modal.append(img.fadeIn())
					}
					var top = $(window).height()/2 - $(this).height()/2 + $(window).scrollTop();
					modal[firstShow?'css':'animate']({
						width: $(this).width(),
						height: $(this).height(),
						top: top < $(window).scrollTop() + 20 ? $(window).scrollTop() + 20 : top,
						left: $(window).width()/2 - $(this).width()/2 + $(window).scrollLeft()
					}).show();
					firstShow = false;
					info.html("<span>"+i+" of "+items.size()+"</span>");
					$(next).add(back).css("height",$(this).height()-40);
				});
				img.attr("src",src);
			};
			show($(this).attr("rel"), function() {
				var i = 0;
				var result = 1;
				items.each(function() {
					i++;
					if(this == clicked) {
						result = i;
					}
				})
				return result;
			}());
			
			next.click(function() {
				var next = false;
				var done = false
				items.each(function(m,k) {
					if(done) return false;
					if(selected == $(this).attr("rel")) {
						next = true;
					} else if(next == true) {
						show($(this).attr("rel"),m+1)
						done = true
					}
				});
				if(!done) {
					show(items.first().attr("rel"),1)
				}
			});
			
			back.click(function() {
				var last = "";
				var done = false
				items.each(function(m,k) {
					if(done) return false;
					if(selected == $(this).attr("rel") && m > 0) {
						show(last,m)
						done = true
					} else {
						last = $(this).attr("rel");
					}
				});
				if(!done) {
					show(items.last().attr("rel"),items.size())
				}
			});
			
			return false;
		});
		
		// zoom icon, if required
		if(container.hasClass("zoomIcon")) {
			items.each(function() {
				var self = this;
				var zoomy = false;
				$(self).mouseover(function() {
					if(zoomy) return false;
					zoomy = $("<a class='zoom_icon'>&nbsp;</a>").appendTo(document.body);
					var hideTimer = false;
					zoomy.css({
						top: $(this).offset()['top'],
						left: $(this).offset()['left'],
						width: $(this).width(),
						height: $(this).height()
					}).click(function() {
						$(self).click();
					});
				});
			});
		}
	});
});

/* Portfolio */
$(function() {
	$("#portfolio_full").each(function() {
		$(this).find(".thumbnails .thumb a").click(function() {
			$("#portfolio_full .mainImage img").attr("src",$(this).attr("rel"));
			return false;
		});
	});
	$(".dropdown select").change(function() {
		$(this).css("opacity",0.01);
		val = this.selectedIndex;
		$(this).parent().find(".dropdown_label").html($(this).children("option:nth("+val+")").html())
	}).change();
})
$(function() {
	$("input[placeholder]").focus(function() {
		if(this.value == this.getAttribute("placeholder")) {
			this.value = "";
		}
		$(this).removeClass("placeholderInput");
	}).blur(function() {
		if(this.value == "") {
			this.value = this.getAttribute("placeholder");
			$(this).addClass("placeholderInput");
		}
	}).blur();
	$("form").submit(function() {
		$(this).find("input").each(function() {
			if(this.value == this.getAttribute("placeholder")) {
				this.value = "";
			}
		})
	});
})
