/**
 * Handles all Lightboxes
 */

(function(){
    if(!window.cs) {
        return;
    }
    
    var lightbox = window.cs.lightbox = {
        // default options/settings
        empty: null
    };
    
    lightbox.init = function() {
        // initialize default actions
        // lightbox.initDemo();
    };


    lightbox.startGathering = function(businessID, otherOptions) {
        // create modal box
        var businessID = businessID != 0 ? businessID : '';
        if (businessID == -1) {
            // Get a businessID for the lightbox, used in case user wants to log in lightbox
            $.getJSON('/getLocationID.json?address='+otherOptions.address+'&name='+otherOptions.name+'&rating='+otherOptions.rating+'&phone='+otherOptions.phone+'&city='+otherOptions.city+'&state='+otherOptions.state+'&zip='+otherOptions.zip+'&latitude='+otherOptions.latitude+'&longitude='+otherOptions.longitude+'&imageurl='+otherOptions.imageurl,
                function(json){
                    if (json.success) {
                        setTimeout('cs.lightbox.startGathering('+json.businessID+');', 100);
                        return false;
                    } else {
                        cs.lightbox.message(json.message, 'Start Gathering Error');
                    }
                }
            );
        }
        var options = otherOptions ? otherOptions : {ajax: '/lightbox/startGathering.php?businessID='+businessID};
        var divinfo = {
            newid: 'startGatheringBox',
            newclass: 'long'
        };
        cs.lbFactory.newModalBox(options, divinfo, true);
        return false;
    }
	
	lightbox.startEventGathering = function(eventID) {
        var options = {ajax: '/lightbox/startGathering.php?eventID='+eventID};
        var divinfo = {
            newid: 'startGatheringBox',
            newclass: 'long'
        };
        cs.lbFactory.newModalBox(options, divinfo, true);
        return false;
    }
	
	lightbox.eventShare = function(data) {
		var dataString = '';
		if(data) {
			$.each(data, function(key, value) {
				dataString += '&' + key + "=" + value;
			});
		}
		
		// Get the eventID from the main page
		eventID = parseInt($('#share_eventID').val());
		if(isNaN(eventID) || eventID <= 0) {
			return;
		}
		
		var options = {ajax: '/lightbox/eventShare.php?eventID='+eventID + dataString};
        var divinfo = {
            newid: 'eventShareBox',
            newclass: 'long'
        };
		
        cs.lbFactory.newModalBox(options, divinfo, true);
        return false;
    }
	
	lightbox.phoneNumber = function(data) {
        var dataString = '';
		if(data) {
			$.each(data, function(key, value) {
				dataString += '&' + key + "=" + value;
			});
		}
		if(dataString != '') {
			dataString = '?' + dataString;
		}
		
        var options = {ajax: '/lightbox/phoneNumber.php' + dataString};
        var divinfo = {
            newid: 'phoneNumberBox'
        };
        cs.lbFactory.newModalBox(options, divinfo, true);
        return false;
    }
    
    lightbox.editGathering = function(gatheringID, otherOptions) {
        // create modal box
        var options = otherOptions ? otherOptions : {ajax: '/lightbox/editGathering.php?gatheringID='+gatheringID};
        var divinfo = {
            newid: 'editGatheringBox',
            newclass: 'long'
        };
        cs.lbFactory.newModalBox(options, divinfo, true);
        return false;
    }    
    
    lightbox.join = function(otherOptions) {
        var options = otherOptions ? otherOptions : {ajax: '/lightbox/join.php'};
        var divinfo = {
            newid: 'joinBox'
        };
        cs.lbFactory.newModalBox(options, divinfo, true);
        return false;
    }
    
    lightbox.forgotPassword = function(otherOptions) {
        var options = otherOptions ? otherOptions : {ajax: '/lightbox/forgotPassword.php'};
        // create modal box
        var divinfo = {
            newid: 'forgotPasswordBox'
        };
        cs.lbFactory.newModalBox(options, divinfo, true);
        return false;
    }
    
    lightbox.changeLocation = function(otherOptions) {
        // create modal box
        var options = otherOptions ? otherOptions : {ajax: '/lightbox/changeLocation.php'};
        var divinfo = {
            newid: 'changeLocationBox'
        };
        cs.lbFactory.newModalBox(options, divinfo, true);
        return false;
    }
    
    lightbox.removeUpcomingGathering = function(gatheringID) {
        // create modal box
        var options = {
            ajax: '/lightbox/removeUpcomingGathering.php?gatheringID='+gatheringID
        };
        var divinfo = {
            newid: 'removeUpcomingGatheringBox',
            newclass: 'short'
        };
        cs.lbFactory.newModalBox(options, divinfo, true);
        return false;
    }
    
    lightbox.removeCrewMember = function(userID) {
        // create modal box
        var options = {
            ajax: '/lightbox/removeCrewMember.php?userID='+userID
        };
        var divinfo = {
            newid: 'removeCrewMemberBox',
            newclass: 'short'
        };
        cs.lbFactory.newModalBox(options, divinfo, true);
        return false;
    }
    
    lightbox.removeGroup = function(groupID) {
        // create modal box
        var options = {
            ajax: '/lightbox/removeGroup.php?groupID='+groupID
        };
        var divinfo = {
            newid: 'removeGroupBox',
            newclass: 'short'
        };
        cs.lbFactory.newModalBox(options, divinfo, true);
        return false;
    }        

    lightbox.createGroup = function(crewID, page) {
        // page == 0 for dashboard, 1 for crew
        // create modal box
        var options = {
            ajax: '/lightbox/createGroup.php?crewID='+crewID+'&page='+page
        };
        var divinfo = {
            newid: 'createGroupBox'
        };
        cs.lbFactory.newModalBox(options, divinfo, true);
        return false;
    }
    
    lightbox.cancelGathering = function(gatheringID) {
        // create modal box
        var options = {
            ajax: '/lightbox/cancelGathering.php?gatheringID='+gatheringID
        };
        var divinfo = {
            newid: 'cancelGatheringBox',
            newclass: 'short'
        };
        cs.lbFactory.newModalBox(options, divinfo, true);
        return false;
    }
    
    lightbox.addCrew = function(userID) {
        if ($('#isVisitorLoggedIn').val() == 0) {
            return cs.lightbox.loginRegister();
        }
        // create modal box
        var options = {
            ajax: '/lightbox/addCrew.php?userID='+userID
        };
        var divinfo = {
            newid: 'addCrewBox',
            newclass: 'short'
        };
        cs.lbFactory.newModalBox(options, divinfo, true);
        return false;
    }    

    lightbox.claimBusiness = function(businessID,businessTitle, otherOptions) {
        // create modal box
        var options = otherOptions ? otherOptions : {ajax: '/lightbox/claimBusiness.php?businessID='+businessID+'&businessTitle='+encodeURI(businessTitle)};
        var divinfo = {
            newid: 'claimBusinessBox'
        };
        cs.lbFactory.newModalBox(options, divinfo, true);
        return false;
    }
    
    lightbox.learnMore = function() {
        // create modal box
        var options = {
            ajax: '/lightbox/learnMore.php'
        };
        var divinfo = {
            newid: 'learnMoreBox'
        };
        cs.lbFactory.newModalBox(options, divinfo, true);
        return false;
    }
    
    lightbox.loginRegister = function(otherOptions) {
        // create modal box
        var options = otherOptions ? otherOptions : {ajax: '/lightbox/loginRegister.php'};
        var divinfo = {
            newid: 'loginRegisterBox'
        };
        cs.lbFactory.newModalBox(options, divinfo, true);
        return false;
    }
    
    lightbox.buildCrew = function() {
        // create modal box
        var options = {
            ajax: '/lightbox/buildCrew.php'
        };
        var divinfo = {
            newid: 'buildCrewBox'
        };
        cs.lbFactory.newModalBox(options, divinfo, true);
        return false;
    }
    
    lightbox.inviteGathering = function(gatheringID, quickUrl) {
        // create modal box
        var options = {
            ajax: '/lightbox/inviteGathering.php?gatheringID='+gatheringID+'&quickUrl='+quickUrl
        };
        var divinfo = {
            newid: 'inviteGatheringBox',
            newclass: 'long'            
        };
        cs.lbFactory.newModalBox(options, divinfo, true);
        return false;
    }
    
    lightbox.setVanityURL = function() {
        // create modal box
        var options = {
            ajax: '/lightbox/setVanityURL.php'
        };
        var divinfo = {
            newid: 'setVanityURLBox',
            newclass: 'short'
        };
        cs.lbFactory.newModalBox(options, divinfo, true);
        return false;
    }                                                    
    
    lightbox.message = function(message, title, onHideFunction) {
        // create modal box
        var options = {
            ajax: '/lightbox/message.php?message='+escape(message)+'&title='+escape(title),
            onHide: onHideFunction
        };
        var divinfo = {
            newid: 'messageBox',
            newclass: 'short'
        };
        cs.lbFactory.newModalBox(options, divinfo, true);
        return false;
    }
        

})();
