$(document).ready(function() {
	
	$('.topMenuAction').click( function() {
		if($('#openCloseIdentifier').is(':hidden')) {
			return true;
			$('#slider').animate({
				marginTop: "-115px"
			}, 500);
			$("#topMenuImage").html('');
			$("#openCloseIdentifier").show();
			return false;
		} else {
			$("#slider").animate({
				marginTop: "0px"
			}, 500 );
			$("#topMenuImage").html('');
			$("#openCloseIdentifier").hide();
		}
		return false;
	});  
				
	$('a.iframe').fancybox({
		width: 854,
		height: 480
	});
	
	$('.playlist a').live('click', function() {
		var char = (window.location.href.indexOf('?') == -1) ? '?' : '&';
		var playlist_id = $(this).siblings('span:hidden').text();
		var eq = $('div.video-list-container').index($(this).parents('div.video-list-container'));
		$(this).parents('div.video-list-container').load(window.location.href + char + 'playlist=' + playlist_id + ' div.video-list-container:eq(' + eq + ') > *').fadeIn();
		return false;
	});
	
	$('.video-list-title a').live('click', function() {
		var eq = $('div.video-list-container').index($(this).parents('div.video-list-container'));
		$(this).parents('div.video-list-container').load(window.location.pathname + ' div.video-list-container:eq(' + eq + ') > *').fadeIn();
		return false;
	});
	
	$('.video-list-controls a').live('click', function() {
		var page = $(this).parent().parent().siblings('span.current-page').html();
		if($(this).html() == 'Prev') {
			page--;
		} else if($(this).html() == 'Next') {
			page++;
		}
		var char = (window.location.href.indexOf('?') == -1) ? '?' : '&';
		var eq = $('div.video-list-container').index($(this).parents('div.video-list-container'));
		$(this).parents('div.video-list-container').load(window.location.href + char + 'page=' + page + ' div.video-list-container:eq(' + eq + ') > *').fadeIn();
		return false;
	});
	
	$('.comments-controls a').live('click', function() {
		var page = $(this).parent().parent().siblings('span.current-page').html();
		if($(this).html() == 'Prev') {
			page--;
		} else if($(this).html() == 'Next') {
			page++;
		}
		var char = (window.location.href.indexOf('?') == -1) ? '?' : '&';
		$(this).parents('#comments').load(window.location.href + char + 'page=' + page + ' #comments > *').fadeIn();
		return false;
	});
	
	$('#comments-form').live('submit', function() {
		$('input, textarea', this).attr('disabled', 'disabled');
		$.ajax({
			url: '/ajax/comment.php',
			data: {
				'vid': $('#comments-form span:first-child').text(),
				'msg': $('#comment-message').val()
			},
			type: 'post',
			success: function(data) {
				if(data == '1') {
					$('#comments-form').fadeOut();
					$('#comments').load(window.location.href + ' #comments > *').fadeIn();
				}
			}
		});
		return false;
	});
	
/**
* hoverIntent r6 // 2011.02.26 // jQuery 1.5.1+
* <http://cherne.net/brian/resources/jquery.hoverIntent.html>
* 
* @param  f  onMouseOver function || An object with configuration options
* @param  g  onMouseOut function  || Nothing (use configuration options object)
* @author    Brian Cherne brian(at)cherne(dot)net
*/
(function($){$.fn.hoverIntent=function(f,g){var cfg={sensitivity:7,interval:100,timeout:0};cfg=$.extend(cfg,g?{over:f,out:g}:f);var cX,cY,pX,pY;var track=function(ev){cX=ev.pageX;cY=ev.pageY};var compare=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);if((Math.abs(pX-cX)+Math.abs(pY-cY))<cfg.sensitivity){$(ob).unbind("mousemove",track);ob.hoverIntent_s=1;return cfg.over.apply(ob,[ev])}else{pX=cX;pY=cY;ob.hoverIntent_t=setTimeout(function(){compare(ev,ob)},cfg.interval)}};var delay=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);ob.hoverIntent_s=0;return cfg.out.apply(ob,[ev])};var handleHover=function(e){var ev=jQuery.extend({},e);var ob=this;if(ob.hoverIntent_t){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t)}if(e.type=="mouseenter"){pX=ev.pageX;pY=ev.pageY;$(ob).bind("mousemove",track);if(ob.hoverIntent_s!=1){ob.hoverIntent_t=setTimeout(function(){compare(ev,ob)},cfg.interval)}}else{$(ob).unbind("mousemove",track);if(ob.hoverIntent_s==1){ob.hoverIntent_t=setTimeout(function(){delay(ev,ob)},cfg.timeout)}}};return this.bind('mouseenter',handleHover).bind('mouseleave',handleHover)}})(jQuery);	

	$("#global-nav li").hoverIntent({    
		sensitivity: 3, // number = sensitivity threshold (must be 1 or higher)    
		interval: 50, // number = milliseconds for onMouseOver polling interval    
		timeout: 500, // number = milliseconds delay before onMouseOut    
		over:function(){
			$(this).addClass("hovering");
		},
		out: function(){
			$(this).removeClass("hovering");
		}
	});	
	
	$('#preview-bar .toggle').click(function() {
		$(this).parent().slideUp();
		return false;
	});
	
	var set_footer = function() {
		
		frgb = [];
		frgb = $('html').css('background-color').match(/^rgba?\((\d+),\s*(\d+),\s*(\d+)(?:,\s*(\d+))?\)$/);
		
		if(!$.isArray(frgb)) {
			$('#footer').addClass('dark-elements');
			return false;
		}
		
		total = Number(frgb[1]) + Number(frgb[2]) + Number(frgb[3]);
		median = total / 2;
		fclass = median < 192 ? 'light-elements' : 'dark-elements';
		
		$('#footer').addClass(fclass);
	};
	
	set_footer();
	
});
