jQuery.fn.hoverIntent = function(f,g) {
	// default configuration options
	var cfg = {
		sensitivity: 7,
		interval: 100,
		timeout: 0
	};
	// override configuration options with user supplied object
	cfg = jQuery.extend(cfg, g ? { over: f, out: g } : f );

	// instantiate variables
	// cX, cY = current X and Y position of mouse, updated by mousemove event
	// pX, pY = previous X and Y position of mouse, set by mouseover and polling interval
	var cX, cY, pX, pY;

	// A private function for getting mouse position
	var track = function(ev) {
		cX = ev.pageX;
		cY = ev.pageY;
	};

	// A private function for comparing current and previous mouse position
	var compare = function(ev,ob) {
		ob.hoverIntent_t = clearTimeout(ob.hoverIntent_t);
		// compare mouse positions to see if they've crossed the threshold
		if ( ( Math.abs(pX-cX) + Math.abs(pY-cY) ) < cfg.sensitivity ) {
			jQuery(ob).unbind("mousemove",track);
			// set hoverIntent state to true (so mouseOut can be called)
			ob.hoverIntent_s = 1;
			return cfg.over.apply(ob,[ev]);
		} else {
			// set previous coordinates for next time
			pX = cX; pY = cY;
			// use self-calling timeout, guarantees intervals are spaced out properly (avoids JavaScript timer bugs)
			ob.hoverIntent_t = setTimeout( function(){compare(ev, ob);} , cfg.interval );
		}
	};

	// A private function for delaying the mouseOut function
	var delay = function(ev,ob) {
		ob.hoverIntent_t = clearTimeout(ob.hoverIntent_t);
		ob.hoverIntent_s = 0;
		return cfg.out.apply(ob,[ev]);
	};

	// A private function for handling mouse 'hovering'
	var handleHover = function(e) {
		// next three lines copied from jQuery.hover, ignore children onMouseOver/onMouseOut
		var p = (e.type == "mouseover" ? e.fromElement : e.toElement) || e.relatedTarget;
		while ( p && p != this ) { try { p = p.parentNode; } catch(e) { p = this; } }
		if ( p == this ) { return false; }

		// copy objects to be passed into t (required for event object to be passed in IE)
		var ev = jQuery.extend({},e);
		var ob = this;

		// cancel hoverIntent timer if it exists
		if (ob.hoverIntent_t) { ob.hoverIntent_t = clearTimeout(ob.hoverIntent_t); }

		// else e.type == "onmouseover"
		if (e.type == "mouseover") {
			// set "previous" X and Y position based on initial entry point
			pX = ev.pageX; pY = ev.pageY;
			// update "current" X and Y position based on mousemove
			jQuery(ob).bind("mousemove",track);
			// start polling interval (self-calling timeout) to compare mouse coordinates over time
			if (ob.hoverIntent_s != 1) { ob.hoverIntent_t = setTimeout( function(){compare(ev,ob);} , cfg.interval );}

		// else e.type == "onmouseout"
		} else {
			// unbind expensive mousemove event
			jQuery(ob).unbind("mousemove",track);
			// if hoverIntent state is true, then call the mouseOut function after the specified delay
			if (ob.hoverIntent_s == 1) { ob.hoverIntent_t = setTimeout( function(){delay(ev,ob);} , cfg.timeout );}
		}
	};

	// bind the function to the two event listeners
	return this.mouseover(handleHover).mouseout(handleHover);
};

/*
	Community Comments
*/

jQuery.show_reply_area = function(comment_id, init_comment ) {
	if (init_comment){
		jQuery('#comment_replies_' + comment_id).show();
	}
	jQuery('#comment_reply_placeholder_' + comment_id).hide();
	jQuery('#comment_reply_field_wrapper_' + comment_id).show();
	jQuery('#comment_reply_field_' + comment_id).focus();
	jQuery('#comment_reply_field_' + comment_id).val('');
};

jQuery.reply_field_blur = function(comment_id) {
	if (jQuery('#comment_reply_field_' + comment_id).val() == "") {
		jQuery('#comment_reply_field_wrapper_' + comment_id).hide();
		jQuery('#comment_reply_placeholder_' + comment_id).show();
	}
};

