/* 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;				
				jQuery(use_id).slideUp({duration: 250});
			}
	}
function switch_slides(current_id, next_id)
	{	
		old_post_id = current_id.replace("image-", "post-");
		new_post_id = next_id.replace("image-", "post-");
		
		jQuery(current_id).fadeOut("fast");
		jQuery(next_id).addClass("floatleft");
		jQuery(old_post_id).slideUp("slow");
		
		setTimeout(
			function()
				{
					jQuery(next_id).fadeIn("slow");
					//jQuery(next_id+" img").css("left", (0.5*(960-jQuery(next_id+" img").attr("width")))+"px")
					jQuery(new_post_id).slideDown("slow").addClass("feature-post-content").addClass("clearfix");
					jQuery.busy = 0;
				}
		,250);
	}
function match_image_height(i, id){
	thumbid = "#post-list-"+id;
	previd="#prev-"+id;
	nextid="#next-"+id;
	
	new_height = jQuery(thumbid+" ul:nth-child("+i+")").height();
	jQuery(thumbid).parent().animate({height: new_height}, 150);
	jQuery(previd+", "+nextid).animate({top: ((new_height/2))}, 400);
	return false;
}

function load_slider(id, style, post_count, grid_format, hide_title, hide_excerpt, reload){
	thumbid = "#post-list-"+id;
	if(jQuery.post_limit[id] == false)
		{
			post_page = jQuery("#template-directory").html()+"/functions/load_slider.php";		
			jQuery.get(post_page, {post_category: id, post_count: post_count, grid_format: grid_format, default_style: style, hide_title: hide_title, hide_excerpt: hide_excerpt}, 
				function(data) {
					if(data !== "")
						{
							jQuery(thumbid).html(data);
							jQuery(thumbid).fadeIn("slow");
							jQuery(thumbid).animate({left: 0}, { duration: 200});
							match_image_height(1, id);
							if(style == "multi" && jQuery(id).children().size()*grid_format > post_count)
								{jQuery("#next_prev_btns").fadeOut("slow");}
							else 
								{jQuery("#next_prev_btns").fadeIn("slow");}
						}
					else
						{jQuery.post_limit[id] = true;}
			});
		}
}
/* Lightbox Gallery Function */
jQuery.post_limit = new Array();
jQuery(document).ready(function()
	{
		/* All functions for the featured videos */	
		frame_left = 0;
		frame_no = 1;
		jQuery("a[id^=next-]").each(function(){
			id = jQuery(this).attr("id").replace("next-", "");
			match_image_height(1, id);
			jQuery.post_limit[id] = false;
		});
		/* Thumbnail View */
		jQuery("a[id^=show-mutli-]").click(function(){											   
			id = jQuery(this).attr("id").replace("show-mutli-", "");
			jQuery.post_limit[id] = false;
			/* Fade Out the Slider */
			use_id = "#post-list-"+id;
			jQuery(use_id).fadeOut("slow");
			/* Switch classes */
			multi_link = "#show-mutli-"+id;
			single_link = "#show-slider-"+id;
			
			jQuery(multi_link).attr("class", "multi-selected");
			jQuery(single_link).attr("class", "single");
			
			/* Set offset to 0 */
			jQuery("#slider-offset-"+id).text("0");
			jQuery("#slider-style-"+id).text("multi");
			load_slider(id, "multi", jQuery("#slider-posts-"+id).text(), jQuery("#slider-format-"+id).text(), jQuery("#slider-hide-title-"+id).text(), jQuery("#slider-hide-excerpt-"+id).text(), 1);
			return false;
		});
		
		jQuery("a[id^=show-slider-]").click(function(){
			id = jQuery(this).attr("id").replace("show-slider-", "");
			jQuery.post_limit[id] = false;
			/* Fade Out the Slider */
			use_id = "#post-list-"+id;
			jQuery(use_id).fadeOut("slow");
			/* Switch classes */
			multi_link = "#show-mutli-"+id;
			single_link = "#show-slider-"+id;
			
			jQuery(multi_link).attr("class", "multi");
			jQuery(single_link).attr("class", "single-selected");
			
			/* Set offset to 0 */
			jQuery("#slider-offset-"+id).text("0");
			jQuery("#slider-style-"+id).text("single");
			load_slider(id, "single", jQuery("#slider-posts-"+id).text(), jQuery("#slider-format-"+id).text(), jQuery("#slider-hide-title-"+id).text(), jQuery("#slider-hide-excerpt-"+id).text(), 1);
			return false;
		});
		jQuery("a[id^=next-]").click(function(){	
			id = jQuery(this).attr("id").replace("next-", "");
			thumbid = "#post-list-"+id;
			
			move_by = 990;
			max_clicks = jQuery(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";
			jQuery(thumbid).animate({left: new_left_attr}, { duration: 200});
			frame_left = new_left;
			frame_no = new_frame_no;
			
			match_image_height(frame_no, id);
			
			/* Append the next set of images */
			style = jQuery("#slider-style-"+id).text();
			post_count = jQuery("#slider-count-"+id).text();
			return false;
		});
		
		jQuery("a[id^=prev-]").click(function(){	
			id = jQuery(this).attr("id").replace("prev-", "");
			thumbid = "#post-list-"+id;
			
			move_by = 990;
			max_clicks = jQuery(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";
			jQuery(thumbid).animate({left: new_left_attr}, {duration: 200});
			frame_left = new_left;
			frame_no = new_frame_no;
			
			match_image_height(frame_no, id);
			
			/* Append the next set of images */
			style = jQuery("#slider-style-"+id).text();
			post_count = jQuery("#slider-count-"+id).text();
			return false;
		});
		jQuery("a[id^='image-href-']").click(function(){
			new_image = jQuery(this).attr("id").replace("image-href-", "#image-src-");
			jQuery("#post-gallery-image").html(jQuery(new_image).html());
			/* THE OLD METHOD OF CHANGING THE THUMBS : 
			jQuery("#post-gallery-image img").attr("src", jQuery(this).attr("href"));
			jQuery("#post-gallery-image img").attr("width", "600");
			jQuery("#post-gallery-image img").attr("style", "height: auto;");
			*/
			return false;
		});
		jQuery("a[id^=like-post-]").click(function(){									
			jQuery("#post-gallery-image img").attr("src", jQuery(this).attr("href"))
			return false;
		});
		jQuery("a[id^=like-post-]").click(function(){
			id = jQuery(this).attr("id").replace("like-post-", "");
			post_page = jQuery("#template-directory").html()+"/functions/like_unlike.php";		
			jQuery.get(post_page, {like: 1, id: id}, 
				function(data) {
					like_count = "("+data+" votes)";
					jQuery("#like-count").html(like_count);
				});
			return false;
		});
		jQuery("a[id^=unlike-post-]").click(function(){
			id = jQuery(this).attr("id").replace("unlike-post-", "");
			post_page = jQuery("#template-directory").html()+"/functions/like_unlike.php";		
			jQuery.get(post_page, {like: 0, id: id}, 
				function(data) {
					like_count = "("+data+" votes)";
					jQuery("#unlike-count").html(like_count);
				});
			return false;
		});
		/********/
		/* 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;
			if(document.getElementById(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({duration: 100});	
					jQuery.open_menu = new_sub_menu;
				}
		});
		
		jQuery("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 = jQuery(this).attr("id").replace("main-menu-page-item-", "");
			id = "sub-page-menu-"+sub_menu_id;
			if(document.getElementById(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-'], [id^='sub-page-menu-']").mouseover(function(){
			// Start the timeout to keep the menu open
			keep_open()														
		});
		jQuery("[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 = jQuery(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);				
		});
		
			
		/******************/
		/* Theme 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("#comment_form_container").addClass("threaded-comments");
					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_form_container").removeClass("threaded-comments");
					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(jQuery("#"+twitter_id).attr("value") == "Twitter ID")
				{jQuery("#"+twitter_id).attr("value", "");}
		});
		
		jQuery("#"+twitter_id).blur(function(){
			if(jQuery("#"+twitter_id).attr("value") == "")
				{jQuery("#"+twitter_id).attr("value", "Twitter ID");}
		})
		
		url_id = "url";		
		jQuery("#"+url_id).focus(function(){
			if(jQuery("#"+url_id).attr("value") == "Website URL")
				{jQuery("#"+url_id).attr("value", "");}
		});
		jQuery("#"+url_id).blur(function(){
			if(jQuery("#"+url_id).attr("value") == "")
				{jQuery("#"+url_id).attr("value", "Website URL");}
		});
		
		twitter_id = "twitter";	
		jQuery("#"+twitter_id).focus(function(){
			if(jQuery("#"+twitter_id).attr("value") == "Twitter ID")
				{jQuery("#"+twitter_id).attr("value", "");}
		});
		
		jQuery("#"+twitter_id).blur(function(){
			if(jQuery("#"+twitter_id).attr("value") == "")
				{jQuery("#"+twitter_id).attr("value", "Twitter ID");}
		})
	});