
$(document).ready(function() {
	/*
	var vH=$('#splash-container').height();
	var vW=$('#splash-container').width();
	var vT=$('#splash-container').offset().top;
	var vL=$('#splash-container').offset().left;
	$('#splash-container').mousemove(function(e){
			var ypos = e.pageY - vT;
			var xpos = e.pageX - vL;
			var y = Math.round(ypos / vW*100);
			var x = Math.round(xpos / vH*100);
			$('#splash-container').css({backgroundPosition: x + '% ' + y + '%'});
	});
	*/
													 
	$("#leftmenu li").mouseenter(function() {
		$(this).find("ul:hidden").slideDown(700);
	});
	$("#leftmenu").mouseleave(function() {
		$(this).find("li").not(".menu_current").find("ul").slideUp(700);
	});
	if($("#doc_options").length > 0) {
		$("input[name='product_type']").click(function() {
			$("#doc_options:hidden").slideDown(500);
			if($("#product_type_link").is(":checked")) {
				$("#doc_type_url").show();
				$("#doc_type_doc").hide();
				$("#doc_type_related").hide();
			}
			if($("#product_type_doc").is(":checked")) {
				$("#doc_type_url").hide();
				$("#doc_type_doc").show();
				$("#doc_type_related").hide();
			}
			if($("#product_type_related").is(":checked")) {
				$("#doc_type_url").hide();
				$("#doc_type_doc").hide();
				$("#doc_type_related").show();
			}
		});
	}
	
	if($("input[name='appointmenttype']").length > 0) {
		$("input[name='appointmenttype']").change(
			function () {
				if($("input[name='appointmenttype']:checked").val() == "home") {
					$("#homeinfo").show("slow");
				} else {
					$("#homeinfo").hide("slow");
				}
			}
		);
	}
	$(".searchSubButton").click(
		function() {
			$(this).parents('form').submit();
		}
	);
	if($("#next").length > 0) {
		$("#next").click(function() {
			$("#next_submit_progress").show();
		});
	}
	if($(".date-pick").length > 0) {
		Date.firstDayOfWeek = 7;
		Date.format = 'mm/dd/yyyy';
		$('.date-pick').datePicker();
	}
	if($("#contact-form").length > 0) {
		// validate signup form on keyup and submit
		var validator = $("#contact-form").validate({
			rules: {
				name: 'required',
				email: {
					required: true,
					email: true
				},	
				comments: "required"
			},
			messages: {
				name: "Enter your name",			
				email: {
					required: "Please enter a valid email address",
					minlength: "Please enter a valid email address"				
				},
				comments: "Enter a comment"
			},
			// the errorPlacement has to take the table layout into account
			errorPlacement: function(error, element) {
				element.after( error );
			},		
			// set this class to error-labels to indicate valid fields
			success: function(label) {
				// set &nbsp; as text for IE
				label.html('&nbsp;').addClass('checked');
			}
		});
	}
	/*
	if($(".splash_page").length > 0) {
		$(".splash_page").attr("current", "false");
		$(".splash_page:first").css("background-color", "#716E43");
		$(".splash_page:first").attr("current", "true");
		ts = $(".splash_page:first").attr("title");
		
		$(".splash_page").each(function () {
			im = $(this).attr("title");
			$.preloadImages("library/splash/" + im);
		});
		
		$("#splash1").hide();
		$("#splash1").css("background-image", "url(library/splash/" + ts + ")");
		$("#splash1").fadeIn("slow");
		$(".splash_page").click(function() {
			$(".splash_page").css("background-color", "#000000");
			$(".splash_page").attr("current", "false");
			$(this).attr("current", "true");
			t = $(this).attr("title");
			$(this).css("background-color", "#716E43");
			if($("#splash1").is(':visible')) {
				$("#splash1").fadeOut("slow");
				$("#splash2").css("background-image", "url(library/splash/" + t + ")");
				$("#splash2").fadeIn("slow");
			} else {
				$("#splash2").fadeOut("slow");
				$("#splash1").css("background-image", "url(library/splash/" + t + ")");
				$("#splash1").fadeIn("slow");
			}
			return false;
		});
		
		function clickNextSplash() {
			a = $(".splash_page[current=true]").next(".splash_page");
			if(a.length > 0) {
				a.click();
			} else {
				$(".splash_page:first").click();
			}
		}

		$("#splash_page_auto").click(function () {
			a = $("#splash_page_auto");
			b = a.html();
			if(b == "PAUSE") {
				clearInterval(slideshowAutorun);
				a.html("PLAY");
			} else {
				slideshowAutorun = setInterval(clickNextSplash, 6000);
				a.html("PAUSE");
			}
		});
		slideshowAutorun = setInterval(clickNextSplash, 6000);
		
	} // end splash page stuff
	*/

	// Drag and drop	
	$(".dd_item").draggable({
		helper:'clone',
		cursor:'pointer', 
		/*distance:'20',*/ 
		opacity:'.5', 
		revert:'invalid', 
		scroll:'true', 
		zIndex:'1000'
	});	
		
	/*
	$("#collector").droppable({
		accept: '.dd_item',
		activeClass: 'droppable-active',
		hoverClass: 'droppable-hover',
		drop: function (ev, ui) {
			var showcase_id = $(ui.draggable).attr('showcase');
			var product_id = $(ui.draggable).attr('product');
			
			if (showcase_id != null) {
				ic_add_showcase(showcase_id);
			} else if (product_id != null) {
				ic_add_product(product_id);
			}			
		}
	});		
	*/
	
	$('.add-item').click(function(){ 
		var showcase_id = $(this).attr('showcase');
		var product_id = $(this).attr('product');
		
		if (showcase_id != null) {
			ic_add_showcase(showcase_id);
		} else if (product_id != null) {
			ic_add_product(product_id);
		}
		// $(this).html("This product is in your Idea Collector.");
		var toggleswitch = $("#idea-collector span.toggle");
		if(toggleswitch.html() == "OPEN") toggleswitch.click();
	});
	
	function ic_title(item_count) {
		var $title = $("#idea-collector .title");
		if (item_count <= 0) {
			$title.text("Idea Collector (Empty)");
		} else if (item_count == 1) {
			$title.text("Idea Collector (1 item)");
		} else {
			$title.text("Idea Collector  ("+item_count+" items)");
		}
	}
	
	function ic_get() { 
	
		var item_count = 0;
	
		$.getJSON("/library/ajax/ic_get_json.php", function(data){
			var html = "";
			var lastCategory = ""; 
			
			item_count = data.ideas.length;
			
			ic_title(item_count);
					
			$.each(data.ideas, function(i,item){
				if(lastCategory != item.category) {
					lastCategory = item.category;
					html += "<li class='ic-title'>" + item.category + "</li>";
				}
				
				var itemLink = "";
				if(item.item_type == 'product') {
					itemLink = "cabinets.php?product=" + item.item_id;
				} else {
					itemLink = "showcase.php?show=" + item.item_id;
				}
				html += "<li rel='" + item.id + "'><a href='" + itemLink + "'><span>" + item.title + "</span></a><img src='library/gfx/delete.png' class='ic_delete'/></li>";		
			});
			$("#info").hide();
			$("#ideas").children().remove();
			$("#ideas").append("<ul>" + html + "</ul>");
			
			$(".ic_delete").click(function(){
				var idea = $(this).parent()
				ic_delete(idea.attr("rel"));		
				idea.remove();
				item_count -= 1;
				ic_title(item_count);
			});
		});
	}	
	
	function ic_add_showcase(id) { 
		$.get("/library/ajax/ic_add_showcase.php?id=" + id, function(data) {
			ic_get();															 
		});		
	}
	
	function ic_add_product(id) { 
		$.get("/library/ajax/ic_add_product.php?id=" + id, function(data) {
			ic_get();															 
		});		
	}
	
	function ic_delete(id) {
		$.get("/library/ajax/ic_del.php?id=" + id, function(data) {
			if ($("#ideas ul").children().length == 0) {
				$("#info").slideDown();	
			}
		});
	}
	
	function ic_delete_all() { 
		$.get("/library/ajax/ic_del_all.php", function(data) { 
			$("#ideas").children().remove();
				$("#info").slideDown();	
			
		});
	}
	
	function ic_save(savename) { 
		$.get("/library/ajax/ic_save.php?savename=" + savename, function(data) {			
		});		
	}
	
	function ic_load(savename) { 
		$.get("/library/ajax/ic_load.php?savename=" + savename, function(data) {			
		});		
	}

	$("#begin_save_ic").click(
		function () {
			$("#begin_load_ic").hide();
			$("#begin_save_ic").hide();
			$("#name_ic").show();
			$("#save_ic").show();
			
			$("#save_ic").click(function () {					
				// Validate Email Address
				var email = $("#name_ic input").val();										  
				var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;

				if(reg.test(email) == false) {
					alert('Invalid Email Address');
					return false;
				}
										  
				// Save the IC
				ic_save(email);	
				
				$("#load_ic").show();
				$("#begin_save_ic").show();
				$("#name_ic").hide();
				$("#save_ic").hide();				
			});	
			
			$("#name_ic input").focus();
			
			$("#name_ic input").focus(function () {
				var email = $("#name_ic input").val();	
				if (email == "Email Address") {
					$("#name_ic input").val("");
				}
			});	
			
			$("#name_ic input").blur(function () {
				var email = $("#name_ic input").val();	
				if (email == "") {
					$("#name_ic input").val("Email Address");
				}
			});	
			
			$("#name_ic input").keydown(function(event){									   
				if (event.keyCode == 13) { // Enter
					$("#load_ic").click();
				}
			});
		}
	);
	
	$("#begin_load_ic").click(
		function () {
			$("#begin_load_ic").hide();
			$("#begin_save_ic").hide();
			$("#name_ic").show();
			$("#load_ic").show();
			
			$("#load_ic").click(function () {					
				// Validate Email Address
				var email = $("#name_ic input").val();										  
				var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;

				if(reg.test(email) == false) {
					alert('Invalid Email Address');
					return false;
				}
										  
				// Save the IC
				ic_load(email);	
				
				$("#begin_load_ic").show();
				$("#begin_save_ic").show();
				$("#name_ic").hide();
				$("#load_ic").hide();
				
				ic_get();	
			});	
			
			$("#name_ic input").focus();
			
			$("#name_ic input").focus(function () {
				var email = $("#name_ic input").val();	
				if (email == "Email Address") {
					$("#name_ic input").val("");
				}
			});	
			
			$("#name_ic input").blur(function () {
				var email = $("#name_ic input").val();	
				if (email == "") {
					$("#name_ic input").val("Email Address");
				}
			});	
			
			$("#name_ic input").keydown(function(event){									   
				if (event.keyCode == 13) { // Enter
					$("#load_ic").click();
				}
			});

		}
	);
	
	$("#clear_ic").click(
		function () { 
			ic_delete_all();
			ic_title(0);
		}
	);

	/*if($("#print").length > 0) {
		$("#print").click(
			function() {
				$(this).html("Print <div class=\"tutorial\">You must save your idea collector before printing it!</div>");
				tutorial_fade();
			}
		);
	}*/

	function tutorial_fade() {
		window.setTimeout(function() {
			$('.tutorial').fadeOut("slow");
		}, 5000);
	}

	// Initialize Idea Collector
	ic_get();
	
	// Admin
	if($("#delete").length > 0) {
		$("#delete").click(
			function () {
				$.prompt("Do you really want to delete this item?", { 
					buttons: { Yes: true, No: false }, 
					opacity: 0.75,
					overlayspeed: 'fast',
					callback: function (v, m) {
						if(v == true) {
							delID = $("#id").attr('value');
							window.location = window.location.pathname + "?del=" + delID;
						}
					}
				});
			}
		);
	}
	
	/*
	if($("#enlarge_showcase").length > 0) {
		$("#enlarge_showcase").click(
			function() {
				target = $("#enlarge_showcase").attr("href");
				window.open(target, "largeversion", "width=1024,height=768");
				return false;
			}
		);
	}
	*/
	
	if($("#jcrop_set").length > 0) {
		$("#uploadNewPic").click(function() {
			$(this).hide();
			$("#jcrop_set").show();
			$("#uploadNewDiv").show();
			$("#jcrop_coords").hide();
			$("#cropnow").attr("checked", "");
			$("#standardImg").show();
		});
		$("#jcrop_set").click(function() {
			$(this).hide();
			$("#uploadNewPic").show();
			$("#uploadNewDiv").hide();
			$("#standardImg").hide();
			$("#jcrop_coords").show();
			$("#cropnow").attr("checked", "checked");
			function showCoords(c) {
				$('#x').val(c.x);
				$('#y').val(c.y);
				$('#x2').val(c.x2);
				$('#y2').val(c.y2);
				$('#w').val(c.w);
				$('#h').val(c.h);
			};
			$(function(){
				$('#jcrop_target').Jcrop({
					onChange: showCoords,
					onSelect: showCoords,
					aspectRatio: 785/488
				});
			});
		});
	}
	if($("#product-pic").length > 0) {
		$("#enlarge_showcase_bg").hide();
		$("#product-pic").mouseover(function () {
			// alert("TEST IN");
			$("#enlarge_showcase_bg").show();
		});
		$("#product-pic").mouseout(function () {
			//																	 	alert("TEST OUT");
			$("#enlarge_showcase_bg").hide();
		});
	}
	if($("#products_vert").length > 0) {
		$("#products_vert a").mouseover(function () {
			$(this).find("span").css("opacity", "1");
			$(this).find("span").css("filter", "alpha(opacity=100)");
		});
		$("#products_vert a").mouseout(function () {
			$(this).find("span").css("opacity", ".65");
			$(this).find("span").css("filter", "alpha(opacity=65)");
		});
	}
	
	// Showcase paging
	if($("div.current").length > 0) {
		var current = $("div.current");
		var currentNum = parseInt(current.attr("num"));
		var margin = -1 * (120 * currentNum);
		for(m = 0; m > margin; m = m - 120 * 5) {
			marginStart = m;
		}
		$("#thumbnails").css("marginLeft", marginStart + "px");
		$("#leftarrow").click(function() {
			var marginLeft = parseInt($("#thumbnails").css("marginLeft"));
			if(marginLeft + 720 < 0) {
				$("#thumbnails").animate({
					marginLeft: "+=720px"
				}, 1250);
			} else {
				$("#thumbnails").animate({
					marginLeft: "0px"
				}, 1250);
			}
		});
		$("#rightarrow").click(function() {
			var lastNum = parseInt($("#thumbnails").children("div:last").attr("num"));
			var marginMax = -1 * (120 * lastNum);
			var marginLeft = parseInt($("#thumbnails").css("marginLeft"));
			if(marginLeft - 720 > marginMax + (120 * 6)) {
				$("#thumbnails").animate({
					marginLeft: "-=600px"
				}, 1250);
			} else {
				$("#thumbnails").animate({
					marginLeft: (marginMax + (120 * 6)) + "px"
				}, 1250);
			}
		});
	}
	// End showcase paging
	/* old paging
	if($("td.current").length > 0) {
		$("td.current").prevAll().hide();
		$("td.current").next().next().next().next().nextAll().hide();
		$("td.alwayshow").show();
		$("#goback").click(function () {
			oldcurrent = $("td.current");
			newcurrent = oldcurrent.prev();
			if(newcurrent.length > 0) {
				oldcurrent.removeClass("current");
				newcurrent.addClass("current");
				$("#thumbnail_row td").show();
				$("td.current").prevAll().hide();
				$("td.current").next().next().next().next().nextAll().hide();
			}
			$("td.alwayshow").show();
		});
		$("#goforward").click(function () {
			oldcurrent = $("td.current");
			newcurrent = oldcurrent.next();
			if(newcurrent.next().next().next().next().length > 0) {
				oldcurrent.removeClass("current");
				newcurrent.addClass("current");
				$("#thumbnail_row td").show();
				$("td.current").prevAll().hide();
				$("td.current").next().next().next().next().nextAll().hide();
			}
			$("td.alwayshow").show();
		});
	}
	*/
	
	if($("#sidebar").length > 0) {
		$("#sidebar").scrollFollow({
			speed: 500,
			container: 'main_middle'
		});
	}
	$("textarea.resizable:not(.processed)").TextAreaResizer();
	$(".cabinet_catalog").fancybox({
		'frameWidth': 820,
		'frameHeight': 610,
	});
	
	$("input#s").each(function() {
		var t = $(this);
		if(t.val().length < 1) {
		t.val("Search").addClass("search_init").focus(function() {
			if(t.hasClass("search_init")) {
					t.removeClass("search_init");
					t.val("");
				}
				t.select();
			}).blur(function() {
				var t = $(this);
				if(t.val().length < 1 || t.val() == "Search") {
					t.addClass("search_init");
					t.val("Search");
				}
			});
		}
	});
	
	var ic = $("#idea-collector");
	var icc = $("#idea-collector-contents");
	ic.find("span.toggle").click(function() {
		var t = $(this);
		icc.slideToggle(500, function() {
			if (icc.is(':visible')) {
				t.text("CLOSE");
			} else {
				t.text("OPEN");
			}
		});
		
	});
});

