/**
 * Javascript Social Network Connector
 *
 */

$(document).ready(function() {
    if(typeof(userId) != 'undefined'){
        Connector.init(userId);
    }else{
        Connector.init();
    }
});

var Connector = function(){
    
    var debug        = false;
    var initialized  = false;
    var flashIsReady = false;
    var userid       = null;
    var flashMap     = null;
    
    var notifyFlash = function(funcName, data){
        alert('notifying flash: ' + funcName + ', ' + data);
    };
    
    var populateExtendedContent = function(url, data){
        $('#flash_map_drop_down_menu').slideUp("fast", function(){
            $(this).load(url, data, function(){
                $(this).slideDown('fast',function(){
                    $(this).find(":input:visible:enabled:first").focus();
                });
            });
        });
       
    };
    
    var log = function(data, args){
        if(debug){
            console.log(data, args);
        }
    }
    
    return{
        
        // initialize connector object
        init : function(iUserID){
            userid  = iUserID;
            initialized = true;
        },
        
        setDebug: function(enable){
            debug = enable;
            log('setDebug', arguments);
        },
        
        log : function(message){
            log(message);
        },
        
        // return the logged in userid
        getUser : function(){
            return userid;
        },
                
        // flash is ready
        flashReady : function(){
            log('flashReady', arguments);
            
            flashIsReady = true;
            
            // setup flash object
            flashMap = $('#flashmap').get(0);
        },
        
        // share map
        // ex: Connector.shareMap({title : 'My title', desc : 'My Desc', zoom : 5, lat : 45.1234, lng : -90.4321, layers : 'photo,video,radar', query : 'tag:winter'});
        shareMap : function(map){
            log('shareMap', map);
            
            // generate share URL based of map object
            url = 'http://' + domain + baseUrl + '/sharedmap?' + jQuery.param(map);
            
            var addThisHTML = '<a class="image_absolute image_replacement button_share" href="http://www.addthis.com/bookmark.php" onmouseout="addthis_close()" onclick="return addthis_open(this, \'\', \'' + url + '\', \'iMapWeather Shared Map\')">Share this map</a>';
            
            // display overlay with link
            modalComponent.showHtml('<p>Want to share this iMap you\'re viewing with someone else? Click the SHARE button below or copy the web address and email it to a friend.</p><br/>' + addThisHTML + '<br/><br/><textarea style="width: 100%; height: 125px;" onfocus="this.select();" onclick="this.focus();this.select();">' + url + '</textarea>');
            
        },
        
        // request the 
        addContentStart : function(type, lat, lng){
            log('addContentStart', arguments);
            
            data = {};
            data['content[type]'] = type;
            data['content[lat]']  = lat;
            data['content[lng]']  = lng;
            
            // load type-specific content add HTML
            populateExtendedContent(baseUrl + '/index/content/add', data );
            
        },
        
        // request the 
        addContentUpdate : function(lat, lng){
            log('addContentUpdate', arguments);
            
            $('#content-lat').val(lat);
            $('#content-lng').val(lng);
        },
        
        addContentCancel : function(){
            log('addContentCancel', arguments);
            Connector.hideExtendedContent();
        },
        
        // start edit content
        editContentStart : function(id){
            log('startEditContent', arguments);
            
            data = { id : id };
            
            // load type-specific content add HTML
            populateExtendedContent(baseUrl + '/index/content/edit', data );
        },
        
        // update edit content
        editContentUpdate : function(lat, lng){
            log('editContentUpdate', arguments);
            
            $('#content-lat').val(lat);
            $('#content-lng').val(lng);
        },
        
        editContentCancel : function () {
            log('editContentCancel', arguments);
            Connector.hideExtendedContent();
        },
                
        // request the 
        showExtendedContent : function(id, tab){
            log('showExtendedContent', arguments);
            
            var data = {
                id : id
            }
                        
            if(typeof tab != 'undefined' && tab != null){
                data['tab'] = tab;
            }
            
            populateExtendedContent(baseUrl+'/maps/extendedcontent/index', data );
        },
        
        // hide the extended content
        hideExtendedContent : function(){
            log('hideExtendedContent', arguments);
            $('#flash_map_drop_down_menu').hide('fast');
        },
        
        scrollToMap : function(){
            log('scrollToMap', arguments);
            $.scrollTo($(flashMap), 800, { offset:{top:-10} });
        },
        
        addCommentStart : function(id){
            log('addCommentStart', arguments);
            populateExtendedContent(baseUrl+'/index/comments/add', {content_id : id} );
        },
        
        addCommentDone : function(content_id){
            log('addCommentDone', arguments);
            Connector.showExtendedContent(content_id, 'comments');  
        },
        
        /**
         * Flash Methods
         */
        flash : {
            cancelAddContent : function(){
                log('cancelAddContent', arguments);
                if(flashIsReady){
                    flashMap.cancelAddContent();
                }
            },
            
            completeAddContent : function(){
                log('completeAddContent', arguments);
                
                if(flashIsReady){
                    flashMap.completeAddContent();
                }
            },
            
            showContent : function(id, tab){
                log('showContent', arguments);
                
                if(flashIsReady){
                    
                    if(typeof tab != 'undefined'){
                        flashMap.showContent(id, tab);
                    }else{
                        flashMap.showContent(id);
                    }
                    
                    Connector.scrollToMap();
                }
            },
            
            loadSavedMap : function(id){
                log('loadSavedMap', arguments);
                
                if(flashIsReady){
                    flashMap.loadSavedMap(id);
                    
                    Connector.scrollToMap();
                }
                
            },
            
            searchContent : function(q){
                log('searchContent', arguments);
                
                if(flashIsReady){
                    flashMap.searchContent(q);
                    Connector.scrollToMap();
                }
            },
            
            // start edit content
            startEditContent : function(id){
                log('startEditContent', arguments);
                
                if(flashIsReady){
                    flashMap.startEditContent(id);
                }
            },
                        
            // edit content complete
            completeEditContent : function(){
                log('completeEditContent', arguments);
                
                if(flashIsReady){
                    flashMap.completeEditContent();
                }
            },
            
            // save imap
            saveMap : function(){
                log('saveMap', arguments);
                
                if(flashIsReady){
                    flashMap.showSavedMapsManager('save');
                }
            },
            
            // save imap
            loadMap : function(){
                log('loadMap', arguments);
                
                if(flashIsReady){
                    flashMap.showSavedMapsManager('load');
                }
            },
            
            addContent : function(type){
                log('addContent', arguments);
                
                if(flashIsReady){
                    flashMap.addContent(type);
                    Connector.scrollToMap();
                }
                return false;
            }
        }
        
    }
}();
