/* 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;
				jQuery(use_id).slideUp("1000");
			}
	}
	
/* Lightbox Gallery Function */
jQuery(function(){jQuery("#gallery-slider > .gallery-list > .gallery-image > a.view-image").lightBox();});
jQuery(function(){jQuery(".copy p img").parent().lightBox();});

jQuery(document).ready(function()
	{
		jQuery.current_selected = 1;
		jQuery("[id^='ocmx-featured-href-']").click(function()
			{
				use_id = jQuery(this).attr("id").replace("ocmx-featured-href-", "");
				
				old_details = "#ocmx-featured-description-"+jQuery.current_selected;
				old_media = "#ocmx-featured-image-"+jQuery.current_selected;
				old_href  = "#ocmx-featured-href-"+jQuery.current_selected;
				
				new_details = "#ocmx-featured-description-"+use_id;
				new_media = "#ocmx-featured-image-"+use_id;
				new_href  = "#ocmx-featured-href-"+use_id;

				jQuery(old_media).slideUp("fast");
				jQuery(old_details).slideUp("fast");
				jQuery(old_href).removeClass("selected");
				/* Clear old header */
				jQuery(new_media).fadeIn("slow");
				jQuery(new_details).slideDown("fast");
				jQuery(new_href).addClass("selected");
				
				jQuery.current_selected = use_id;
				return false;
			});
		if(document.getElementById("feature-auto-slide") && jQuery("#feature-auto-slide").html() !== "0")
			{
				slide_interval = (jQuery("#feature-auto-slide").html()*1000);
				if(!isNaN(slide_interval))
					{						
						setInterval(function()
							{
								old_details = "#ocmx-featured-description-"+jQuery.current_selected;
								old_media = "#ocmx-featured-image-"+jQuery.current_selected;
								old_href  = "#ocmx-featured-href-"+jQuery.current_selected;
								
								use_id = (jQuery.current_selected/1+1);
								if(jQuery("#feature-count").html() == use_id)
									{use_id = 1;}
									
								new_details = "#ocmx-featured-description-"+use_id;
								new_media = "#ocmx-featured-image-"+use_id;
								new_href  = "#ocmx-featured-href-"+use_id;
				
								jQuery(old_media).slideUp("fast");
								jQuery(old_details).slideUp("fast");
								jQuery(old_href).removeClass("selected");
								/* Clear old header */
								jQuery(new_media).fadeIn("slow");
								jQuery(new_details).slideDown("fast");
								jQuery(new_href).addClass("selected");
								jQuery.current_selected = use_id;
							}, slide_interval);
					}
				//return false;
			}
		
		/* All functions for the featured videos */	
		jQuery.current_tab = 1;		
		jQuery("a[id^='tabbed-href-']").click(function()
			{
				old_href_id = "#tabbed-href-"+jQuery.current_tab;
				old_tab_id = "#tabbed-div-"+jQuery.current_tab;
				jQuery(old_tab_id).slideUp("slow");
				jQuery(old_href_id).parent().removeClass("selected");
				
				use_id = jQuery(this).attr("id").replace("tabbed-href-", "");
				new_tab_id = "#tabbed-div-"+use_id;
				
				jQuery(this).parent().addClass("selected");
				jQuery(new_tab_id).slideDown("slow");
				jQuery.current_tab = use_id;
				return false;
								
			});
		/*****************/
		/* Platform Menu */
		jQuery.open_menu = 0;
		jQuery("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 = jQuery(this).attr("id").replace("main-menu-item-", "");
			id = "sub-menu-"+sub_menu_id;
			new_sub_menu = "#"+id;
			
			if(jQuery.open_menu !== new_sub_menu)
				{jQuery(".sub_menu").slideUp("fast");}
		
			// fade in the submenu
			jQuery(new_sub_menu).addClass("sub_menu").slideDown("2000");	
			jQuery.open_menu = new_sub_menu;
		});
		jQuery("[id^='sub-menu-']").mouseover(function(){
			// Start the timeout to keep the menu open
			keep_open()														
		});
		jQuery("[id^='sub-menu-']").mouseout(function(){
			// Create the id to ref the submenu
			sub_menu_id = jQuery(this).attr("id").replace("sub-menu-", "");
			// Start the cloding process
			close_menu(sub_menu_id);				
		});
		jQuery("a[id^='main-menu-item-']").mouseout(function(){
			// Create the id to ref the submenu
			sub_menu_id = jQuery(this).attr("id").replace("main-menu-item-", "");
			// Start the cloding process
			close_menu(sub_menu_id);								
		});
			
		/********************/
		/* Platform Comments */		
		jQuery("#commentform").submit(function(){return false;});
		jQuery("#comment_jump").click(function(){
			setTimeout(function(){jQuery("html").animate({scrollTop: jQuery("#comment_anchor").offset().top}, 1000);}, 500);
			return false;
		});
		jQuery("#comment_submit").live("click", function(){
			// Compile the request location
			post_page = jQuery("#template-directory").html()+"/functions/ocmx_comment_post.php";
			
			// Compile all the request details
			author = jQuery("#author").attr("value");
			email = jQuery("#email").attr("value");
			url = jQuery("#url").attr("value");
			comment = jQuery("#comment").attr("value");
			twitter = jQuery("#twitter").attr("value");
			email_subscribe = jQuery("#email_subscribe").attr("checked");
			post_id = jQuery("#comment_post_id").attr("value");
			comment_parent_id = jQuery("#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
			//jQuery(new_comments_id).fadeOut("fast");
			jQuery("#commment-post-alert").fadeIn("slow");
			// Perform the "Magic" which is just a bit of Ajax
			jQuery.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(jQuery.browser.msie)
						{location.reload();}
					else
						{jQuery(new_comments_id).html(jQuery(new_comments_id).html()+" "+data).fadeIn("slow");}
					jQuery("#commment-post-alert").fadeOut("fast");
					jQuery("#comment").attr("value", "");
			});
			return false;
		});
		
		jQuery("a[id^='reply-']").live("click", function(){
			// Create the Comment Id and apply it to the comment form
			comment_id = jQuery(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(jQuery(new_location_id).html().toString().indexOf("Leave") == -1)
				{
					jQuery("#comment_form_container").remove().appendTo(new_location_id);
					jQuery(new_location_id).fadeIn("slow");
					jQuery("#comment_parent_id").attr("value", comment_id);
					// Change href to Cancel
					jQuery(href_id).html("Cancel Reply");
				}
			else
				{
					jQuery(new_location_id).fadeOut("fast");
					jQuery("#comment_form_container").remove().appendTo("#original_comment_location");
					jQuery("#comment_parent_id").attr("value", "0");
					// Change href back to Reply
					jQuery(href_id).html("Reply");
				}
			setTimeout(function(){jQuery("html").animate({scrollTop: jQuery("#commentform").offset().top}, 1000);}, 500);
			return false;
		});
		
		/**********************/
		/* Search Form Clearer */
		search_criteria_id = "search_criteria";
		jQuery("#"+search_criteria_id).focus(function(){
			if(jQuery("#"+search_criteria_id).attr("value") == "Search")
				{jQuery("#"+search_criteria_id).attr("value", "");}
		});
		
		jQuery("#"+search_criteria_id).blur(function(){
			if(jQuery("#"+search_criteria_id).attr("value") == "")
				{jQuery("#"+search_criteria_id).attr("value", "Search");}
		});
		
		/************************/
		/* Contact Form Clearer */
		contact_name_id = "contact_name";
		jQuery("#"+contact_name_id).focus(function(){
			if(jQuery("#"+contact_name_id).attr("value") == "Name")
				{jQuery("#"+contact_name_id).attr("value", "");}
		});
		
		jQuery("#"+contact_name_id).blur(function(){
			if(jQuery("#"+contact_name_id).attr("value") == "")
				{jQuery("#"+contact_name_id).attr("value", "Name");}
		});
					
		contact_email_id = "contact_email";
		jQuery("#"+contact_email_id).focus(function(){
			if(jQuery("#"+contact_email_id).attr("value") == "Email Address")
				{jQuery("#"+contact_email_id).attr("value", "");}
		});
		
		jQuery("#"+contact_email_id).blur(function(){
			if(jQuery("#"+contact_email_id).attr("value") == "")
				{jQuery("#"+contact_email_id).attr("value", "Email Address");}
		});
			
		contact_subject_id = "contact_subject";
		jQuery("#"+contact_subject_id).focus(function(){
			if(jQuery("#"+contact_subject_id).attr("value") == "Subject")
				{jQuery("#"+contact_subject_id).attr("value", "");}
		});
		
		jQuery("#"+contact_subject_id).blur(function(){
			if(jQuery("#"+contact_subject_id).attr("value") == "")
				{jQuery("#"+contact_subject_id).attr("value", "Subject");}
		});
		contact_message_id = "contact_message";
		jQuery("#"+contact_message_id).focus(function(){
			if(jQuery("#"+contact_message_id).attr("value") == "Your Message")
				{jQuery("#"+contact_message_id).attr("value", "");}
		});
		
		jQuery("#"+contact_message_id).blur(function(){
			if(jQuery("#"+contact_message_id).attr("value") == "")
				{jQuery("#"+contact_message_id).attr("value", "Your Message");}
		});
		
		/*************************/
		/* Comments Form Clearer */
		search_id = "s";	
		jQuery("#"+search_id).focus(function(){
			if(jQuery("#"+search_id).attr("value") == "Search")
				{jQuery("#"+search_id).attr("value", "");}
		});
		
		jQuery("#"+search_id).blur(function(){
			if(jQuery("#"+search_id).attr("value") == "")
				{jQuery("#"+search_id).attr("value", "Search");}
		});
		
		/*************************/
		/* Comments Form Clearer */
		author_id = "author";	
		jQuery("#"+author_id).focus(function(){
			if(jQuery("#"+author_id).attr("value") == "Name")
				{jQuery("#"+author_id).attr("value", "");}
		});
		
		jQuery("#"+author_id).blur(function(){
			if(jQuery("#"+author_id).attr("value") == "")
				{jQuery("#"+author_id).attr("value", "Name");}
		});
		
		email_id = "email";	
		jQuery("#"+email_id).focus(function(){
			if(jQuery("#"+email_id).attr("value") == "EMail Address")
				{jQuery("#"+email_id).attr("value", "");}
		});
		
		jQuery("#"+email_id).blur(function(){
			if(jQuery("#"+email_id).attr("value") == "")
				{jQuery("#"+email_id).attr("value", "EMail Address");}
		});
		
		twitter_id = "twitter";	
		jQuery("#"+twitter_id).focus(function(){
			if(("#"+twitter_id).attr("value") == "Twitter ID")
				{("#"+twitter_id).attr("value", "");}
		});
		
		jQuery("#"+twitter_id).blur(function(){
			if(("#"+twitter_id).attr("value") == "")
				{("#"+twitter_id).attr("value", "Twitter ID");}
		})
		
		url_id = "url";		
		jQuery("#"+url_id).focus(function(){
			if(("#"+url_id).attr("value") == "Website URL")
				{("#"+url_id).attr("value", "");}
		});
		jQuery("#"+url_id).blur(function(){
			if(("#"+url_id).attr("value") == "")
				{("#"+url_id).attr("value", "Website URL");}
		});
		
		twitter_id = "twitter";	
		jQuery("#"+twitter_id).focus(function(){
			if(("#"+twitter_id).attr("value") == "Twitter ID")
				{("#"+twitter_id).attr("value", "");}
		});
		
		jQuery("#"+twitter_id).blur(function(){
			if(("#"+twitter_id).attr("value") == "")
				{("#"+twitter_id).attr("value", "Twitter ID");}
		})
	});