/*	JS Document 
 *	Author: Tashreek Jackson
 *	Overview: jQuery Functions.	
*/

jQuery.fn.inputHints=function() {
    // hides the input display text stored in the title on focus
    // and sets it on blur if the user hasn't changed it.

    // show the display text
    $(this).each(function(i) {
        $(this).val($(this).attr('title'));
    });

    // hook up the blur & focus
    $(this).focus(function() {
        if ($(this).val() == $(this).attr('title'))
            $(this).val('');
    }).blur(function() {
        if ($(this).val() == '')
            $(this).val($(this).attr('title'));
    });
};


$(document).ready(function() {
	
	$('input[title]').inputHints();

	$("a.iframe").click(function() {
		$.fancybox({
			'showCloseButton' : false,
			'titlePosition' : 'inside',
			'titleFormat' 	: frameTitle,
			//'padding'		: 12,
			'autoScale'		: false,
			'transitionIn'	: 'none',
			'transitionOut'	: 'none',
			//'title'			: this.title,
			'width'			: 640,
			'height'		: 550,
			'href'			: this.href,
			'type'			: 'iframe',
		});
		return false;
	});
	
	$(".show-vid").click(function() {
		$.fancybox({
			'showCloseButton' : false,
			'titlePosition' : 'inside',
			'titleFormat' 	: formatTitle,
			'padding'		: 12,
			'autoScale'		: false,
			'transitionIn'	: 'none',
			'transitionOut'	: 'none',
			'title'			: this.title,
			'width'			: 640,
			'height'		: 385,
			'href'			: this.href.replace(new RegExp("watch\\?v=", "i"), 'v/'),
			'type'			: 'swf',
			'swf'			: {
				 'wmode'			: 'transparent',
				 'allowfullscreen'	: 'true'
			}
		
		});
		return false;
	});
	
	$("a.l24").fancybox({
			'showCloseButton' : false,
			'titlePosition' : 'inside',
			'titleFormat' 	: formatTitle
		});
	
	$(".sortby").change(function () {
    	$("form#filter_leaderboard").submit();
	});
	
	///////////////////////////////////////////////////////////////////////////
	
	$(".vote").click(function (e) {	   
		var user_id = this.rel;
		var cookieName = 'vote' + this.rel;
		
		if($.cookie(cookieName) == this.rel)
		{
			$(e.target).parent().html('<p class="r-thanks">You\'ve already voted.</p>');
		}
		else{
			$.cookie(cookieName, this.rel, { expires: 1 });
			
			$.ajax({
				type: 'POST',
				url: '/profile/vote',
				data: { user_id: user_id, vote: 1 },
				success: function(data){
					$('#loading').html('<p class="r-thanks">Thank you.</p>');
				},
				error: function(req,error){
				  if(error === 'error'){error = req.statusText;}
				  var errormsg = 'There was a communication error: '+error;
				  $(e.target).parent().html(errormsg);
				},
				beforeSend: function(data){
					$(e.target).parent().html('<div id="loading"><img class="preloader-small" src="/assets/images/bg/preloader.gif" alt="Loading..." /></div>');
				}
			});
		}

	});
	
	
	///////////////////////////////////////////////////////////////////////////
	
	$(".vote").click(function (e) {	   
		var user_id = this.rel;
		var cookieName = 'vote' + this.rel;
		
		if($.cookie(cookieName) == this.rel)
		{
			$(e.target).parent().html('<p class="r-thanks">You\'ve already voted.</p>');
		}
		else{
			$.cookie(cookieName, this.rel, { expires: 1 });
			
			$.ajax({
				type: 'POST',
				url: '/profile/vote',
				data: { user_id: user_id, vote: 1 },
				success: function(data){
					$('#loading').html('<p class="r-thanks">Thank you.</p>');
				},
				error: function(req,error){
				  if(error === 'error'){error = req.statusText;}
				  var errormsg = 'There was a communication error: '+error;
				  $(e.target).parent().html(errormsg);
				},
				beforeSend: function(data){
					$(e.target).parent().html('<div id="loading"><img class="preloader-small" src="/assets/images/bg/preloader.gif" alt="Loading..." /></div>');
				}
			});
		}

	});
	
	///////////////////////////////////////////////////////////////////////////
	
	$("#click-to-vote").click(function (e) {
		var user_id = this.rel;
		var cookieName = 'vote' + this.rel;
		
		if($.cookie(cookieName) == this.rel)
		{
			$(e.target).parent().html('<p class="r-thanks">You\'ve already voted.</p>');
		}
		else{
			$.cookie(cookieName, this.rel, { expires: 1 });
			$.ajax({
				type: 'POST',
				url: '/profile/vote',
				data: { user_id: user_id, vote: 1 },
				success: function(data){
					$('#loading').html('<p class="r-thanks">Thank you.</p>');
				},
				error: function(req,error){
				  if(error === 'error'){error = req.statusText;}
				  var errormsg = 'There was a communication error: '+error;
				  $(e.target).parent().html(errormsg);
				},
				beforeSend: function(data){
					$(e.target).parent().html('<div id="loading"><img class="preloader-small" src="/assets/images/bg/preloader2.gif" alt="Loading..." /></div>');
				}
			});
		}
	});

	
});

function formatTitle(title, currentArray, currentIndex, currentOpts) {
    return '<div id="fancybox-b-close"><a href="javascript:;" onclick="$.fancybox.close();"><img src="/assets/images/btns/closelabel.gif" /></a></div>';
}


function frameTitle(title, currentArray, currentIndex, currentOpts) 
{
    return '<div id="fancybox-b-close"><a href="javascript:;" onclick="$.fancybox.close();"><img src="/assets/images/btns/closelabel.gif" /></a></div>';
}

