/* Declare a namespace for the site */
var Site = window.Site || {};

jQuery(document).ready(function () {
	jQuery('#signInBox').hide();
	jQuery('#signIn').click(function () {
		jQuery('#signInBox').animate({
			opacity:'toggle',
			height:'toggle'
		}, 'slow');
	});
	
	jQuery(".scrollable").scrollable({ vertical: true, mousewheel: true, circular: true, speed: 1000 });	
	jQuery('.browse.down').hover(function(){ jQuery(".scrollable").data('scrollable').next(); });
	jQuery('.browse.up').hover(function(){ jQuery(".scrollable").data('scrollable').prev(); });
	init_pagination();
	init_selector();
	init_validation();
	auto_show_notifications();

});

function displayFeaturedPrize(prize_id)
{
	jQuery('div.prize').fadeOut(200);
	jQuery('#prize_'+prize_id).fadeIn(200);
}

function show_modal_error(message, options){
	default_options = 	{
			title: 		"Error",
			buttons: 	{
							"Ok": function(){ jQuery(this).dialog("destroy"); if(default_options.refresh_on_close == true) { location.reload(true); } }
						},
			modal:		true,
			refresh_on_close: 	false
		}
	jQuery.extend( default_options, options );
	jQuery("<div>"+message+"</div>").dialog(default_options);
}

function show_modal_confirm(message, options){
	default_options = {
			title: 		"Confirm",
			buttons: 	{
						"Ok"	 : true,
						"Cancel" : function(){ jQuery(this).dialog("destroy"); if(default_options.refresh_on_close == true) { location.reload(true); } }
						},
			modal:		true,
			refresh_on_close: 	false
	}
	jQuery.extend( default_options, options );
	jQuery("<div>"+message+"</div>").dialog(default_options);
}

function show_modal_message(message, options){
	default_options = {
			title: 		"Notice",
			buttons: 	{
						"Ok" : function(){ jQuery(this).dialog("destroy"); if(default_options.refresh_on_close == true) { location.reload(true); } }
						},
			modal:		true,
			refresh_on_close: 	false
	}
	jQuery.extend( default_options, options );
	jQuery("<div>"+message+"</div>").dialog(default_options);
}

function init_pagination()
{
	if(jQuery(".paginate").length > 0)
	{
		//jQuery("body").append(jQuery("<script type='text/javascript' src='/assets/js/jquery.pagination.js'></script>"));
		jQuery(".paginate").each(function(){
			jQuery(this).paginate({
				item_container: jQuery(this).children("div:first"),
				items_per_page: 9, 
				nav_panel_id: '.paging',
				nav_label_first : 'First',
				nav_label_prev : 'Prev',
				nav_label_next : 'Next',
				nav_label_last : 'Last',
				num_page_links_to_display: 5
			});
		});
	}
}

// Dropdown selector options found in user profile, friends, elsewhere.
function init_selector()
{	
	jQuery('.selectorInfo').hide();
	jQuery('.selector').each(function() {
		jQuery(this).click(function (e) {
		mouse_is_inside=false;
			jQuery('.selectorInfo').hide();
			jQuery(this).next('.selectorInfo')
			.animate({
			opacity: 'toggle',
			height: 'toggle'
			}, 'fast') 
			.hover(function(){ 
			mouse_is_inside=true; 
			}, function(){ 
			mouse_is_inside=false; 
			});
		jQuery(document).mouseup(function(){
			if(! mouse_is_inside) jQuery('.selectorInfo').hide();
		});
		});
	});
}

validator_opts = {};
custom_validator_opts = {}

function init_validation()
{
	validator_opts = jQuery.extend({},validator_opts,custom_validator_opts);
	jQuery('form.validate').each(function(){
		jQuery(this).validate(validator_opts);
	});
}


// Process user action.
// This is used primarily for friend requests.
function do_action(action, id)
{		
	var url = '/friends/' + action + '/' + id;
	jQuery.ajax({
	  url: url,
	  success: process_response,
	  dataType: 'json'
	});
}

// Display server response from a user action.
// This is used primarily for friend requests.
function process_response(data)
{
	var message_output = '';
	
	switch (data.response)
	{
		case 'error':
			
			jQuery.each(data.messages, function(key, message){
				message_output += ('<p>' + message + '</p>');
			});
					
		break;
		
		case 'success':		
			
			jQuery.each(data.messages, function(key, message){
				message_output += ('<p>' + message + '</p>');
			});
			
		break;
	}
	
	jQuery('<div id="notification_dialog">' + message_output + '</div>').dialog({title: 'Friend Request'});
	
}


/**
 * Add notification to slidedown, display the slidedown.
 * Initializes the slidedown on first call.
 * @param string message
 */
var notifications_initialized = false;
function add_notification(message, type)
{
	if(type == undefined || type == '')
	{
		type = 'error';
	}
	if (!notifications_initialized)
	{
		jQuery('#notification-messages').append('<ul id="notifications-list"></ul>');
		notifications_initialized = true;
	}
	
	jQuery('#notifications-list').append('<li class="alert '+type+'">' + message + '<img src="/assets/images/button_cancel.png" onclick="clearThis(this);"></li>');
	show_notifications('fixed');
}

/**
 * Flush notifications out of notification bar.
 */
function clear_notifications()
{
	jQuery('#notification-messages').html('');
	notifications_initialized = false;
}

/**
 * Autoshow notificaitons on page load.
 */
function auto_show_notifications()
{
	var display_notifications = jQuery('#display_notifications').val();	
	show_notifications(display_notifications);
}

function clearThis(el)
{
	jQuery(el).parent().slideUp();
	setTimeout(function(){jQuery(el).parent().remove()}, 1000);
	setTimeout(function(){ 
		if(jQuery('#notifications-list').children(':visible').length == 0)
		{
			hide_notifications();
		}
	}, 1200);
	return false;
}
/**
 * Display notification messages according to display setting.
 */
function show_notifications(display)
{	
	switch (display)
	{
		case 'fixed':
			
			jQuery('#notification').slideDown('slow');
			
		break;
		
		case 'quick-fade':
		
			jQuery('#notification').slideDown('slow');
			setTimeout(function(){jQuery('#notification').slideUp('slow')}, 5000);
			
		break;
		
		case 'fade':
		
			jQuery('#notification').slideDown('slow');
			setTimeout(function(){jQuery('#notification').slideUp('slow')}, 10000);
			
		break;
	}
}

/**
 * Manually hide the notification bar.
 */
function hide_notifications()
{
	jQuery('#notification').slideUp('slow');
	setTimeout('clear_notifications()', 1000);
}

