/*
 * (c) Copyright 2001-2009 Hellriser Customs. All Rights Reserved. 
 */ 
 
var ckSelectedBikes = 'HRC_BIKES';  // The cookie name
var selectedBikes = new Array();
var maxBikes = 4;

function gotoPage( link, bikeName, bikeMake, bikeCode ) {
  var bike = { name: bikeName, make: bikeMake, code: bikeCode, url: link };
  var found = false;
  // Search if the bike is already in the list
  for (var i=0; i < selectedBikes.length; i++) {
    if (selectedBikes[i].code == bikeCode) {
      found = true;
      break;
    }
  };
  // If not then add it and save the list in the cookie
  if (!found) {
    if (selectedBikes.length == maxBikes) {
      selectedBikes.shift();
    }
    selectedBikes.push(bike);    
    $.cookie(ckSelectedBikes, JSON.stringify(selectedBikes), {expires: 365, path: '/'});
  }
  window.location = link;
}

function getSelectedBikes () {
  var rawList = $.cookie(ckSelectedBikes);
  if (rawList == null) {
    return(new Array());
  }
  return(JSON.parse(rawList));
}

function showSelectedBikes () {
  for (var i=0; i < selectedBikes.length; i++) {
    $("#selectedBikes").append(
      '<div class="oneBike">' +
      '<a href="' + selectedBikes[i].url + '">' +
      '<img src="/images/ui/bikes/' + selectedBikes[i].make + '/' + selectedBikes[i].code + '.jpg">' +
      '</a>' +
      '<br>' +
      '<a href="/' + selectedBikes[i].url + '">' + selectedBikes[i].name + '</a></div>'
    );
  };
}

$(document).ready( function() { 
    $("#msgBox").hide();
    checkIE6();    
    // The menu
    $("ul.sf-menu").superfish({ speed: 'fast'});

    // =====================
    // = Garage invitation =
    // =====================
    $("#garageCarousel").scrollable({ 
        loop: true, 
        vertical: true, 
        interval: 5000, 
        size: 1,
        speed: 1000
    });

    // =================
    // = Burnout Alley =
    // =================
    var HRCBOACookie = 'HRCBOA';
    var boaScrollerID = 'boaScroller';
    var boaScrollInterval = '10s';
    // The BoA scrolls automatically during idle time. We create this effect
    // by calling the API for "scrollable" andby  switching direction 
    // when reaching the end of the list. When the user clicks on either one of the buttons 
    // we stop the scroller
    var scrollCallback = function() {
      var scroller = $("#burnoutAlley_product_window").scrollable();
      var pages = scroller.getPageAmount();
      if (scrollFwd) {
        if (scroller.getPageIndex() < pages-1) { 
          scroller.nextPage();
        }
        else {
          scrollFwd = false;
        }
      }
      else {
        if (scroller.getPageIndex() > 0) { 
          scroller.prevPage();
        }
        else {
          scrollFwd = true;
        }
      }
    }
    
    var scrollFwd = true;
    $("#burnoutAlley_product_window").scrollable({
      vertical: true,
      size: 3,
      clickable: false,
      hoverClass: 'boaHover',
      items: '#burnoutAlley_product_list',
      onSeek: function() {
        $.cookie(HRCBOACookie, this.getPageIndex()+1);        
      }
    })
    .everyTime(boaScrollInterval,boaScrollerID,function() {
      scrollCallback();
    });

    // Obtain the BOA page when the user clicks on a item
    $("a.boa").click(function() {
      var scroller = $("#burnoutAlley_product_window").scrollable();
      $.cookie(HRCBOACookie, scroller.getPageIndex());
    });
    
    // Reset the idle timer when the user clicks on the nav buttons
    $(".boaNavButton").click(function() {      
      $("#burnoutAlley_product_window").stopTime(boaScrollerID);
    });
    
    // Reposition BOA to the last clicked page
    var scroller = $("#burnoutAlley_product_window").scrollable();
    var scrollPage = $.cookie(HRCBOACookie);
    if ( scrollPage != null) {
      scroller.setPage(scrollPage);
    }
    
    // ================
    // = Recent Picks =
    // ================
    // Retrieve the list of previously selected bikes, if any
    selectedBikes = getSelectedBikes()
    showSelectedBikes();
    if (selectedBikes.length == 0) {
      $("#selectedBikes").hide();
    }
    
    var url = '';
    // We can arrive to this page via a series of different referrals, here we try to separate the codes that we pass via AdWords
    var query = window.location.search.substring(1);
    if ( query != '' ) {
      var querypair = query.split("=");
      url = querypair[1];
    }
    else {
      url = document.referrer;
    }
    if ( url == '') {
      url = ' ';
    }
    // ===========
    // = Logging =
    // ===========
    url = Base64.encode(url);
    $.get('/log_visits/index/' + url + "/" + screen.width + "/" + screen.height );
    
    $(".bikeBrands li").click(function(event) {
      event.preventDefault();
    });

    $("a[rel]").overlay(function() {              
      // grab wrapper element inside content 
      var wrap = this.getContent().find("div.wrap");
      var trigger = this.getTrigger();
      if (trigger.hasClass('iframe')) {
        wrap.html('<iframe src="' + trigger.attr("href") + '" allowtransparency="true" frameborder=0></iframe>');        
      }
      else {
        wrap.load(trigger.attr("href"));
      }
    });   
    // Workaround for Safari nor setting the hidden atrtribute of the overlay panel background image
    $("img[src$='gray.png']").hide();
});