/* clearTimeOut Keeps the Menu Open */
$clearTimeOut = 0;
// Keep the Menu Open if we're highlighting a sub-menu item
function keep_open()
	{$clearTimeOut = 0;}
// Begin the closing procedure with a countdown
function close_menu($id)
	{
		$clearTimeOut = 1;
		$temp_timeout = setTimeout("close_menu_final("+$id+");", 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;
				$($use_id).slideUp("fast");
			}
	}
function send_comment()
			{
				// Compile the request location
				$post_page = $("#template-directory").html()+"/functions/ocmx_comment_post.php";
				
				// Compile all the request details
				$author = $("#author").attr("value");
				$email = $("#email").attr("value");
				$url = $("#url").attr("value");
				$comment = $("#comment").attr("value");
				$twitter = $("#twitter").attr("value");
				$email_subscribe = $("#email_subscribe").attr("checked");
				$post_id = $("#comment_post_id").attr("value");
				$comment_parent_id = $("#comment_parent_id").attr("value");
				$captchafree = $("#captchafree").attr("value");
				// Set which area the new comment will end up in
				if($comment_parent_id !== "0" && $comment_parent_id !== "")
					{$new_comments_id = "#new-reply-"+$comment_parent_id;}
				else
					{$new_comments_id = "#new_comments";}
				
				// 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, { author: $author, email: $email, url: $url, twitter: $twitter, email_subscribe: $email_subscribe, comment: $comment, comment_post_id: $post_id, comment_parent: $comment_parent_id, captchafree: $captchafree}, 
					function(data) {
						if($.browser.msie)
							{location.reload();}
						else
							{$($new_comments_id).html($($new_comments_id).html()+" "+data).fadeIn("slow");}
						$("#commment-post-alert").fadeOut("fast");
						$("#comment").attr("value", "");
				});
				return false;
			};
