/*
 * Project: Grande Portage
 * Author: Rudy Affandi
 * Created: 02/02/2011
 * File name: functions.js
 */

// Initialize all other javascript functions

// jQuery initialization and CSS settings, waiting for DOM tree to initialize
$(document).ready(function(){
   // Main navigation, please use #nav for your main UL
   sfHover = function() {
      var sfEls = document.getElementById("nav").getElementsByTagName("LI");
      for (var i=0; i<sfEls.length; i++) {
         sfEls[i].onmouseover=function() {
            this.className+=" sfhover";
         }
         sfEls[i].onmouseout=function() {
            this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
         }
      }
   }
   if (window.attachEvent) window.attachEvent("onload", sfHover);

   // Activate "class='active'" on current URL
   var path = location.pathname.substring(1).toLowerCase();
   var filename = path.match(/.*\/([^/]+)\.([^?]+)/i)[1];

   // Fix active state on Home page
   if ( filename == 'home')
   {
     $('a#n1').addClass('active');
   };
   if ( path )
   {
     $('#nav li a[href$="' + path + '"]').addClass('active');
     $('.content_side ul li a[href$="' + path + '"]').addClass('active');
   };

   // For URL with parameters
   var path2 = location.search.substring(1).toLowerCase();
   if ( path2 )
   {
     $('#nav li a[href$="' + path2 + '"]').addClass('active');
     $('.content_side ul li a[href$="' + path2 + '"]').addClass('active');
   };

   // Style default RFI form (Experimental)
   // First we clean up the mess from original table formatting
   $('div.form_wrapper table').addClass('ui-helper-reset');
   $('div.form_wrapper table').removeAttr("background");
   $('div.form_wrapper table').removeAttr("border");
   // and then we apply modifier to form elements
   $('div.form_wrapper table input[type=text], div.form_wrapper table td').addClass("ui-corner-all");
   $('div.form_wrapper table input[type=submit], div.form_wrapper table input[type=reset]').button();
   $('div.form_wrapper table textarea').addClass("ui-corner-all");
   $('select, input:checkbox, input:radio, input:file').uniform();
});

// External link warning dialog
function extLink(link) {
   var answer = confirm('You are leaving Grande Portage Resources Website')
   if (answer){
      window.location = link;
   }
   else {
      return false;
   }
};

$(document).ready(function(){
  var currentPosition = 0;
  var slideWidth = 612;
  var slides = $('.slide');
  var numberOfSlides = slides.length;

  // Remove scrollbar in JS
  $('#slidesContainer').css('overflow', 'hidden');
  $('.slide img').addClass('ui-corner-all');

  // Wrap all .slides with #slideInner div
  slides
    .wrapAll('<div id="slideInner"></div>')
    // Float left to display horizontally, readjust .slides width
	.css({
      'float' : 'left',
      'width' : slideWidth
    });

  // Set #slideInner width equal to total width of all slides
  $('#slideInner').css('width', slideWidth * numberOfSlides);

  // Insert controls in the DOM
  $('#slideshow')
    .prepend('<span class="control" id="leftControl">Clicking moves left</span>')
    .append('<span class="control" id="rightControl">Clicking moves right</span>');

  // Hide left arrow control on first load
  manageControls(currentPosition);

  // Create event listeners for .controls clicks
  $('.control')
    .bind('click', function(){
    // Determine new position
	currentPosition = ($(this).attr('id')=='rightControl') ? currentPosition+1 : currentPosition-1;
    
	// Hide / show controls
    manageControls(currentPosition);
    // Move slideInner using margin-left
    $('#slideInner').animate({
      'marginLeft' : slideWidth*(-currentPosition)
    });
  });
    
  $('.homeBanner').cycle({
    fx: 'fade',
    speed: 2000
  });

  // manageControls: Hides and Shows controls depending on currentPosition
  function manageControls(position){
    // Hide left arrow if position is first slide
	if(position==0){ $('#leftControl').hide() } else{ $('#leftControl').show() }
	// Hide right arrow if position is last slide
    if(position==numberOfSlides-1){ $('#rightControl').hide() } else{ $('#rightControl').show() }
  }
  
});
