/*
inLocation JSON & XML-RPC API

AJAX URL: http://api.inlocation.ca/ajax
XML-RPC URL: http://api.inlocation.ca/

*/

$.inLoc = {};
$.inLoc.callQueue = [];
$.inLoc.settings = {
        apikey: 'ea355321-81c4-450d-88b4-1a5aa1cc8e71',
        session: '',
        tg_format: 'application/json'
    };

$.inLoc.call                            = function(fn, callback, options){
    if ( !options ) var options = {};
    
    if ( $.inLoc.settings.session == '' && fn != 'session' ) {
        // No session has been established yet, so for now just add the call to the queue.
        $.inLoc.callQueue.push({'fn': fn, 'callback': callback, 'options': options});
        return
    }
    
    var data = $.extend({}, $.inLoc.settings, options);

    $.ajax({
        cache: false,
        success: callback,
        dataType: 'json',
        type: 'post',
        url: inLocationAPIBase + fn,
        data: data
    });
};

$.inLoc.fnTemplate                      = function(fn, mycall){
    var func = function(callback, options){
        return $.inLoc.call(fn, callback, options);
    };
    return func;
};

// session() - Create a new session or continue an existing one.  The value stored in $.inLoc.settings.session should be saved to a cookie, then repopulated on subsequent page loads!
$.inLoc.onSession                       = function(data){
    if ( data.status == 'ok' ) {
        $.inLoc.settings.session = data.result;
        
        $.cookie('INSession', data.result, {path: '/', expires: 7});
        
        $('body').data('INSession', data);
        
        // $('#manage-searches span').text(data.counts.searches);
        $('#manage-lightbox span:first').text(data.counts.lightbox.locations).parent().find('span:last').text(data.counts.lightbox.images);
        $('#manage-portfolio span').text(data.counts.portfolios);
        
        $('#manage-lightbox div, #manage-portfolio div').fadeIn('slow');
        
        for ( var i in $.inLoc.callQueue ) {
            $.inLoc.call($.inLoc.callQueue[i].fn, $.inLoc.callQueue[i].callback, $.inLoc.callQueue[i].options);
        }
        
        $.inLoc.callQueue = [];
    } else {
        $.flash.error('Session Error', "Unable to restore your session.  Please contact site support staff.");
    }
};

$.inLoc.session                         = function(){
    var fn = $.inLoc.fnTemplate('session');
    fn($.inLoc.onSession, {session: $.cookie('INSession')});
};

$.inLoc.session.authenticate            = $.inLoc.fnTemplate('authenticate');
$.inLoc.session.destroy                 = $.inLoc.fnTemplate('destroySession');

$.inLoc.account                         = {};
$.inLoc.account.create                  = $.inLoc.fnTemplate('createAccount');

$.inLoc.resource                        = {};
$.inLoc.resource.message                = $.inLoc.fnTemplate('messageResource');