/* Lightbox Gallery Function */
$(function(){$("a[rel=lightbox]").lightBox();});
$(document).ready(function()
	{
		/* All functions for the featured videos */	
		$frame_left = 0;
		$frame_no = 1;
		$video_move_by = 530;
		$("a[id^=next-]").each(function(){
			setTimeout(function(){match_image_height(1);}, 2500);
		});
		$("#next-featured-video").click(function(){				 
			$thumbid = "#post-list";
			$max_clicks = $($thumbid).children().size();
			$imgCont = $move_by * $max_clicks; 
		
			/* Set the new position & frame number */
			$new_frame_no = (($frame_no/1)+1);
			$new_left = (($frame_left/1) - $video_move_by);
			/* Check if we're moving too far over */
			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";
			$($thumbid).animate({left: $new_left_attr}, {duration: 200});
						
			$old_description = "#video-description-"+$frame_no+" div:nth-child(1)";
			$new_description = "#video-description-"+$new_frame_no+" div:nth-child(1)";
			
			$($old_description).removeClass("selected");
			$($new_description).addClass("selected");
			
			$frame_left = $new_left;
			$frame_no = $new_frame_no;
			match_image_height($frame_no);
			return false;
		});
		
		$("#previous-featured-video").click(function(){
			$thumbid = "#post-list";
			
			$max_clicks = $($thumbid).children().size();
			
			$imgCont = $move_by * $max_clicks; 
		
			/* Set the new position & frame number */
			$new_frame_no = (($frame_no/1)-1);
			$new_left = (($frame_left/1) + $video_move_by);
			/* Check if we're moving too far over */
			if($new_frame_no <= 0)
				{
					/* Move the images all the way left, minus one frame */
					$new_left = -($move_by*$max_clicks)+$video_move_by;
					$new_frame_no = $max_clicks;
				}
			$new_left_attr = $new_left+"px";
			$($thumbid).animate({left: $new_left_attr}, {duration: 200});
						
			$old_description = "#video-description-"+$frame_no+" div:nth-child(1)";
			$new_description = "#video-description-"+$new_frame_no+" div:nth-child(1)";
			
			$($old_description).removeClass("selected");
			$($new_description).addClass("selected");
						
			$frame_left = $new_left;
			$frame_no = $new_frame_no;
			
			match_image_height($frame_no);
			return false;
		});
		$("a[id^=attachment-]").click(function(){
			$(".sliding-element > div > a > img").attr("src", $(this).attr("href"));
			$(".sliding-element > div > a > img").attr("style", "height: auto;");
			return false;
		});
		/* All functions for the featured videos */	
		$frame_left = 0;
		$frame_no = 1;
		$move_by = 370;
		$("a[id^=next-]").each(function(){
			match_image_height(1);
		});
		$("#next-featured").click(function(){	
			$thumbid = "#post-list";
						
			$max_clicks = $($thumbid).children().size();
			$imgCont = $move_by * $max_clicks; 
		
			/* Set the new position & frame number */
			$new_frame_no = (($frame_no/1)+1);
			$new_left = (($frame_left/1) - $move_by);
			/* Check if we're moving too far over */
			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";
			$($thumbid).animate({left: $new_left_attr}, {duration: 200});
			$frame_left = $new_left;
			$frame_no = $new_frame_no;
			match_image_height($frame_no);
			return false;
		});
		
		$("#previous-featured").click(function(){
			$thumbid = "#post-list";
			
			$max_clicks = $($thumbid).children().size();
			
			$imgCont = $move_by * $max_clicks; 
		
			/* Set the new position & frame number */
			$new_frame_no = (($frame_no/1)-1);
			$new_left = (($frame_left/1) + $move_by);
			/* Check if we're moving too far over */
			if($new_frame_no <= 0)
				{
					/* Move the images all the way left, minus one frame */
					$new_left = -($move_by*$max_clicks)+$move_by;
					$new_frame_no = $max_clicks;
				}
			$new_left_attr = $new_left+"px";
			$($thumbid).animate({left: $new_left_attr}, {duration: 200});
			$frame_left = $new_left;
			$frame_no = $new_frame_no;
			
			match_image_height($frame_no);
			return false;
		});
		
		/* All functions for the featured videos */	
		$.current_tab = 1;		
		$("a[id^='tabbed-href-']").click(function()
			{
				$old_href_id = "#tabbed-href-"+$.current_tab;
				$old_tab_id = "#tabbed-div-"+$.current_tab;
				$($old_tab_id).slideUp("slow");
				$($old_href_id).parent().removeClass("selected");
				
				$use_id = $(this).attr("id").replace("tabbed-href-", "");
				$new_tab_id = "#tabbed-div-"+$use_id;
				
				$(this).parent().addClass("selected");
				$($new_tab_id).slideDown("slow");
				$.current_tab = $use_id;
				return false;
								
			});
		/*****************/
		/* Platform Menu */
		$.open_menu = 0;
		$("a[id^='main-menu-item-']").mouseover(function(){
			// Start the timeout to keep the menu open
			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).addClass("sub_menu").slideDown("2000");	
			$.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);								
		});
			
		/********************/
		/* Platform Comments */		
		$("#commentform").submit(function(){return false;});
		$("#comment_jump").click(function(){
			setTimeout(function(){$("html").animate({scrollTop: $("#comment_anchor").offset().top}, 1000);}, 500);
			return false;
		});
		
		$("#comment_submit").click(function(){gethash()});
		
		$("a[id^='reply-']").live("click", function(){
			// Create the Comment Id and apply it to the comment form
			$comment_id = $(this).attr("id").replace("reply-", "");
			
			// Set which href we're dealing with
			$href_id = "#reply-"+$comment_id;
			
			//Set where exactly the comment form will end up
			$new_location_id = "#form-placement-"+$comment_id;
			
			//Create the Id for the new placement of the comment Form and put it there
			if($($new_location_id).html().toString().indexOf("Leave") == -1)
				{
					$("#comment_form_container").remove().appendTo($new_location_id);
					$($new_location_id).fadeIn("slow");
					$("#comment_parent_id").attr("value", $comment_id);
					// Change href to Cancel
					$($href_id).html("Cancel Reply");
				}
			else
				{
					$($new_location_id).fadeOut("fast");
					$("#comment_form_container").remove().appendTo("#original_comment_location");
					$("#comment_parent_id").attr("value", "0");
					// Change href back to Reply
					$($href_id).html("Reply");
				}
			setTimeout(function(){$("html").animate({scrollTop: $("#commentform").offset().top}, 1000);}, 500);
			return false;
		});
		
		/**********************/
		/* Search Form Clearer */
		$search_criteria_id = "search_criteria";
		$("#"+$search_criteria_id).focus(function(){
			if($("#"+$search_criteria_id).attr("value") == "Search")
				{$("#"+$search_criteria_id).attr("value", "");}
		});
		
		$("#"+$search_criteria_id).blur(function(){
			if($("#"+$search_criteria_id).attr("value") == "")
				{$("#"+$search_criteria_id).attr("value", "Search");}
		});
		
		/************************/
		/* Contact Form Clearer */
		$contact_name_id = "contact_name";
		$("#"+$contact_name_id).focus(function(){
			if($("#"+$contact_name_id).attr("value") == "Name")
				{$("#"+$contact_name_id).attr("value", "");}
		});
		
		$("#"+$contact_name_id).blur(function(){
			if($("#"+$contact_name_id).attr("value") == "")
				{$("#"+$contact_name_id).attr("value", "Name");}
		});
					
		$contact_email_id = "contact_email";
		$("#"+$contact_email_id).focus(function(){
			if($("#"+$contact_email_id).attr("value") == "Email Address")
				{$("#"+$contact_email_id).attr("value", "");}
		});
		
		$("#"+$contact_email_id).blur(function(){
			if($("#"+$contact_email_id).attr("value") == "")
				{$("#"+$contact_email_id).attr("value", "Email Address");}
		});
			
		$contact_subject_id = "contact_subject";
		$("#"+$contact_subject_id).focus(function(){
			if($("#"+$contact_subject_id).attr("value") == "Subject")
				{$("#"+$contact_subject_id).attr("value", "");}
		});
		
		$("#"+$contact_subject_id).blur(function(){
			if($("#"+$contact_subject_id).attr("value") == "")
				{$("#"+$contact_subject_id).attr("value", "Subject");}
		});
		$contact_message_id = "contact_message";
		$("#"+$contact_message_id).focus(function(){
			if($("#"+$contact_message_id).attr("value") == "Your Message")
				{$("#"+$contact_message_id).attr("value", "");}
		});
		
		$("#"+$contact_message_id).blur(function(){
			if($("#"+$contact_message_id).attr("value") == "")
				{$("#"+$contact_message_id).attr("value", "Your Message");}
		});
		
		/*************************/
		/* Comments Form Clearer */
		$search_id = "s";	
		$("#"+$search_id).focus(function(){
			if($("#"+$search_id).attr("value") == "Search")
				{$("#"+$search_id).attr("value", "");}
		});
		
		$("#"+$search_id).blur(function(){
			if($("#"+$search_id).attr("value") == "")
				{$("#"+$search_id).attr("value", "Search");}
		});
		
		/*************************/
		/* Comments Form Clearer */
		$author_id = "author";	
		$("#"+$author_id).focus(function(){
			if($("#"+$author_id).attr("value") == "Name")
				{$("#"+$author_id).attr("value", "");}
		});
		
		$("#"+$author_id).blur(function(){
			if($("#"+$author_id).attr("value") == "")
				{$("#"+$author_id).attr("value", "Name");}
		});
		
		$email_id = "email";	
		$("#"+$email_id).focus(function(){
			if($("#"+$email_id).attr("value") == "EMail Address")
				{$("#"+$email_id).attr("value", "");}
		});
		
		$("#"+$email_id).blur(function(){
			if($("#"+$email_id).attr("value") == "")
				{$("#"+$email_id).attr("value", "EMail Address");}
		});
		
		$twitter_id = "twitter";	
		$("#"+$twitter_id).focus(function(){
			if($("#"+$twitter_id).attr("value") == "Twitter ID")
				{$("#"+$twitter_id).attr("value", "");}
		});
		
		$("#"+$twitter_id).blur(function(){
			if($("#"+$twitter_id).attr("value") == "")
				{$("#"+$twitter_id).attr("value", "Twitter ID");}
		})
		
		$url_id = "url";		
		$("#"+$url_id).focus(function(){
			if($("#"+$url_id).attr("value") == "Website URL")
				{$("#"+$url_id).attr("value", "");}
		});
		$("#"+$url_id).blur(function(){
			if($("#"+$url_id).attr("value") == "")
				{$("#"+$url_id).attr("value", "Website URL");}
		});
		
		$twitter_id = "twitter";	
		$("#"+$twitter_id).focus(function(){
			if($("#"+$twitter_id).attr("value") == "Twitter ID")
				{$("#"+$twitter_id).attr("value", "");}
		});
		
		$("#"+$twitter_id).blur(function(){
			if($("#"+$twitter_id).attr("value") == "")
				{$("#"+$twitter_id).attr("value", "Twitter ID");}
		})
	});