jQuery.submit_bulk_actions = function() {
	jQuery('#bulk_action').val(jQuery('bulk_select_box').val());
	jQuery('bulk_approval_form').submit();
};

jQuery.toggle_comment = function(id) {
	el = jQuery("#comment_video_" + id);
	
	if (jQuery(el).is(':visible')) {
		jQuery(el).hide();
	} else {
		jQuery(el).show();
	}
};

jQuery(function($){
	
	$('.vr_toggle_link').live('click', function(){
		jQuery.toggle_comment($(this).attr('id'));
		return false;
	});
	
	$('.single_comment_show_all').live('click', function(){
		id = $(this).attr('id').split('show_hidden_replies_');
		$(this).hide();
		$('#comment_hidden_replies_' + id[1]).show();
		return false;
	});
	
	$('.main_write_reply_link, .fake_reply_placeholder').live('click', function(){
		if ($(this).attr('class') == "fake_reply_placeholder"){
			id = $(this).closest('li').attr('id').split('comment_reply_placeholder_');
		} else {
			id = $(this).attr('id').split('reply_to_comment_');
		}
		
		if ($('#comment_reply_placeholder_' + id[1]).is(':visible')){
			newC = false;
		} else {
			newC = true;
		}
		
		$.show_reply_area(id[1], newC);
		return false;
	});
	
	// $("textarea[id^='comment_reply_field_']").live('blur', function(){
	// 	id = $(this).attr('id').split('comment_reply_field_');
	// 	$.reply_field_blur(id[1]);
	// });
	
	$('.funnel-testimonial-footer #back, .funnel-testimonial-footer #next').click(function() {
		var action = $(this).attr('id');
		var current = $('.single-testimonial:visible');
		if(action == 'next'){
			if($(current).attr('id') == 'kern'){
				$('#trey').fadeIn(2000);
			} else {
				$(current).next('.single-testimonial').fadeIn(2000);
			}
			$(current).animate({left: -300}, function(){
				$(current).hide();
				$(current).css('left', '0px');
			});
		} else {
			if($(current).attr('id') == 'trey'){
				$('#kern').fadeIn(2000);
			} else {
				$(current).css('left', '0px');
				$(current).prev('.single-testimonial').fadeIn(2000);
			}
			$(current).animate({left: +300}, function(){
				$(current).hide();
				$(current).css('left', '0px');
			});
		};
	});
	
	$('li.header-navigation-more-content').hoverIntent({    	
		interval: 50,
        sensitivity: 4,
		timeout: 50,
        over: function(){
			$(this).find('div.header-navigation-more-content-container').fadeIn('fast');
		},
        out: function(){
			$(this).find('div.header-navigation-more-content-container').fadeOut('fast');
		}
	});
	
	$('ul.header-navigation-list li.header-navigation-list-dashboard, ul.header-navigation-list li.header-navigation-more-content').hover(function() {
		$(this).find('a:first').addClass('header-main-navigation-link-hover');
	}, function() {
		$(this).find('a:first').removeClass('header-main-navigation-link-hover');
	});
	
	$('.header-navigation-more-content-item').hover( function(){
		$(this).find('a:first').addClass('header-navigation-submenu-level-1-hover');
	}, function(){
		$(this).find('a:first').removeClass('header-navigation-submenu-level-1-hover');
	});
	
	//$('.next_page').ajaxGet();

	$('.single_comment_show_all').live('click', function(){
	        id = $(this).attr('id').split('show_hidden_replies_');
	        $(this).hide();
	        $('#comment_hidden_replies_' + id[1]).show();
	        return false;
	    });
	
	$('#admin_preview_as').change(function(){	
	  $.cookie('admin_preview_as', $(this).val());
      location.reload();
  	});
	

	$('#new_comment, .new_reply').data("block", "Please verify you actually typed a message, and that Javascript is not disabled on your computer!");

	$("#comment_comment,  textarea[id^='comment_reply_field_']").change( function(){
		if($(this).val() != ''){
			$(this).closest('form').removeData("block");
		}else{
			$(this).closest('form').data("block", "Please verify you actually typed a message, and that Javascript is not disabled on your computer!");
		};
	});
	$('.new_reply, #new_comment').ajaxSubmit();	
	
	$('#comment_comment').focus( function(){
		$(this).val('');
	});
	
});

