// Copyright (c) 2009 Drew Wilson. All Rights Reserved. 
// wwww.drewwilson.com
// If you find this code helpful.. throw me some props on your blog/website :)

$.controller.defaults.url = "http://photography.drewwilson.com/index.php/api";

$(function(){
	var notIE = $.support.opacity;
	if(notIE == false){
		$("#topbar").fadeTo(0,0.1);
	}

	var galleries = $.controller.array("gallery/galleries?include=taggings", {gallery_category_id: 6, order: "position", published: "YES"}); 
	
	$("#gallery-list li a").format(function(elem, data){
		$(elem).attr("href", "#"+data.sef_title).text(data.title).addClass(data.sef_title);
		
		$.getJSON($.controller.defaults.url+"/tag/tags?id="+data.taggings[0].tag_id, function(data){
			$(elem).parent().addClass(data.items[0].name.toLowerCase());			
		});
		
		$(elem).click(function(){		
			$("div.fullsize-wrapper, div.fullsize-sh-wrap").fadeOut(300, function(){
	 			$(this).remove();
	 		});		
			var parent = $(elem).parent();
			if(!parent.hasClass("active")){
				$("#thumbs").html('<li><img src="" longdesc="" alt="" /></li>');
					
				$("#gallery-list li, #pricing, #contact").removeClass("active");
				$("#gallery-list li a").not(elem).animate({paddingLeft: "0"},200);
				parent.addClass("active");
				$(elem).animate({paddingLeft: "4px"},200);
				
				var imgs = $.controller.array("gallery/galleries_media_items?include=media_item", {gallery_id: data.id, order: "position"});
				
				$("#thumbs li img").format(function(elem, data){
/*
					var o_width = parseInt(data.media_item[0].width);
					var o_height = parseInt(data.media_item[0].height);
					var aspect = o_height / o_width;
					var n_height = Math.round(300 * aspect);
					if(n_height > 201){
						$(elem).css({"width":"300px", "margin-top": "-" + (n_height - 200) / 2 + "px"});
					} else {
						$(elem).css({"width":"300px"});
					}
*/
					$(elem).attr("src", "http://photography.drewwilson.com/upload/media/" + data.media_item[0].original).attr("longdesc","http://photography.drewwilson.com/upload/media/" + data.media_item[0].original).attr("alt", data.media_item[0].name);
					return true;
				}).load(function(){
					$(this).imgCenter();
				});
				
				$("#thumbs").template(imgs, {success:function(){
					$("#thumbs li img").fullsize({
						leftOffset: 227,
						start: function(){
							$("#thumbs li").fadeTo(200, 0.1);
						},
						end: function(){
							$("#thumbs li").fadeTo(300, 1);
						},
						useIcon: false
					}).hover(function(){
						$(this).stop(true,false).fadeTo(300, 0.8);
					}, function(){
						$(this).stop(true,false).fadeTo(300, 1);
					});					
					if(notIE == false){
						$("#thumbs li img").show();
					}
				}});
				
			}
		});
		return true;
 	});
 	
	$("#gallery-list").template(galleries, {success:function(){
		var anchor = $.url.attr("anchor");
		if(anchor){
			$("#gallery-list li a."+anchor).trigger('click');
		} else {
			$("#gallery-list li:first a").trigger('click');
		}
	}});
	
	$("#contact").click(function(){
		$(this).addClass("active");
		$("div.fullsize-wrapper, div.fullsize-sh-wrap").fadeOut(300, function(){
	 		$(this).remove();
	 	});
		$("#gallery-list li a").animate({paddingLeft: "0"},200);
		$("#gallery-list li, #pricing").removeClass("active");
		$.getJSON($.controller.defaults.url+"/page/pages?id=3", function(data){
			$("#thumbs").html(data.items[0].text);
			
			$("form input[name=name]").focus(function(){
				if($(this).val() == "Your Name"){ 
					$(this).val(""); 
				}
			}).blur(function(){
				if($(this).val() == "") { 
					$(this).val("Your Name"); 
				}
			});
			$("form input[name=email]").focus(function(){
				if($(this).val() == "Your Email"){ 
					$(this).val(""); 
				}
			}).blur(function(){
				if($(this).val() == "") { 
					$(this).val("Your Email"); 
				}
			});
			$("form input[name=date]").focus(function(){
				if($(this).val() == "Event Date"){ 
					$(this).val(""); 
				}
			}).blur(function(){
				if($(this).val() == "") { 
					$(this).val("Event Date"); 
				}
			});
			$("form input[name=location]").focus(function(){
				if($(this).val() == "Event Location"){ 
					$(this).val(""); 
				}
			}).blur(function(){
				if($(this).val() == "") { 
					$(this).val("Event Location"); 
				}
			});
			$("form textarea").focus(function(){
				if($(this).val() == "Comments"){ 
					$(this).val(""); 
				}
			}).blur(function(){
				if($(this).val() == "") { 
					$(this).val("Comments"); 
				}
			});
			$("form input.wedding-check").click(function(){
				if($(this).is(":checked")){
					$("div.wedding-options").slideDown(100);
				} else {
					$("div.wedding-options").slideUp(100);
				}
			});
			
			$("#thumbs form a.submit").click(function(){
				$("#thumbs form").submit();
				return false;
			});
			
			$("#thumbs form").submit(function(){
				if($("form input[name=name]").val() == "" || $("form input[name=name]").val() == "Your Name"){
					$("#thumbs div.message").text("Please enter Your Name.");
				} else if($("form input[name=email]").val() == "" || $("form input[name=email]").val() == "Your Email"){
					$("#thumbs div.message").text("Please enter Your Email.");
				} else if($("form input[name=comments]").val() == "" || $("form input[name=comments]").val() == "Comments"){
					$("#thumbs div.message").text("Please enter add some Comments.");
				} else {
					$.ajax({
					   type: "POST",
					   url: "http://photography.drewwilson.com/contact.php",
					   data: $("#thumbs form").serialize(),
					   success: function(msg){
					   	$("#thumbs div.message").text(msg);
					   }
					 });
				}
				return false;
			});			
		});
		return false;
	});
	$("#pricing").click(function(){
		$(this).addClass("active");
		$("div.fullsize-wrapper, div.fullsize-sh-wrap").fadeOut(300, function(){
	 		$(this).remove();
	 	});		
		$("#gallery-list li a").animate({paddingLeft: "0"},200);
		$("#gallery-list li, #contact").removeClass("active");
		$.getJSON($.controller.defaults.url+"/page/pages?id=4", function(data){
			var str = data.items[0].text;
			str = str.replace(/\{\{\$base_url\}\}/gi, "http://photography.drewwilson.com/");
			$("#thumbs").html(str);		
			$("#thumbs a.contact").click(function(){
				$("#contact").trigger('click');
				return false;
			});
		});
		return false;
	});
	
});