/*
 * Project: UEX Corporation
 * Author: Rudy Affandi
 * Created: 08/11/2011
 * File name: functions.js
 */

// Basic settings
var companyName = 'UEX Corporation';

// Main navigation drop down, 3rd level settings (please enter 'yes' or 'no')
var allow_main_3rd_level = 'yes';

// Side navigation drop down, 3rd level settings (please enter 'yes' or 'no')
var allow_side_3rd_level = 'yes';

//Cufon nav
var cufon_nav = 'no';

// Load required libraries asynchronously
head.js(
   {jquery: 'http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js'},
   {jqueryui: 'http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/jquery-ui.min.js'},
   {j_csspreload: 'http://repository.lesaff.com/scripts/jquery_plugins/preloadCssImages.jQuery_v5.js'},
   {j_uniform: '/cc/lib/jquery/plugins/uniform/jquery.uniform.min.js'},
   {j_hoverintent: 'http://repository.lesaff.com/scripts/jquery_plugins/jquery.hoverIntent.minified.js'},
   {j_url: 'http://repository.lesaff.com/scripts/jquery_plugins/jquery.url.js'},
   {j_cycle: 'http://repository.lesaff.com/scripts/jquery_plugins/jquery.cycle.all.min.js'},
   {j_validate: 'http://repository.lesaff.com/scripts/jquery_plugins/jquery.validate.min.js'},
   {j_maphilight: 'http://repository.lesaff.com/scripts/jquery_plugins/jquery.maphilight.min.js'},
   {j_tinysort: 'http://repository.lesaff.com/scripts/jquery_plugins/jquery.tinysort.min.js'},
   {cufon_font1: '/cc/lib/cufon/fonts/Trade_Gothic_LT_Std_700.font.js'},
   {cufon_font2: '/cc/lib/cufon/fonts/TradeGothic_400.font.js'},
   {shadowbox: '/cc/lib/shadowbox/v3/shadowbox.js'},
   {masterfunctions: '/cc/lib/masterfunctions/masterfunctions.js'}
);

// jQuery initialization and CSS settings, waiting for DOM tree to initialize
head.ready(function(){
   $(document).ready(function(){
      // Preload images from CSS
      if (!$('html.mozilla'))
      {
         $.preloadCssImages();
      }

      // Header slide show
      $('.header_img').cycle({
         fx: 'fade',
         random:  1,
         timeout: 9000
      });

      // Set language information and retrieve associated content
      $('html').attr('lang', 'en');

      // Front map
      $('#home-page .map, #projects-page .prj').maphilight({
         fill: true,
         fillColor: 'ffffff',
         fillOpacity: 0.2,
         stroke: true,
         strokeColor: '0c5753',
         fade: true
      });

      // Set h tag margin
      $('.content_body h2:first').css('margin-top', '0');

      // Add highlight to spot price table
      $('.front_spotprice tbody tr:nth-child(even)').addClass('highlight');

      // Remove unwanted br
      $('.front_spotprice tbody td.r').find('br').remove();

      // Set up 2 col AGM page
      $('.ac_format.two_col:nth-child(odd)').css('clear', 'both');

      // Financials, check if PDF exist
      $('.fs_format div.link a').each(function() {
         if   ( $(this).attr('href').length <= 0 )
         {
            $(this).hide();
         }
      });

      // Events Calendar
      var currentTime = new Date();
      var month = currentTime.getMonth() + 1
      var day = currentTime.getDate();
      var year = currentTime.getFullYear();
      if (day < 10) { day = '0' + day };
      if (month < 10) { month = '0' + month };
      var currentDate = ('e' + year + '' + month + '' + day);

      $('#events-page .events>div').each(function(){
         var eventDate = $(this).attr('id');
         if (eventDate >= currentDate)
         {
            $(this).addClass('upcoming');
         }
         else
         {
            $(this).addClass('past');
         }
         $('.events>div.upcoming').appendTo('#upcoming_events').show();
         $('#upcoming_events > div').tsort("",{order:"asc",attr:"id"});
         $('.events>div.past').appendTo('#past_events').show();
         $('#past_events > div').tsort("",{order:"desc",attr:"id"});
         $('.events>div.upcoming, .events>div.past').detach();
         // Deal with images
         if ($('#event_photo').length > 0)
         {
            //$('#upcoming_events > div:first').css({'display': 'inline-block', 'clear': 'both'});
            //$('#upcoming_events > div').css({'display': 'inline-block','width': '270px'});
            $('#event_photo').detach().insertAfter('#upcoming_events > h2');
         }
      });

      // Projects section
      var prj_title = $('.prj_format h2:first').text();
      $('.prj_format h2:first').detach();
      if (prj_title){
         $('.content_header h2').append(': ' + prj_title);
      }

      // Photo gallery settings (Grid format)
      $('ul.image_grid_format li a[href*="_sm"]').each(function(){
         var href = $(this).attr('href').replace('_sm', '');
         $(this).attr('href', href);
      });

      // Stylize form
      $('form#webform input[type="text"], form#webform h2.invert, form#webform textarea').addClass('ui-corner-all');

      // Validate form
      $('#webform').validate();
      $('#webform input:text:first').focus();

      // Initialize Cufon
      Cufon.set('fontFamily', 'Trade Gothic LT Std');
         Cufon.replace('.content_side h2', { hover: true });
      Cufon.set('fontFamily', 'TradeGothic');
         Cufon.replace('.front_content h2', { hover: true });
      Cufon.now();

      // Initiate shadowbox
      Shadowbox.init();

   });// End of $.ready
});// End of Head.ready

