$(document).ready(function() {	
	//header
	var enews_default = "Your Email Address";
	
	$("#qjudrr-qjudrr").val(enews_default);
	
	$("#qjudrr-qjudrr").focus(function() {
		if($(this).val() == enews_default) {
			$(this).val('');
		}
	});
	
	$("#qjudrr-qjudrr").blur(function() {
		if($(this).val() == '') {
			$(this).val(enews_default);
		}
	});

	//work
	$("#work .grid .item a").colorbox({innerWidth: "75%", innerHeight: "75%", scalePhotos: true, transition: "none", close: "", next: "", previous: "", current: ""});
	
	//store
	var thumb_width = $(".product-thumbnails li").length * 50;
	$(".product-thumbnails").css("width", thumb_width);
	
	$(".product-thumbnails li a").each(function() {
		$(this).click(function() {
			$("#product_image").attr("src", $(this).attr("rel"));
		});
	});
	
	//videos
	function loadVideo(ID) {
		$("#videos .player").html('<object width="640" height="385"><param name="movie" value="http:\/\/www.youtube.com\/v\/' + ID + '&showsearch=0&f=gdata_videos"><\/param><param name="allowFullScreen" value="true"><\/param><param name="allowscriptaccess" value="always"><\/param><embed src="http:\/\/www.youtube.com\/v\/' + ID + '&showsearch=0&f=gdata_videos" type="application\/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="640" height="385"><\/embed><\/object>');
	}
	
	$("#videos .thumbnails .item a").each(function() {
		$(this).click(function() {
			loadVideo($(this).attr("rel"));
		});
	});
		
	var videos_total = $("#videos .thumbnails .item").length;
	var videos_limit = 5;	
	var videos_pages = Math.ceil(videos_total/videos_limit);
	var videos_current_page = 1;
	
	if (videos_pages > 1) {
		$('.thumbnails-wrapper').css("width", (videos_total * 150));
		$('#videos .next').click(function() {
			nextVideoPage();
		});
		$('#videos .prev').click(function() {
			prevVideoPage();
		});
	}
	
	function nextVideoPage() {
		var next_page = videos_current_page + 1;
		var adjusted_margin = -((next_page - 1) * 750);
		if (next_page <= videos_pages) {
			$('.thumbnails-wrapper').animate({marginLeft: adjusted_margin}, 'normal');
			videos_current_page = next_page;
		}
	}
	function prevVideoPage() {
		var prev_page = videos_current_page - 1;
		var adjusted_margin = -((prev_page - 1) * 750);
		if (prev_page > 0) {
			$('.thumbnails-wrapper').animate({marginLeft: adjusted_margin}, 'normal');
			videos_current_page = prev_page;
		}
	}
	
	//contact
	$("#contact_submit").click(function() {
		submitContact();
	});
	$("#contact_name").focus(function() {
		var value = $(this).val()
		if (value == "Your Name") {
			$(this).val("");
		}
	});
	$("#contact_name").blur(function() {
		var value = $(this).val()
		if (value == "") {
			$(this).val("Your Name");
		}
	});
	$("#contact_email").focus(function() {
		var value = $(this).val()
		if (value == "Email Address") {
			$(this).val("");
		}
	});
	$("#contact_email").blur(function() {
		var value = $(this).val()
		if (value == "") {
			$(this).val("Email Address");
		}
	});
	$("#contact_message").focus(function() {
		var value = $(this).val()
		if (value == "Message") {
			$(this).val("");
		}
	});
	$("#contact_message").blur(function() {
		var value = $(this).val()
		if (value == "") {
			$(this).val("Message");
		}
	});
	
	function submitContact() {
		$("#contact_submit").attr("src", "/_images/contact_submit_inactive.gif");
		
		var name = $("#contact_name").val();
		var email = $("#contact_email").val();
		var message = $("#contact_message").val();
		
		//create data query string
		var q = "action=contact&name="+name+"&email="+email+"&message="+message;
		
		//send contact to database
		$.ajax({type: "POST", url: "/_helpers/jquery.contact.php", data: q, dataType: "json",
			success: function (data, textStatus) {
				if (data.errors) {					
					$("#contact_submit").attr("src", "/_images/contact_submit.gif");
					
					//display errors
					for (i = 0; i < data.errors.length; i++) {
						var ref = data.errors[i].input + "_error";
						if ($("#"+ref)) {
							$("#"+ref).remove();
							$("#"+data.errors[i].input).removeClass("highlight");
						}
						if (data.errors[i].display.length > 0) {						
							$("#"+data.errors[i].input).after('<div id="'+ref+'" class="error">' + data.errors[i].display + '</div>');
							$("#"+data.errors[i].input).addClass("highlight");
							$("#"+ref).hide();
							$("#"+ref).slideDown("fast");		
						}
					}
					
					//stop
					return false;
				} else {			
					//clear errors, show confirmation
					$("#contact_form div.error").remove();	
					$("#contact_form input").removeClass("highlight");
					$("#contact_form textarea").removeClass("highlight");
					$("#contact_form").html('<p class="success">Your message has been delivered&mdash;thanks!</p>');				
				}
			}
		});
	}
});

//pop up window
function popWindow(src, width, height) {
	var w = 800, h = 600;
	if (screen.width || screen.height) {
		w = screen.width;
		h = screen.height;
	}
	var leftPos = (w-width)/2;
	var topPos = (h-height)/2;
	window.open(''+src+'','','width='+width+',height='+height+',toolbar=no,status=no,menubar=no,scrollbars=yes,resizeable=no,top=' + topPos + ',left=' + leftPos);
	void(0);
}

Cufon.replace('.knockout');
