/* clearTimeOut Keeps the Menu Open */
$clearTimeOut = 0;
$clearPricingTimeOut = 0;
// Keep the Menu Open if we're highlighting a sub-menu item
function keep_open()
	{
		$clearTimeOut = 0;
		$clearPricingTimeOut = 0;
		try{clearTimeout($temp_timeout)}catch(e){}
	}

// Begin the closing procedure with a countdown
function close_menu($id)
	{
		$clearTimeOut = 1;
		$temp_timeout = setTimeout("close_menu_final("+$id+");", 250);					
	}
function close_pricing()
	{
		$clearPricingTimeOut = 1;
		$temp_timeout = setTimeout("close_pricing_final();", 250);					
	}	
// Do the final menu clearing
function close_menu_final($id)
	{
		// Check whether or not we've scrolled over a menu item
		if($clearTimeOut == 1)
			{
				$use_id = "#sub-menu-"+$id;
				setTimeout(function(){$($use_id).parent().children("a").removeClass("hover");}, 250);
				$($use_id).slideUp({duration: 250});
			}
	}
function close_pricing_final()
	{
		// Check whether or not we've scrolled over a menu item
		if($clearPricingTimeOut == 1)
			{$(".pricing-instructions").animate({left: -200});}
	}

$(function(){$("a[rel=lightbox]").lightBox();});
$(document).ready(function()
	{
		/**************/
		/* MENU STUFF */
		$.open_menu = 0;
		$("a[id^='main-menu-item-']").mouseover(function(){
			// Start the timeout to keep the menu open
			//$(this).addClass("hover")
			keep_open()
			// Create the id to ref the submenu
			$sub_menu_id = $(this).attr("id").replace("main-menu-item-", "");
			$id = "sub-menu-"+$sub_menu_id;
			$new_sub_menu = "#"+$id;
			
			if($.open_menu !== $new_sub_menu)
				{$(".sub_menu").slideUp("fast");}
		
			// fade in the submenu
			$($new_sub_menu).slideDown({duration: 250});
			$.open_menu = $new_sub_menu;
		});
		$("[id^='sub-menu-']").mouseover(function(){
			// Start the timeout to keep the menu open
			keep_open()														
		});
		$("[id^='sub-menu-']").mouseout(function(){
			// Create the id to ref the submenu
			$sub_menu_id = $(this).attr("id").replace("sub-menu-", "");
			// Start the cloding process
			close_menu($sub_menu_id);				
		});
		$("a[id^='main-menu-item-']").mouseout(function(){
			// Create the id to ref the submenu
			$sub_menu_id = $(this).attr("id").replace("main-menu-item-", "");
			// Start the cloding process
			close_menu($sub_menu_id);								
		});
		
		/***************/
		/* INDEX STUFF */
		$.selected_slide = 1;
		$.selected_tab = 1;
		$max_slides= $("#client-quote-count").html(); 
		if(document.getElementById("client-quotes")) {
			setInterval(
				function()
					{
						$current_id = "#quote-"+$.selected_slide;
						$.selected_slide++
						if($max_slides == $.selected_slide)
							{$.selected_slide = 1}
						$new_id = "#quote-"+$.selected_slide;
						$($current_id).fadeOut({duration: 750});
						setTimeout(function(){$($new_id).fadeIn({duration: 750});}, 800);						
					}
				,4000
			);
		};
		$frame_left = 0;
		$frame_no = 1;
		$max_clicks = 3;
		$("a[id^='theme-right-href-']").click(function(){
			$move_by = 250;
			//Choose Frame Number
			$new_frame_no = $(this).attr("id").replace("theme-right-href-", "");
			
			//Change dot
			$(".dot-selected").removeClass("dot-selected");			
			$dot_id = "#theme-right-href-"+$new_frame_no;
			$($dot_id).addClass("dot-selected");

			//Slide me!			
			$new_top = -($new_frame_no*$move_by)+($move_by);
			$new_top_attr = $new_top+"px";
			$("#theme-right-slider").animate({top: $new_top_attr}, 350);

			
			return false;
		});
		
		$frame_left = 0;
		$frame_no = 1;
		$max_clicks = $("#three-col-theme-slider ul").children().size(); 
		$("a[id^='index-dot-']").click(function(){
			$move_by = 704;
			//Choose Frame Number
			$new_frame_no = $(this).attr("id").replace("index-dot-", "");
			
			//Change dot
			$(".dot-selected").removeClass("dot-selected");			
			$dot_id = "#index-dot-"+$new_frame_no;
			$($dot_id).addClass("dot-selected");

			//Slide me!			
			$new_left = -($new_frame_no*$move_by)+($move_by);
			$new_left_attr = $new_left+"px";
			$("#three-col-theme-slider").animate({left: $new_left_attr}, 350);

			
			return false;
		});
		
		/********************/
		/* THEME SHOP STUFF */
		$frame_left = 0;
		$frame_no = 1;
		$start_middle = 0;
		$move_by = $("#screenshot-container div").width();
		$max_clicks = $("#screenshot-container div").children().size(); 
		
		$("#screenshot-slider").animate({left: 0}, 250);
		/* Kudos to Rob MacKay [http://www.twitter.com/svgrob] for suggesting that we don't have to put a static style="width: 3960px" attribute in the div tag */
		$imgCont = $move_by * $max_clicks; 
		
		$("#image_container div").css({width: $imgCont});		
		$("#screenshot-slider").click(function()
			{
				/* Set the new position & frame number */
				$new_frame_no = (($frame_no/1)+1);
				$new_left = (($frame_left/1) - $move_by);
				
				if($new_frame_no == $max_clicks)
					{		
						/* Move all the way right, to the beginning*/
						$new_left = 0;
						$new_frame_no = 1;
					}			
				$new_left_attr = $new_left+"px";
				$("#screenshot-slider").animate({left: $new_left_attr}, 350);
				$frame_left = $new_left;
				$frame_no = $new_frame_no;
				
				//Change dot
				$(".dot-selected").removeClass("dot-selected");			
				$dot_id = "#theme-image-href-"+$new_frame_no;
				$dot_id_2 = "#theme-image-href-2-"+$new_frame_no;
				$($dot_id+", "+$dot_id_2).addClass("dot-selected");
				
				return false;
			});	
		
		$("a[id^='theme-image-href-'], a[id^='theme-image-href-2-']").click(function(){
			//Choose Frame Number
			$new_frame_no = $(this).attr("id").replace("theme-image-href-2-", "");
			$new_frame_no = $new_frame_no.toString().replace("theme-image-href-", "");
			
			//Change dot
			$(".dot-selected").removeClass("dot-selected");			
			$dot_id = "#theme-image-href-"+$new_frame_no;
			$dot_id_2 = "#theme-image-href-2-"+$new_frame_no;
			$($dot_id+", "+$dot_id_2).addClass("dot-selected");

			//Slide me!			
			$new_left = -($new_frame_no*$move_by)+($move_by);
			$new_left_attr = $new_left+"px";
			$("#screenshot-slider").animate({left: $new_left_attr}, 350);

			
			return false;
		});
		
		$("#expand-product-window").click(function(){
			if($(this).html() == "Expand Screenshot Window")
				{
					$new_height = $("#image_container div img").height();
					if($new_height > 600) 
						{$new_height = 600;}
					$(".screenshot-container").animate({height: $new_height}, 150);
					$(".prev-screenshot, .next-screenshot").animate({top: (($new_height/2)-12)}, 400);
					$(this).html("Contract Screenshot Window");
				}
			else
				{
					$(".screenshot-container").animate({height: 460}, 150);
					$(".prev-screenshot, .next-screenshot").animate({top: 120}, 400);
					$(this).html("Expand Screenshot Window");
				}
			return false;
		});
		
		$(".view-all-features").click(function(){
				if($(this).html() == "View All Features")
					{
						$(this).html("Hide Extra Features");
						$("[id^=hidden-features-]").slideDown("fast");
					}
				else
					{
						$(this).html("View All Features");
						$("[id^=hidden-features-]").slideUp("fast");
					}				
				return false;
			});

		$("a[id^='purchase-options-href-']").click(function(){
			$id = $(this).attr("id").replace("href-", "div-");
			$use_id = "#"+$id;
			if($(this).html() !== "Cancel")
				{
					$($use_id).fadeIn("fast");
					$(this).html("Cancel");
				}
			else
				{
					$($use_id).fadeOut("fast");
					$(this).html("Purchase");
				}
			return false;
		});
		
		$("#pricing-grid li a").mouseover(function(){
			$(".pricing-instructions").animate({left: 0});
		});
		
		$("#pricing-grid, #pricing-grid li a").mouseout(function(){
			close_pricing();
		});
		
		$("a[id^='add-']").mouseover(function(){
			// Start the timeout to keep the menu open
			keep_open();
			$useid = $(this).attr("id").replace("add-", "");
			 
			$price_id = "#price-desicription-"+$useid;
			$old_price = "#price-desicription-"+$.chosen_price;
			
			$($old_price).css("display", "none");
			$($price_id).css("display", "block");
			 
			$.chosen_price = $useid;
		});
		
		
		/******************/
		/* SHOWCASE STUFF */
		
		$("[id^=like-showcase-]").click(function(){
			$menuId = $(this).attr("id").replace("like-showcase-", "");
			if($(this).parent().hasClass("mod-like-active"))
				{
					$(this).parent().attr("class", "mod-like");
					$like = 0;
				}
			else
				{
					$(this).parent().attr("class", "mod-like-active");
					$like = 1;
				}
			// Compile the request location
			$post_page = "like_showcase.cfm";
			$.post($post_page, {menuId: $menuId, like: $like}, 
				function(data) {
					$("#likes").html(data);
			});
			return false;
		});
		
		/**************/
		/* CART STUFF */
		
		$("[id^=add-]").click(function(){$(".feed").html("Redirecting to the Checkout Page");});
		$("[id^=remove-]").click(function(){$(".feed").html("Removing Product");});
		
		$("#register-checkout-href").click(function(){
			$use_id = "#"+$(this).attr("id").replace("href", "div");
			if(!($(this).hasClass("selected")))
				{
					$("#login-checkout-href").removeClass("selected");
					$("#register-checkout-href").addClass("selected");
					$("#register-checkout-div").slideDown();
					$("#login-checkout-div").slideUp();
				}
			return false;
		});
		
		$("#login-checkout-href").click(function(){
			$use_id = "#"+$(this).attr("id").replace("href", "div");
			if(!($(this).hasClass("selected")))
				{
					$("#register-checkout-href").removeClass("selected");
					$("#login-checkout-href").addClass("selected");
					$("#login-checkout-div").slideDown();
					$("#register-checkout-div").slideUp();
				}
			return false;
		});
		$("#change-pass").click(function(){
			if($("#password-fields").hasClass("no_display"))
				{
					$("#password-fields").slideDown().removeClass("no_display");
					$(this).html("Cancel");
				}
			else
				{
					$("#password-fields").slideUp().addClass("no_display");
					$(this).html("Change Your Password");
				}
			return false;
		});
		$("[id^=checkout-theme-]").click(function(){
			$my_free_themes = "";
			$my_free_themes_count = $("#free_theme_count").attr("value");
			$max_free_themes = $("#max_free_themes").attr("value");

			$use_theme = $(this).attr("id").replace("checkout-theme-", "");

			if(($max_free_themes > $my_free_themes_count && $max_free_themes !== $my_free_themes_count) && !($(this).hasClass("active")))
				{
					$(this).addClass("active");
				}
			else if(!($(this).hasClass("active")))
				{
					$str_len = ($("#free_themes").attr("value").length-6);
					if($str_len > 0)
						{
							$use_id = $("#free_themes").attr("value").substr($str_len , 6);
							$use_id = "#checkout-theme-"+$use_id;
							$($use_id).removeClass("active");
						}
					else
						{$(".active").removeClass("active");}
					$(this).addClass("active");
				}
			else
				{$(this).removeClass("active");}
				
			$my_free_themes_count = 0;
			$(".active").each(function(){
				$use_theme_id = $(this).attr("id").replace("checkout-theme-", "");
				
				if($my_free_themes == "")
					{$my_free_themes = $use_theme_id;}
				else
					{$my_free_themes += ","+$use_theme_id;}
					
				if($use_theme_id !== "")
					{$my_free_themes_count++;}
			});
			
			$("#free_theme_count").attr("value", $my_free_themes_count);
			$("#free_themes").attr("value", $my_free_themes);

			//	$("#free_theme_count_html").html(($max_free_themes-($my_free_themes_count/1)));
			
			return false;
		});
		
		$("#shopping-cart").click(function(){
			if($("#cart-container").css("display") == "none")
				{$("#cart-container").fadeIn();}
			else
				{$("#cart-container").fadeOut();}
			return false;
		});
		
		$("#terms").click(function(){
			if($(this).attr("checked"))
				{$("#buy_button").removeAttr("disabled");}
			else
				{$("#buy_button").attr("disabled", "1");}
		});
		
		$("#coupon").focus(function(){
			$("#2-checkout-form").attr("action", "checkout_3.cfm");
		});
		
		$("#coupon").blur(function(){
			$("#2-checkout-form").attr("action", "2checkout_direct.cfm");
		});
		
		$("[id^=validate-coupon]").click(function(){		
			$("#2-checkout-form").attr("action", "checkout_3.cfm");
			$("#2-checkout-form").submit();
			return false;
		});
		
		/**************/
		/* BLOG STUFF */
		
		$("#comment_form").submit(function(){
			// Compile the request location
			$post_page = "refresh_comments.cfm";
			// Compile all the request details
			$menuId = $("#menuid").attr("value");
			$section = $("#section").attr("value");
			$name = $("#name").attr("value");
			$email = $("#email").attr("value");
			$link = $("#url").attr("value");
			$comment = $("#comment").attr("value");
			$email_subscribe = $("#check_subscribe").attr("checked");
			
			$error = "";
			if($name == "" || $name == "Name"){$error += "\n Enter in your name.";}
			if($comment == ""){$error += "\n Enter in a comment."}
			if($error)
				{
					alert("Please correct the following:\n"+$error);
					return false;
				}
			else
				{
					// Fade out the new comment div so that we can fade it in after posting our new comment
					//$($new_comments_id).fadeOut("fast");
					$("#commment-post-alert").fadeIn("slow");
							
		
					// Perform the "Magic" which is just a bit of Ajax
					$.post($post_page, {menuId: $menuId, name: $name, email: $email, url: $link, comment: $comment, section: $section, email_subscribe: $email_subscribe, insert : 1}, 
						function(data) {
							if($.browser.msie)
								{location.reload();}
							else
								{$("#new_comments").html($("#new_comments").html()+" "+data).fadeIn("slow");}
							$("#commment-post-alert").fadeOut("fast");
							$("#comment").attr("value", "");
					});
				}
			return false;
		});
	
	
		$(".delete-icon").click(function(){
			var delete_special = confirm("Are you sure you want to delete this special?");
			if(!delete_special)
				{return false;}
			else
				{return true;}
		});
		$("#header-login-btn, #close-login").click(function()
			{	
				
				if($("#header-cart-form").attr("class") !== "no_dispaly")
					{
						$(".cart-tip").fadeOut("slow");
						$("#header-cart-form").slideUp("slow").addClass("no_display");
					}
				if($("#header-login-btn").html() == "Logout")
					{
						container_dim();
						$("#login-menu").slideUp("fast");
					}
				else if($("#header_login_form").attr("class") == "no_display")
					{
						container_dim();
						$("#header_login_form").fadeIn("slow").removeClass("no_display");						
						$("#header-login-btn").html("Hide Form");
						return false;
					}				
				else
					{
						$("#login-form-alert").html("");
						$("#header_login_form").fadeOut("fast").addClass("no_display");
						$("#header-login-btn").html("Login");
						container_lighten();
						return false;
					}
			});
		
		$("#forget-me").click(function()
			{
				$("#login-menu").slideUp("fast");
				$post_page = "forget_me.cfm";
				$("#cookie-info").remove();
				$("#cart-li").remove();
				$.post($post_page, {}, function(data){$("#login-menu").slideDown("fast");});
				$.post("fetch-cart-details.cfm?fetch=1", {},
				function(data)
					{
						$("#login-menu").append(data).slideDown("fast");
						$("#cart-li").slideDown("slow");
					});
				return false;
			});
		
		/*****************/
		/* DOCUMENTATION */
				
		$.selected_doc_tab = 1;
		
		$("#documantation-theme-selector #theme-list").change(function(){
			$("#documantation-theme-selector").submit();
		});
		$("[id^=documentation-href-]").click(function(){
				$id = $(this).attr("id").replace("documentation-href-", "");
				$old_href = "#documentation-href-"+$.selected_doc_tab;
				$old_ul = "#documentation-ul-"+$.selected_doc_tab;
				$old_detail = "#details-"+$.selected_doc_tab+"-1";
				$old_tab = "#documentation-details-"+$.selected_doc_tab;				
				$new_href = "#documentation-href-"+$id;			
				$new_ul = "#documentation-ul-"+$id;
				$new_detail = "#details-1-"+$id;
				$new_tab = "#documentation-details-"+$id;
				
				$($old_href).removeClass("selected");				
				$($new_href).addClass("selected");
				
				$($old_detail).slideUp("fast");
				$($old_tab).fadeOut("fast");
				$($old_ul).slideUp("fast");
				setTimeout(function()
					{						
						$($new_tab).fadeIn("fast").addClass("clearfix");
						$($new_detail).fadeIn("fast");
						$($new_ul).slideDown("fast");
					}, 200);
				
				$old_sub_href = "#expand-href-1-"+$.selected_doc_tab;
				
				$.selected_doc_tab = $id;
				$.selected_doc_detail = "1-"+$id;
				
				$new_sub_href = "#expand-href-"+$.selected_doc_detail;		
				
				$($old_sub_href).removeClass("selected");				
				$($new_sub_href).addClass("selected");		
				
				return false
			});
		
		$.selected_doc_detail = "1-1";
		$("a[id^=expand-href-]").click(function(){
			$id = $(this).attr("id").replace("expand-href-", "");
			$old_doc_deet = $.selected_doc_detail;
			if($id !== $.selected_doc_detail)
				{
					$old_sub_href = "#expand-href-"+$.selected_doc_detail;
					$old_tab = "#details-"+$.selected_doc_detail;
					$new_sub_href = "#expand-href-"+$id;	
					$new_tab = "#details-"+$id;
					
					$($old_sub_href).removeClass("selected");				
					$($new_sub_href).addClass("selected");	
					
					$($old_tab).fadeOut("fast");
					setTimeout(function(){$($new_tab).fadeIn("fast").addClass("clearfix");}, 195);
				}
			$.selected_doc_detail = $id;
			return false;
		});
		
		
		$("a[id^='documentation-image-href-']").click(function(){
			$move_by = 322;
			//Choose Frame Number
			$new_frame_no = $(this).attr("id").replace("documentation-image-href-"+$.selected_doc_detail+"-", "");			
			$slider_id = "#documentation-slider-"+$.selected_doc_detail;
			
			//Change dot
			$(".dot-selected").removeClass("dot-selected");						
			$(this).addClass("dot-selected");
			
			//Slide me!			
			$new_left = -($new_frame_no*$move_by)+($move_by);
			$new_left_attr = $new_left+"px";
			
			$($slider_id).animate({left: $new_left_attr}, 350);
			
			return false;
		});
		
		$("[id^=checkout-tab-]").click(function()
			{
				if($(this).attr("id") == "checkout-tab-1")
					{
						$(this).addClass("register-button-active");
						$("#checkout-tab-2").removeClass("login-button-active");
						$("#checkout-content-1").slideDown("fast");
						$("#checkout-content-2").slideUp("fast");
					}
				else
					{
						$("#checkout-tab-1").removeClass("register-button-active");
						$(this).addClass("login-button-active");
						$("#checkout-content-1").slideUp("fast");
						$("#checkout-content-2").slideDown("fast");
					}
				return false;
			});
		$("#source_domain").keyup(function(){
			if(document.getElementById("new_theme"))
				{
					$use_html = "<link rel=\"stylesheet\" type=\"text/css\" href=\""+$(this).attr("value")+"/style.css\" />";
					$use_html += "\n<!--[if lte IE 8]><link href=\""+$(this).attr("value")+"/ie7-style.css\" rel=\"stylesheet\" type=\"text/css\" />";
					$use_html += "\n<script src=\""+$(this).attr("value")+"/js/domassistantcompressed-2.7.4.js\" type=\"text/javascript\"></script>";
					$use_html += "\n<script src=\""+$(this).attr("value")+"/js/ie-css3.js\" type=\"text/javascript\"></script>";
					$use_html += "\n<![endif]-->";
				}
			else if(document.getElementById("colour"))
				{
					$use_html = "<link rel=\"stylesheet\" type=\"text/css\" href=\""+$(this).attr("value")+"/style.css\" />";
					$use_html += "\n<!--[if lte IE 8]><link href=\""+$(this).attr("value")+"/ie-style.css\" rel=\"stylesheet\" type=\"text/css\"><![endif]-->";
					$use_html += "\n<link rel=\"stylesheet\" type=\"text/css\" href=\""+$(this).attr("value")+"/color-styles/"+$("#colour").attr("value")+"/style.css\" />";
				}
			else
				{$use_html = "<link rel=\"stylesheet\" type=\"text/css\" href=\""+$(this).attr("value")+"/style.css\" />";}
			$("#source_code").attr("value", $use_html);
		});
		$("#colour").change(function(){
			$use_html = "<link rel=\"stylesheet\" type=\"text/css\" href=\""+$("#source_domain").attr("value")+"/style.css\" />";
			$use_html += "\n<!--[if lte IE 8]><link href=\""+$("#source_domain").attr("value")+"/ie-style.css\" rel=\"stylesheet\" type=\"text/css\"><![endif]-->";
			$use_html += "\n<link rel=\"stylesheet\" type=\"text/css\" href=\""+$("#source_domain").attr("value")+"/color-styles/"+$("#colour").attr("value")+"/style.css\" />";
			$("#source_code").attr("value", $use_html);
		});
		
		/* Hotfix Listing*/
		$("[id^=hotfix-href-]").click(function()
			{
				$use_id = $(this).attr("id").replace("hotfix-href-", "#hotfix-details-");
				
				if($($use_id).attr("class") == "no_display")
					{$($use_id).slideDown("slow").removeClass("no_display");}
				else
					{$($use_id).slideUp("slow").addClass("no_display");}
				return false;
			});
		
		/* Tab Swapping */
		if(document.getElementById("profile-tab"))
			{$.profiletab = $("#profile-tab").html();}
		else
			{$.profiletab = 1;}
		$tab_id = new Array();
		$old_tab_id = new Array();
		$check_load_page = new Array();
		$load_page = new Array();
		$("[id^=profile-tab-]").click(function(){
			/* Create the id to reference the content*/
			$id = this.id.toString().replace("profile-tab-", "");
			$old_tab_id[$id] = "#profile-content-" + $.profiletab;
			$tab_id[$id] = "#profile-content-" + $id;
			$check_load_page[$id] = "fetch-page-"+$id;
			$load_page[$id] = "#fetch-page-"+$id;
			// If we're clicking a new tab, fade it in.
			if($(".selected > a").attr("id") !== this.id)
				{
					//Fade the Old Form Out
					$($old_tab_id[$id]).slideUp("fast");
					
					//Fade the New Form out, Change it's Class, and Fade it in again
					$($tab_id[$id]).addClass("selected_tab").slideDown("fast");
											
					//Clear the Class of the Selected Tab
					$(".selected").attr("class", "")
					//Clear the Class of the New Tab						
					$(this).parent().attr("class", "selected")
					
					if(document.getElementById($check_load_page[$id]))
						{
							$post_page = $($load_page[$id]).html();
							$.post($post_page, {}, function(data)
							{									
								$($tab_id[$id]+" > div").html(data);
								$($tab_id[$id]+" > p").remove();
								$("#"+$check_load_page).attr("id", "");
								$(function(){$("[id^=banner-img-]").lightBox();});
							});
						}
				}
			$.profiletab = $id;
			return false;
		});
		
		/* Banner List */
		
		$("[id^=show-hide-], [id^=tr-show-hide-]").live("click", function(){
				if($(this).attr("id").indexOf("tr") !== -1)
					{
						$tbody = "#"+$(this).attr("id").replace("tr-show-hide-", "body-");
						$href_id = "#"+$(this).attr("id").replace("tr-show-hide-", "show-hide-");
					}
				else
					{
						$tbody = "#"+$(this).attr("id").replace("show-hide-", "body-");
						$href_id = "#"+$(this).attr("id");
					}
					
				if($($href_id).html().indexOf("+ Expand") !== -1)
					{
						$($href_id).html("- Hide");
						$($tbody).fadeIn("slow");
					}
				else
					{
						$($href_id).html("+ Expand");
						$($tbody).fadeOut("fast");
					}
				return false;
			});
	});