jQuery.noConflict();
/* 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: 150});
			}
	}
	
function slideFrame(thumbid, move_by, direction, type, match_height, use_element)
	{
		/* Set the new position & frame number */
		frame_left = jQuery(thumbid).css(type).replace("px", "");
		max_clicks = jQuery(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";
		frame_no = new_frame_no;
		if(type == "left")
			{jQuery(thumbid).animate({"left": new_left_attr}, {duration: 350});}
		else
			{jQuery(thumbid).animate({"top": new_left_attr}, {duration: 300});}
			
		if(match_height)
			{
				setTimeout(function()
					{match_image_height(thumbid, new_frame_no, use_element)}
				, 350)
			}
	}
	
function match_image_height(thumbid, i, use_element){
	if(use_element == "quotes")
		{
			img_height_id= "#client-quote-img-"+i;
			copy_height_id= "#client-quote-"+i;
			min_height = 40;
		}
	else 
		{
			selected_href= "#feature-href-"+i;
			img_height_id= "#feature-image-"+i;
			copy_height_id= "#feature-splurb-"+i;
			jQuery("#slider-pages li a.active").toggleClass("active");
			jQuery(selected_href).addClass("active");
			min_height = 265;
		}
		
	if(jQuery(img_height_id).height() > jQuery(copy_height_id).height())
		{new_height = jQuery(img_height_id).height();}
	else 
		{new_height = jQuery(copy_height_id).height();}
		
	if(new_height < min_height){new_height = min_height};
	new_height = (new_height/1+10);
	
	if(new_height) {
		jQuery(thumbid).parent().animate({height: new_height}, 150);
		//jQuery(previd+", "+nextid).animate({top: ((new_height/2)-8)}, 400);
	}
	return false;
}
jQuery(document).ready(function()
	{
		/************************/
		/* Popular Posts Slider */
		
		/* All functions for the featured videos */	
		frame_left = 0;
		frame_no = 1;
		quoteno = 1;
		jQuery("#next-quote").click(function(){
			clearInterval(start_quotes);
			sliding_element = "#quote-slider";
			move_by = 970;
			slideFrame(sliding_element,  move_by, 0, "left", 1, "quotes");
			
			old_quote = "#client-quote-"+quoteno;
			old_quote_name = "#client-quote-by-"+quoteno;
			old_quote_img = "#client-quote-img-"+quoteno;
			max_clicks = jQuery(".quote-from").children().size();
			quoteno = (quoteno/1)+1;
			
			if(quoteno <= max_clicks)
				{
					new_quote = "#client-quote-"+quoteno;
					new_quote_name = "#client-quote-by-"+quoteno;
					new_quote_img = "#client-quote-img-"+quoteno;
				}
			else 
				{
					quoteno = 1;
					new_quote = "#client-quote-"+quoteno;
					new_quote_name = "#client-quote-by-"+quoteno;
					new_quote_img = "#client-quote-img-"+quoteno;
				}
			jQuery(old_quote_name).fadeOut("fast");
			setTimeout(function(){
				jQuery(new_quote_name).fadeIn("fast");							
			}, 200);
			return false;
		});
		
		jQuery("#feature-next").click(function(){
			clearInterval(start_slider);
			sliding_element = "#feature-slider";
			move_by = 970;
			slideFrame(sliding_element,  move_by, 0, "left", 1, "feature-slider");
			return false;
		});
			
		jQuery("#feature-prev").click(function(){
			clearInterval(start_slider);
			sliding_element = "#feature-slider";
			move_by = 970;
			slideFrame(sliding_element,  move_by, 1, "left", 1, "feature-slider");
			return false;
		});
		
		if(document.getElementById("feature-slider"))
			{
				slide_interval = 5000;
				start_slider = setInterval(function()
					{
						sliding_element = "#feature-slider";
						move_by = 970;
						slideFrame(sliding_element,  move_by, 0, "left", 1, "feature-slider");
					}, slide_interval);
			}
		
		if(document.getElementById("feature-slider"))
			{
				slide_interval = 5000;
				start_quotes = setInterval(function()
					{
						sliding_element = "#quote-slider";
						move_by = 970;
						slideFrame(sliding_element,  move_by, 0, "left", 1, "quotes");
						
						old_quote = "#client-quote-"+quoteno;
						old_quote_name = "#client-quote-by-"+quoteno;
						old_quote_img = "#client-quote-img-"+quoteno;
						max_clicks = jQuery(".quote-from").children().size();
						quoteno = (quoteno/1)+1;
						
						if(quoteno <= max_clicks)
							{
								new_quote = "#client-quote-"+quoteno;
								new_quote_name = "#client-quote-by-"+quoteno;
								new_quote_img = "#client-quote-img-"+quoteno;
							}
						else 
							{
								quoteno = 1;
								new_quote = "#client-quote-"+quoteno;
								new_quote_name = "#client-quote-by-"+quoteno;
								new_quote_img = "#client-quote-img-"+quoteno;
							}
						jQuery(old_quote_name).fadeOut("fast");
						setTimeout(function(){
							jQuery(new_quote_name).fadeIn("fast");							
						}, 200);
					}, slide_interval);
			}
		jQuery("a[id^='feature-href-']").click(function(){
			move_by = 970;
			use_id = jQuery(this).attr("id").replace("feature-href-", "");
			new_left_attr = -(use_id-1)*move_by;
			jQuery("#feature-slider").animate({"left": new_left_attr}, {duration: 350});
			jQuery("#slider-pages li a.active").toggleClass("active");
			jQuery(this).addClass("active");
			return false;
		});
		
		
		jQuery("a[id^='team-']").click(function(){
			use_id = jQuery(this).attr("id").replace("team-'", "");
			desc_id = "#team-description-"+use_id;
			href_id = "#team-"+use_id;
			if(jQuery(this).hasClass("open"))
				{
					jQuery(this).removeClass("open");
					jQuery(this).parent().children("div").slideUp()
					setTimeout(function(){jQuery(this).parent().children("div").attr("class", "no_display");}, 50);
				}
			else 
				{
					jQuery(this).addClass("open");
					jQuery(this).parent().children("div").slideUp({duration: 0}).attr("class", "member-description clearfix").slideDown();
				}
			return false;
		});					
		
		/*****************/
		/********/
		/* Menu */
		
		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(open_menu !== new_sub_menu)
						{jQuery(".sub_menu").slideUp("fast");}
				
					// fade in the submenu
					jQuery(new_sub_menu).addClass("sub_menu").slideDown({duration: 100});	
					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(open_menu !== new_sub_menu)
						{jQuery(".sub_menu").slideUp("fast");}
					// fade in the submenu
					jQuery(new_sub_menu).addClass("sub_menu").slideDown("2000");	
					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);				
		});
		
		jQuery("a").mouseout(function(){
			// Create the id to ref the submenu
			sub_menu_id = jQuery(this).attr("id").replace("main-menu-page-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("Post") == -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");}
		});
	});
