//jQuery center functon to function both vertically and horizontally
jQuery.fn.center = function () {
    this.css("top", ( jQuery(window).height() - this.height() ) / 2+jQuery(window).scrollTop() + "px");
    this.css("left", ( jQuery(window).width() - this.width() ) / 2+jQuery(window).scrollLeft() + "px");
    return this;
} 


var SERVICES = {
    //overlay to display processing status
    hideAll: function(containerId){
        jQuery('#'+containerId+' .dyn_panel').css('display', 'none');
    },
		
    showProcessing: function(containerId) {
        SERVICES.hideAll(containerId);
        jQuery('#'+containerId+' #dyn_processing').css('display', '');
    },

    showSuccess: function(containerId) {
        SERVICES.hideAll(containerId);
        jQuery('#'+containerId+' #dyn_success').css('display', '');
    },

    showError: function(containerId) {
        SERVICES.hideAll(containerId);
        jQuery('#'+containerId+' #dyn_error').css('display', '');
    },

    //requires updateShareOptions() to be called first
    validateSubscribe:	function() {
        var errors = new Array;
        if(jQuery('input#name').val() == '')
            errors.push('Please enter your full name');

        if( !UTILS.isValidEmail(jQuery('input#email').val()) )
            errors.push('Please enter a valid email address' );

        if(errors.length)
        {
            alert(errors.join('\n'));
            return false;
        }
        else
        {
            return true;
        }
    },

    //fire off the AJAX request to deliver client message
    subscribeToOffers: function() {
        if( SERVICES.validateSubscribe() )
        {
            SERVICES.showProcessing('offers_outer');
            jQuery.ajax({
                url: "/webservices/subscribe.php",
                global: false,
                type: 'post',
                data: {
                    'name' : jQuery('input#name').val(),
                    'email' : jQuery('input#email').val()
                },
                dataType: "json",
                success: function(response){
                    if(response.success == true)
                        SERVICES.showSuccess('offers_outer');
                    else
                        SERVICES.showError('offers_outer');
                },
                error: function(response){
                    SERVICES.showError(response);
                }
            }
            );
        }
    },

    //requires updateShareOptions() to be called first
    validateSMS: function() {
        if(jQuery('input#sms_to_phone').val() == '')
        {
            alert('Please enter your mobile phone number.');
            return false;
        }
        else
        {
            return true;
        }
    },
		
    sendSMS: function() {
        if( this.validateSMS() )
        {
            var isIE6 = (jQuery.browser.msie && jQuery.browser.version < 7);
            var isIE7 = (jQuery.browser.msie && jQuery.browser.version < 8);
            if(!isIE6 && !isIE7)
                SERVICES.showProcessing('sms_outer');
				
            //Fire ROI tracking function
            ROILabs_trackInfoToPhone();
				
            jQuery.ajax({
                url: "/webservices/sms.php",
                global: false,
                type: 'post',
                data: {
                    'sms_email_form' : jQuery('select#sms_email_form').val(),
                    'sms_to_phone' 	 : jQuery('input#sms_to_phone').val()
                },
                dataType: "json",
                success: function(response){
                    if(response.success == true)
                        if(!isIE6 && !isIE7)
                            SERVICES.showSuccess('sms_outer');
                        else
                            jQuery('#sms_outer .close_btn').click();
                    else
                        SERVICES.showError('sms_outer');
                },
                error: function(response){
                    SERVICES.showError('sms_outer');
                }
            }
            );
        }
    }
};


//Helper Functions
var UTILS = {
    getFormattedDate: function()
    {
        var d = new Date();
        return (d.getMonth() + 1) + '/' + d.getDate() + '/' + d.getFullYear();
    },
	
    isValidEmail: function(addr)
    {
        var reg = new RegExp('^[0-9a-zA-Z]+@[0-9a-zA-Z]+[\.]{1}[0-9a-zA-Z]+[\.]?[0-9a-zA-Z]+$');
        return reg.test(addr);
    }
};



//On Ready Events
jQuery(document).ready(function() {	
	
    //setup datepicker for OpenTable
    jQuery('.calendar').DatePicker({
        date: UTILS.getFormattedDate(),
        current: UTILS.getFormattedDate(),
        starts: 0,
        position: 'right',
        format: 'm/d/Y',
        onChange: function(formated, dates){
            jQuery('#date').val(formated);
            jQuery('.calendar').DatePickerHide();
        }
    });
	

    /**
	 * News & Offers Events - START
	 */
	
    //add click event to date picker to send request to OpenTable
    jQuery('#res_btn').click(function(){
        document.opentable.d.value = jQuery('#date').val() + ' ' +  jQuery('#time').val();
        document.opentable.p.value = jQuery('#party').val();
		
        //ROI tracking for GA
        ROILabs_trackFindTable(jQuery('#date').val(), jQuery('#time').val(),jQuery('#party').val())
		
        //Submit
        document.opentable.submit();
        return false;
    });
	
	
    // add whos more event
    jQuery('.showMore').click(function(){
        jQuery('#more').slideDown('fast');
    });
	
    //add news and offers events
    /*jQuery('#getNewOffers').click(function(){
		jQuery('#offers_outer').slideDown('normal', function(){
			jQuery('#offers_outer #name').get(0).focus();
		}); 
	});*/
	
    //close news and offers form
    jQuery('#offers_outer .close_btn').click(function(){
        jQuery('#offers_outer').fadeOut('normal', function(){
            SERVICES.hideAll('offers_outer');
            jQuery('#offers_outer')
            .find('#dyn_content input').val('').end()
            .find('#dyn_content').show();
        });
    });
	
    //subscribe to news and offers
    jQuery('#offers_outer #submit_btn').click(function(){
        SERVICES.subscribeToOffers();
    });
	
    /**
	 * SMS Events - START
	 */
    jQuery('#send_to_phone_btn').click(function(){
        jQuery('#sms_outer').center().show();
        _gaq.push(['_trackPageview', "/click/send_to_phone"]);
        return false;
    });
	
    //close SMS form
    jQuery('#sms_outer .close_btn').click(function(){
        jQuery('#sms_outer').fadeOut('normal', function(){
            SERVICES.hideAll('sms_outer');
            jQuery('#sms_outer')
            .find('#dyn_content input').val('').end()
            .find('#dyn_content').show();
        });
        return false;
    });
	
    //send SMS message event
    jQuery('#sms_submit').click(function(){
        SERVICES.sendSMS();
    });
	
    jQuery('form#sms_message').submit(function(){
        SERVICES.sendSMS();
        return true;
    });


    jQuery('#private').click(function(){
        _gaq.push(['_trackPageview', "/email/private_dining"]);
        return true;
    });

    jQuery('.twitter').click(function(){
        _gaq.push(['_trackPageview', "/social/twitter"]);
        return true;
    });

    jQuery('.facebook').click(function(){
        _gaq.push(['_trackPageview', "/social/facebook"]);
        return true;
    });

/**
	 * EVENTS - END
	 */
});

