/**
 * @author amwn
 */
var loading_img = '<img src="/images/ajax-loader.gif" border="0">';

function user_check_email( eaddress ){
	var post_url = "/user/check_email/";
    $.post(post_url, { 
		email: eaddress
    }, function(data){
		$('#check_email').html(data.message).fadeIn(500);
	}, "json");
}

function user_add_list(){
	var post_url = "/user/add_favorite/";
    $.post(post_url, { 
    }, function(data){
		switch( data.result )
		{
			case 'error':
			case 'create':
				open_dialog(data.title, data.message);
			break;
		}
	}, "json");
}

function user_save_favorite(lid){
    var post_url = "/user/save_favorite/";
    $.post(post_url, {
        loft_id: "" + lid + ""
    }, function(data){
		switch( data.result )
		{
			case 'select':
				open_dialog(data.title, data.message);
			break;
			
			case 'success':
				$('#favorite_count').html(data.count);
	            $("#user_favorites").animate({
	                backgroundColor: "#c15a21"
	            }, 500).animate({
	                backgroundColor: "#efefef"
	            }, 500).animate({
	                backgroundColor: "#c15a21"
	            }, 500).animate({
	                backgroundColor: "#efefef"
	            }, 500);
			break;
			
			case 'error':
			case 'create':
			case 'login':
				open_dialog(data.title, data.message);
			break;
		}
        
    }, "json");
}

function user_remove_favorite( list, lid )
{
	var post_url = "/user/remove_favorite/";
    $.post(post_url, {
        list_id: "" + list + "",
        loft_id: "" + lid + ""
    }, function(data){
		switch( data.result )
		{
			case 'success':
				$("#" + lid).fadeOut(1000, function() {
					$(this).remove();
				});
				$('#favorite_count').html(data.count);
	            $("#user_favorites").animate({
	                backgroundColor: "#c15a21"
	            }, 500).animate({
	                backgroundColor: "#efefef"
	            }, 500).animate({
	                backgroundColor: "#c15a21"
	            }, 500).animate({
	                backgroundColor: "#efefef"
	            }, 500);
			break;
			
			case 'error':
				open_dialog(data.title, data.message);
			break;
		}
    }, "json");
	build_favorites_list();
}

function user_remove_list( list )
{
	var post_url = "/user/remove_list/";
    $.post(post_url, {
        list_id: list
    }, function(data){
		switch( data.result )
		{
			case 'success':
				$("#list_" + list).fadeOut(2000).remove();
			break;
			
			case 'error':
				open_dialog(data.title, data.message);
			break;
		}
        
    }, "json");
	build_favorites_list();
}

function open_dialog(title, message){
	var did = Math.floor(Math.random()*100);
    var dialog_div = jQuery('<div id="' + did + '" />').appendTo(document.body);
    $("#" + did + "").html('<p>' + message + '</p>');
    $("#" + did + "").dialog({
        modal: true,
        overlay: {
            "background-color": "#000",
            "opacity": "0.8",
            "-moz-opacity": "0.8"
        },
        resizable: false,
        title: title,
        width: 300,
        closeable: true
    });
}

function show_gallery(lid){
    /* Call AJAX Gallery */
    $("#events_{loft_id}").hide('slow');
    var post_url = "/loft/gallery/";
    $("#gallery_" + lid).show();
    $("#gallery_" + lid).html(loading_img);
    $.post(post_url, {
        loft_id: "" + lid + ""
    }, function(data){
        if (data.result.length > 0) {
            $("#gallery_" + lid).html(data.result);
        }
        else {
            $("#gallery_" + lid).html('Gallery Not Available.');
        }
    }, "json"), function(){
        tb_reinit('a.thickbox, area.thickbox, input.thickbox');
    };
    
}

function show_events(lid){
    /* Call AJAX Gallery */
    var post_url = "/loft/events/";
    $("#events_" + lid).show();
    $("#events_" + lid).html(loading_img);
    $.post(post_url, {
        loft_id: "" + lid + ""
    }, function(data){
        if (data.result.length > 0) {
            $("#events_" + lid).html(data.result);
        }
        else {
            $("#events_" + lid).html('Events Not Available.');
        }
    }, "json");
}

function build_favorites_list()
{
	var post_url = "/user/get_favorites/";
    $.post(post_url, {
    }, function(data){
		switch( data.result )
		{
			case 'success':
				$('#user_favorites_list').html(data.content);
			break;
			
			case 'error':
				open_dialog(data.title, data.message);
			break;
		}
        
    }, "json");
}

function toggle( div )
{
	$('#' + div ).toggle();
}
