var $jj = jQuery.noConflict();

// Here ya go, IE...
if( !Array.indexOf )
{
  Array.prototype.indexOf = function( obj )
  {
    for( var i=0; i < this.length; i++ )
    {
      if( this[i]==obj )
      {
        return i;
      }
    }
    return -1;
  }
}



// Crunchy Functionalityness

/* REGION SELECTOR 
**********************************/
function launchOverlay()
{
  // Note: langoverlay will not exist unless the site is on the US domain (www.oakley.com).
  if( $j( '#langoverlay' ))
  {
    $j.ajaxSetup({ 
      'beforeSend': function(xhr) { xhr.setRequestHeader("Accept", "text/html"); } 
    });

    $j( '#langoverlay' ).overlay({
      mask: { color: '#000', opacity: 0.9 },
      onBeforeLoad: function(){ this.getOverlay().load( '/region-select' ); },
      closeOnClick: true,
      load: true
    });
  }
}

function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}

function checkOverlay()
{
  jQuery.get( '/info.js', function( data )
  {
    data = eval( data );

    if( data.HTTP_X_AKAMAI_EDGESCAPE )
    {
      edgescape = data.HTTP_X_AKAMAI_EDGESCAPE;
  
      edgescape = edgescape.split( "," );
  
      country_code = '';
  
      // Loop the edgescape array looking for the country_code
      for( var i in edgescape )
      {
        esvar = edgescape[i].split( "=" );
  
        if( esvar[0] == 'country_code' )
        {
          country_code = esvar[1];
          break;
        }
      }


      // If they're not from a country we have a site for, and they dont have a cookie set, give them the overlay
      country_cookie = readCookie( 'akamai_redirect' );
      arrCountries = [ 'at', 'au', 'ca', 'ch', 'cn', 'be', 'dk', 'de', 'es', 'fr', 'ie', 'in', 'it', 'lu', 'nl', 'no', 'pl', 'pt', 'se', 'uk' ];

      if( location.href.indexOf( '/www.' ) != -1 && country_code.toLowerCase() != 'us' && country_cookie != 'http://www.oakley.com' )
      {
        launchOverlay();
      }
    }
  });
}

function closeOverlay()
{
  if( $j( '#langoverlay' ))
  {
    $j( '#langoverlay' ).overlay().close();
  }
}

function randomPrimaryPromo()
{
  var arrPromos = $j( '.promo_primary' );
  var intRandPromo = Math.floor( Math.random()*arrPromos.length);
  var objRandPromo = $j( arrPromos[ intRandPromo ]);

  arrPromos.hide();
  objRandPromo.show();
  $j( '#inner_wrapper' ).addClass( objRandPromo.attr( 'id' ));
  
  function switchIcons()
	{
  	  	if($j('#inner_wrapper').hasClass('holiday-ocp-oil-rig-red-icon')) {
		$j('#inner_wrapper').switchClass('holiday-ocp-oil-rig-red-icon','holiday-ocp-oil-rig-blue-icon',3000,'easeOutBounce');
		$j('#inner_wrapper').switchClass('holiday-ocp-oil-rig-blue-icon','holiday-ocp-oil-rig-chrome-icon',3000,'easeOutBounce');
		$j('#inner_wrapper').switchClass('holiday-ocp-oil-rig-chrome-icon','holiday-ocp-oil-rig-orange-icon',3000,'easeOutBounce');
		$j('#inner_wrapper').switchClass('holiday-ocp-oil-rig-orange-icon','holiday-ocp-oil-rig-black-icon',3000,'easeOutBounce');
		$j('#inner_wrapper').switchClass('holiday-ocp-oil-rig-black-icon','holiday-ocp-oil-rig-red-icon',3000,'easeOutBounce');
		}
	}
  //switchIcons();
  //setInterval(switchIcons, 3000);		
}

// Do stuffs on page load
$j(function()
{

  arrDeepLink = location.href.split( '#' );
  strDeepLink = '';
  bUseDeepLink = false;

  if( arrDeepLink.length == 2 )
  {
    strDeepLink = arrDeepLink[1];

    if( $j( '#' + strDeepLink + '.promo_primary' ).length > 0 )
    {
      bUseDeepLink = true;
    }
  }


  objTabs = $j( '#promotabs_nav a' );

  if( objTabs.length > 1 )
  {
    $j( '#promotabs_nav' ).tabs( '#promotabs_content > div',
    {
      effect: 'fade',
      fadeOutSpeed: 40,
      rotate: true
    }).slideshow({
      interval: 6000,
      autoplay: true      
    });
  }
  else
  {
    $j( '#promotabs_nav' ).hide();
  }

  if( bUseDeepLink )
  {
    $j( '.promo_primary' ).hide();
    $j( '#' + strDeepLink + '.promo_primary' ).show();
    $j( '#inner_wrapper' ).addClass( strDeepLink );
  }
  else
  {
    randomPrimaryPromo();
  }

  checkOverlay();
});

// Changes the background position on secondary promos
$j(function()
{
    $j('li.promo_secondary_4').removeClass('change_background');
    $j("li.change_background").hover(
        function() {
            $j(this).contents().find("div").css("background-position","0 -70px");
        },
        function() {
            $j(this).contents().find("div").css("background-position","0 0");
        }
    );    
});

