/* 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 && ($id.indexOf("sub-menu-") > -1 || $id.indexOf("sub-page-menu-") > -1))
			{
				$use_id = "#"+$id;				
				$($use_id).slideUp({duration: 150});
			}
	}
	
function slideFrame($thumbid, $move_by, $direction, $type, $match_height)
	{
		/* Set the new position & frame number */
		$frame_left = $($thumbid).css($type).replace("px", "");
		$max_clicks = $($thumbid).children().size();	
		
		$frame_no = (-($frame_left/$move_by)+1);
		if($direction == 0)
			{
				$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;
					}		
			}
		else
			{
				$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";
		if($type == "left")
			{$($thumbid).animate({"left": $new_left_attr}, {duration: 350});}
		else
			{$($thumbid).animate({"top": $new_left_attr}, {duration: 300});}
			
		if($match_height)
			{match_image_height($thumbid, $new_frame_no);}
	}
	
function match_image_height($thumbid, $i){
	$use_id = $($thumbid).attr("id").replace("feature-slider-", "");
	$previd="#previous-feature-slide-"+$use_id;
	$nextid="#next-feature-slide-"+$use_id;
	
	$copy_height_id= "#feature-copy-"+$i
	$copy_height = $($copy_height_id).height();
	
	$copy_image_id= "#feature-image-"+$i
	$image_height = $($copy_image_id).height();
	
	if($image_height > $copy_height)
		{$new_height = ($image_height/1+10);}
	else
		{$new_height = ($copy_height/1+10);}
	
	if($new_height < 310){$new_height = 310};
	if($new_height) {
		$($thumbid).parent().animate({height: $new_height}, 150);
		//$($previd+", "+$nextid).animate({top: (($new_height/2)-8)}, 400);
	}
	return false;
}
$(function(){$("a[rel=lightbox]").lightBox();});
$(document).ready(function()
	{
		/************************/
		/* Popular Posts Slider */
		
		/* All functions for the featured videos */	
		$frame_left = 0;
		$frame_no = 1;
		$("#related-next").click(function(){			
			$thumbid = "#related-list";
			$move_by = 244;
			slideFrame($thumbid,  $move_by, 0, "top");
			$current_page = $("#related-page").html();
			$max_clicks = $($thumbid).children().size();
			if(($current_page) < ($max_clicks))
				{$("#related-page").html(($current_page/1+1));}
			else
				{$("#related-page").html(1);}
			return false;
		});
		
		$("#related-prev").click(function(){
			$thumbid = "#related-list";
			$move_by = 244;
			slideFrame($thumbid,  $move_by, 1, "top");
			$current_page = $("#related-page").html();
			$max_clicks = $($thumbid).children().size();
			if(($current_page-1) > 0)
				{$("#related-page").html(($current_page/1-1));}
			else
				{$("#related-page").html($max_clicks);}
			return false;
		});
		
		$("a[id^='next-feature-slide-']").click(function(){
			$use_id = $(this).attr("id").replace("next-feature-slide-", "");
			$sliding_element = "#feature-slider-"+$use_id;
			$move_by = 960;
			slideFrame($sliding_element,  $move_by, 0, "left", 1);
			return false;					
		});	
		$("a[id^='previous-feature-slide-']").click(function(){
			$use_id = $(this).attr("id").replace("previous-feature-slide-", "");
			$sliding_element = "#feature-slider-"+$use_id;
			$move_by = 960;
			slideFrame($sliding_element,  $move_by, 1, "left", 1);
			return false;					
		});
		$("a[id^='showcase-next-']").click(function(){
			$use_id = $(this).attr("id").replace("showcase-next-", "");
			$sliding_element = "#showcase-containter-"+$use_id;
			$move_by = 980;
			slideFrame($sliding_element,  $move_by, 0, "left");
			return false;					
		});
		$("a[id^='showcase-prev-']").click(function(){
			$use_id = $(this).attr("id").replace("showcase-prev-", "");
			$sliding_element = "#showcase-containter-"+$use_id;
			$move_by = 980;
			slideFrame($sliding_element,  $move_by, 1, "left");
			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).removeClass("tab-active");
				
				$use_id = $(this).attr("id").replace("tabbed-href-", "");
				$new_tab_id = "#tabbed-div-"+$use_id;
				
				$(this).addClass("tab-active");
				$($new_tab_id).slideDown("slow");
				$.current_tab = $use_id;
				
				return false;								
			});
		/*****************/
		/********/
		/* 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;
			if(document.getElementById($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({duration: 100});	
					$.open_menu = $new_sub_menu;
				}
		});
		
		$("a[id^='main-menu-page-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-page-item-", "");
			$id = "sub-page-menu-"+$sub_menu_id;
			if(document.getElementById($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-'], [id^='sub-page-menu-']").mouseover(function(){
			// Start the timeout to keep the menu open
			keep_open()														
		});
		$("[id^='sub-menu-'], [id^='main-menu-item-'], [id^='sub-page-menu-'], [id^='main-menu-page-item-']").mouseout(function(){
			// Create the id to ref the submenu
			$sub_menu_id = $(this).attr("id");
			if($sub_menu_id.indexOf("main-menu-item-") > -1)
				{$sub_menu_id = $sub_menu_id.replace("main-menu-item-", "sub-menu-");}
			else if($sub_menu_id.indexOf("main-menu-page-item-") > -1)
				{$sub_menu_id = $sub_menu_id.replace("main-menu-page-item-", "sub-page-menu-");}
			// Start the cloding process
			close_menu($sub_menu_id);				
		});
		
		$("a").mouseout(function(){
			// Create the id to ref the submenu
			$sub_menu_id = $(this).attr("id").replace("main-menu-page-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").live("click", function(){
			// 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");
	
			// 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}, 
				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;
		});
		
		$("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("Post") == -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");}
		});
	});