var interval_slideshow_maintenance;
var interval_slideshow_pulse;

jQuery.noConflict();

jQuery(document).ready( function() {

 // Detect CSS mask support and hack it into Modernizr
 if ( document.body.style[ '-webkit-mask-repeat' ] !== undefined ) {
  Modernizr.cssmasks = true;
  jQuery('html').addClass('cssmasks');
 } else {
  Modernizr.cssmasks = false;
  jQuery('html').addClass('no-cssmasks');
  // Rewrite peel URLs
  jQuery('#Offerings span.peel>img').each( function(i) {
   var jq_obj = jQuery(this);
   var body = jQuery('body');
   if ( body.hasClass('ie6') || body.hasClass('ie7') || body.hasClass('ie8') ) {
    jq_obj.attr('src', jq_obj.attr('src').replace('.png', '-no-masks-no-shadow.png') );
   } else {
    jq_obj.attr('src', jq_obj.attr('src').replace('.png', '-no-masks.png') );
   }
  });
 }

 //add links to peels
 jQuery('#SportsBetting span.peel').click(
  function(event){

   if(window.location = jQuery("#SportsBetting>a").attr("href") != 'undefined') {

    event.preventDefault();
    window.location = jQuery("#SportsBetting>a").attr("href");
   }
  }
  );
 jQuery('#EnterOurCasino span.peel').click(
  function(event){

   if(window.location = jQuery("#EnterOurCasino>a").attr("href") != 'undefined') {

    event.preventDefault();
    window.location = jQuery("#EnterOurCasino>a").attr("href");
   }
  }
  );
 jQuery('#PlayPokerOnline span.peel').click(
  function(event){

   if(window.location = jQuery("#PlayPokerOnline>a").attr("href") != 'undefined') {

    event.preventDefault();
    window.location = jQuery("#PlayPokerOnline>a").attr("href");
   }
  }
  );
 // Provide for striping
 jQuery('#OddsListing .view_of_odds table tbody tr:odd').addClass('even');

 jQuery("#Ticker").click(
  function(event){

   if(window.location = jQuery("#Ticker>#tickergame").attr("href") != 'undefined') {

    event.preventDefault();
    window.location = jQuery("#Ticker>#tickergame").attr("href");
   }
  }
  );
  
 // Work around IE6 deficiencies
 jQuery('body.ie6 #OddsListing .view_of_odds table tbody tr,#Ticker ul li.preview,body.ie6 #Offerings ul li a,body.ie6 div.sidebar_ad a').hover(
  function() {
   jQuery(this).addClass('hover');
  },
  function() {
   jQuery(this).removeClass('hover');
  }
  );
    
 jQuery('body.ie6 #Header nav ul li a,body.ie6 #Header a#Login,body.ie6 #Header a#MyAccount,body.ie6 #Header a#Logout').hover(
  function() {
   jQuery(this).children('span').css({
    visibility: 'visible'
   });
  },
  function() {
   jQuery(this).children('span').css({
    visibility: 'hidden'
   });
  }
  );
    
  
 // Fading on nav and Offerings
 if ( !( Modernizr.csstransitions ) ) {

  // Indirect transitions
  jQuery('#Offerings>ul>li>a,.sidebar_ad>a').hover(
   function() {
   },
   function() {
    var outgoing = jQuery(this).find('div.image>span.peel');
    outgoing.stop();
    outgoing.animate( {
     width: '116px',
     height: '88px'
    }, 250 );
   }
   );
    
  // Peels
  jQuery('#Offerings>ul>li>a>div.image>span.peel').hover(
   function() {
    var incoming = jQuery(this);
    incoming.stop();
    incoming.animate( {
     width: '296px',
     height: '225px',
     opacity: 1
    }, 250 );
   },
   function() {
    var outgoing = jQuery(this);
    outgoing.stop();
    outgoing.animate( {
     width: '116px',
     height: '88px',
     opacity: 1
    }, 250 );
   }
   );
 }
 jQuery("#Ticker>#tickergame").hide();
 // Start Ticker
 jQuery("section#Ticker > ul").liScroll();

  
 /*if ( jQuery('#Ticker').length > 0 ) {
    jQuery('#Ticker>ul>li:last-child').addClass('last');
    jQuery('#Ticker>ul').css('left', jQuery('#Ticker').width() );
    calculate_ticker_widths();
    jQuery(window).resize( function() { calculate_ticker_widths(); } );
    animate_ticker();
    jQuery('#Ticker>ul').hover(
      function() {
        jQuery(this).stop();
      },
      function() {
        animate_ticker();
      }
    );
  }
  */



 // Set pollers up
 interval_slideshow_maintenance = window.setInterval( slideshow_maintenance, 500 )
 interval_slideshow_pulse = window.setInterval( slideshow_pulse, 2500 );

});

function calculate_ticker_widths() {
 var page_width = jQuery('#Ticker').width();
 var total_li_width = 0;
 jQuery('#Ticker>ul>li').each( function(i) {
  total_li_width += jQuery(this)[0].offsetWidth + parseInt( jQuery(this).css('margin-right') ); // Margin is 60
 });
 var last_margin = Math.max( ( page_width + 70 - total_li_width ), 60 );
 jQuery('#Ticker>ul>li.last').css({
  marginRight: last_margin
 });
}

function animate_ticker() {
 var ticker = jQuery('#Ticker>ul');
 var first = jQuery('#Ticker>ul>li:first-child');
 var li_width = first[0].offsetWidth + parseInt( first.css('margin-right') );
 var distance = ticker[0].offsetLeft + li_width;
 ticker.animate({
  left: ( 0 - li_width )
 }, ( distance * 12 ), 'linear', function() {
  clean_up_ticker();
  animate_ticker();
 } );
}

function clean_up_ticker() {
 var first = jQuery('#Ticker>ul>li:first-child');
 var ul = jQuery('#Ticker>ul');
 first.detach();
 ul.css({
  left: 0
 });
 ul.append( first );
}

function slideshow_maintenance() {
 jQuery('.slideshow').each( function(i) {
  var jq_obj = jQuery(this);
  var next = jq_obj.find('.next');
  if ( ( next.length > 0 ) && ( parseInt( next.css('left') ) == 0 ) ) {
   var current = jq_obj.find('.current');
   current.removeClass('current');
   next.addClass('current');
   next.removeClass('next');
   if ( next.hasClass('final') ) {
    jq_obj.trigger('lastSlide');
   } else {
    var true_next = next.next();
    if ( true_next.length == 0 ) {
     true_next = jq_obj.children().first();
    }
    true_next.addClass('next');
   }
   if ( Modernizr.csstransitions ) {
    jq_obj.removeClass('sliding');
   } else {
    current.css({
     opacity: 0
    });
   }
  }
 });
}

function slideshow_pulse() {
 if ( Modernizr.csstransitions ) {
  jQuery('.slideshow').addClass('sliding');
 } else {
  jQuery('.slideshow').each( function(i) {
   jQuery(this).find('.next').animate( {
    left: 0
   }, 250 );
  });
 }
}