// location(, {id}) - Load location record detail, including top three image IDs.
// location.images(id) - Load complete list of all image IDs for a given location.
$.inLoc.location                        = {};
$.inLoc.location.get                    = $.inLoc.fnTemplate('getLocation');
$.inLoc.location.create                 = $.inLoc.fnTemplate('createLocation');
$.inLoc.location.update                 = $.inLoc.fnTemplate('updateLocation');
$.inLoc.location.update.category        = $.inLoc.fnTemplate('updateLocationCategory');
$.inLoc.location.update.address         = $.inLoc.fnTemplate('updateAddress');
$.inLoc.location.remove                 = $.inLoc.fnTemplate('deleteLocation');
$.inLoc.location.proximity              = {};
$.inLoc.location.proximity.add          = $.inLoc.fnTemplate('addProximity');
$.inLoc.location.proximity.remove       = $.inLoc.fnTemplate('removeProximity');
$.inLoc.location.lightbox               = $.inLoc.fnTemplate('addLocationToLightbox');
$.inLoc.location.lightbox.remove        = $.inLoc.fnTemplate('removeLocationFromLightbox');
$.inLoc.location.lightbox.order         = $.inLoc.fnTemplate('orderLocationInLightbox');
$.inLoc.location.images                 = $.inLoc.fnTemplate('getLocationImages');
$.inLoc.location.image                  = {};
$.inLoc.location.image.remove           = $.inLoc.fnTemplate('removeImage');
$.inLoc.location.image.lightbox         = $.inLoc.fnTemplate('addImageToLightbox');
$.inLoc.location.image.lightbox.remove  = $.inLoc.fnTemplate('removeImageFromLightbox');
$.inLoc.location.image.lightbox.order   = $.inLoc.fnTemplate('orderImageInLightbox');
$.inLoc.location.image.position         = $.inLoc.fnTemplate('setLocationImagePosition');
$.inLoc.location.image.update           = $.inLoc.fnTemplate('updateImageDetail')
$.inLoc.location.contact                = $.inLoc.fnTemplate('getLocationContact');
$.inLoc.location.contact.set            = $.inLoc.fnTemplate('setLocationContact');

$.inLoc.debug                           = $.inLoc.fnTemplate('debug');
$.inLoc.config                          = $.inLoc.fnTemplate('config');

// getImage(id) - Get full image details - uploader ID, resolution, title, description, and tags.
$.inLoc.image                           = $.inLoc.fnTemplate('getImage');
$.inLoc.image.update                    = $.inLoc.fnTemplate('updateImage');
$.inLoc.image.remove                    = $.inLoc.fnTemplate('deleteImage');
$.inLoc.image.scale                     = $.inLoc.fnTemplate('scaleImage');
$.inLoc.image.move                      = $.inLoc.fnTemplate('moveImage');
$.inLoc.image.dimensions                = $.inLoc.fnTemplate('getImageDimensions');

// performSearch(category, kind, text, limit) - Return search results 
$.inLoc.search                          = $.inLoc.fnTemplate('search');
$.inLoc.search.save                     = $.inLoc.fnTemplate('saveSearch');
$.inLoc.search.remove                   = $.inLoc.fnTemplate('removeSearch');
$.inLoc.search.load                     = $.inLoc.fnTemplate('getSavedSearch');
$.inLoc.searches                        = $.inLoc.fnTemplate('getSavedSearches');
                                        
$.inLoc.lightbox                        = $.inLoc.fnTemplate('getLightbox');
$.inLoc.lightbox.clone                  = $.inLoc.fnTemplate('cloneLightbox');
$.inLoc.lightbox.empty                  = $.inLoc.fnTemplate('clearLightbox');
$.inLoc.portfolio                       = $.inLoc.fnTemplate('getPortfolio');
$.inLoc.portfolio.update                = $.inLoc.fnTemplate('updatePortfolioDetail');
$.inLoc.portfolio.group                 = {};
$.inLoc.portfolio.group.create          = $.inLoc.fnTemplate('createPortfolioGroup');
$.inLoc.portfolio.group.remove          = $.inLoc.fnTemplate('removePortfolioGroup');
$.inLoc.portfolio.group.update          = $.inLoc.fnTemplate('updatePortfolioGroup');
$.inLoc.portfolio.group.order           = $.inLoc.fnTemplate('orderGroupInPortfolio');
$.inLoc.portfolio.group.image           = {};
$.inLoc.portfolio.group.image.add       = $.inLoc.fnTemplate('addObjectToPortfolioGroup');
$.inLoc.portfolio.group.image.remove    = $.inLoc.fnTemplate('removeImageFromPortfolio');

$.inLoc.categories                      = $.inLoc.fnTemplate('getCategories');
$.inLoc.regions                         = $.inLoc.fnTemplate('getRegions');

$.inLoc.contact                         = $.inLoc.fnTemplate('getContact');

$(function(){ $.inLoc.session(); });
