/*----------------------------------------------------------------------
[Global JavaScript ] 
Desc: RED site wide JavaScript		
-----------------------------------------------------------------------*/

(function($) {
    $(document).ready(function() {
        try{
          geoIPWelcome();
         $('input[type=checkbox].switch').makeSwitch();
       }catch(Error ){}
       customScrollBar();

       //Subscribe to newsletter
       $('#newsletterSignup').live('submit.RED', function(e) {
           NewsletterSignupFormSubmission('newslettersubscription', this);
           return false;
       });
   });

   function NewsletterSignupFormSubmission(operationname, form) {
       var dataString = "";
       var formdata = "&";
       formdata = formdata + $(form).serialize();

       dataString = dataString + '&ajaxoperationame=' + operationname + formdata;

       $('.ajaxloading').show();
       $('input#newslettersignupsubmit').hide();
       $('.error').hide();

       $.ajax({
           type: "post",
           url: "/resources/ajax/AjaxOperations.aspx",
           data: dataString,
           dataType: "json",
           success: function(data) {
               //was the operation success full, data.IsValid equivalent of ConductIT.InvalidFields.IsValid
               if (data.IsValid) {
                   $('#newsletterSignup').hide();
                   $('.ajaxsuccess').show();

                   //track google event of user subscribing
                   var country = $('#your_country').val(); //$('#your_country option:selected').text();
                   var language = $('#your_preferred_language').val(); //$('#your_preferred_language option:selected').text();
                   pageTracker._trackEvent('NewsletterRegistration', 'Submitted', country + ' - ' + language);
               } else {
                   //loop over every invalid field and show the predefined error message
                   if (data.InvalidFieldsArray != null && data.InvalidFieldsArray.length > 0) {
                       for (i = 0; i <= data.InvalidFieldsArray.length; i++) {
                           if (data.InvalidFieldsArray[i] != null) {
                               var selector = '#msg_' + data.InvalidFieldsArray[i];
                               $(selector).show();
                           }
                       }
                   }
                   $('input#newslettersignupsubmit').show();
               }
               $('.ajaxloading').hide();
           }
       });
   }

    /* Global > Close welcome message and write cookie --------------------*/
    function geoIPWelcome() {
        if ($("#CloseWelcomeOverlay").length > 0) {
            $('#CloseWelcomeOverlay').click(function() {
                //Hide overlay and message box
                $('#welcomeMessageOverlay').fadeOut(200);
                $('#welcomeMessageOverlayBoxWrapper').hide().empty();

                //Create welcome cookie options
                var cookie_name = 'WelcomeMessage';
                var hostname = window.location.hostname;
                var expiryDate = new Date(10000, 0, 1, 0, 0, 0); //1 Jan 10000
                var options = { path: '/', expires: expiryDate };

                //Write cookie
                $.cookie(cookie_name, hostname, options);

                //Don't follow link
                return false;
            });
        }
    };

    /* AF: sort this out */
    //$(document).mouseenter(function() {
        //if (!videoPlayer.isPlaying()) videoPlayer.play();
    //}).mouseleave(function() {
        //videoPlayer.pause();
    //});
    
})(jQuery);

