var expanderTimer,headlineTimer,loginBoxHtml;


	var expander = {};

	expander.init = function () {
			$('#insSelector, #tagSelector, #selectors, #tagExpander, #insExpander, #expanders')
				.unbind('mouseover')
				.unbind('mouseout');

      $('#insSelector').bind('mouseover',function(){
            clearTimeout(expanderTimer);
            expanderTimer = setTimeout('startExpander("insExpander");',50);
      });
      $('#tagSelector').bind('mouseover',function() {
            clearTimeout(expanderTimer);
            expanderTimer = setTimeout('startExpander("tagExpander");',50);
      });
      $('#selectors').bind('mouseout',function() {
        clearTimeout(expanderTimer);
        $('#expanders *').stop(true,false);
        expanderTimer = setTimeout('toggleExpander("expanders");',100);
      });

      $('#tagExpander').bind('mouseover',function() {
        $('#btnTagSelector').css('background-position','bottom left');
        clearTimeout(expanderTimer);
      });
      $('#insExpander').bind('mouseover',function(){
        $('#btnInsiderSelector').css('background-position','bottom left');
        clearTimeout(expanderTimer);
      });

      $('#expanders').bind('mouseout',function() {
        clearTimeout(expanderTimer);
        expanderTimer = setTimeout('toggleExpander("expanders");',250);
      });
	};


  $(document).ready(function(){
		expander.init();
  });

  function startExpander(item)
  {
    if (item=="insExpander")
    {
        $('#btnTagSelector').css('background-position','top left');
        $('#btnInsSelector').css('background-position','bottom left');
        $('#dropDownBar img').fadeIn('fast',function() {
          clearTimeout(expanderTimer);
          expanderTimer = setTimeout('toggleExpander("insExpander");',300);

        });

    }
    else
    {

        $('#btnInsSelector').css('background-position','top left');
        $('#btnTagSelector').css('background-position','bottom left');
        $('#dropDownBar img').fadeIn('fast',function() {
            clearTimeout(expanderTimer);
            expanderTimer = setTimeout('toggleExpander("tagExpander");',300);
        });

    }
  }

  function toggleExpander(item)
  {
    $('#expanders').show();
    if($('#expanders *:animated').length == 0)
    {
      $('#expanders *').stop(true,true);
      $('#tagExpander,#insExpander').css('height','316px');
      if (item=="insExpander")
      {
        $('#btnInsSelector').css('background-position','bottom left');
        $('#btnTagSelector').css('background-position','top left');
        // Hide tag expander if visible
        // Show Insider Expander if hidden
        // Else hide Insider Expander and hide bar
        if($('#tagExpander:hidden').length==0)
        {
          $('#tagExpander').hide();
          $('#insExpander').show();
        }
        else if($('#insExpander:hidden').length>0)
        {
          $('#insExpander').slideDown(function() {

          });
        }
      }
      else if (item=="tagExpander")
      {
        $('#btnTagSelector').css('background-position','bottom left');
        $('#btnInsSelector').css('background-position','top left');
        // Reset tag expander scrolls
        $('#tagExpander ul').scrollTo('li:eq(0)',150);

        // Hide insider expander if visible
        // Show Tag Expander if hidden
        // Else hide Tag Expander and hide bar
        if($('#insExpander:hidden').length==0)
        {
          $('#insExpander').hide();
          $('#tagExpander').show();
        }
        else if($('#tagExpander:hidden').length>0)
        {
          $('#tagExpander').slideDown(function() {

          });
        }
      }

    }
    if(item=='expanders')
    {
        $('#btnTagSelector').css('background-position','top left');
        $('#btnInsSelector').css('background-position','top left');

        $('#expanders').slideUp('fast',function() {
          $('#dropDownBar img').hide();
          $('#tagExpander,#insExpander').hide();
        });
    }
  }
  
$(document).ready(function(){

    $('#loginLink a').bind('click',function(){
       showLogin($(this).attr('title'));
    });

    $('#bookdirectForm .submit').bind('mousedown',function(){ this.src.replace('_search.png','_search_down.png'); });
    $('#bookdirectForm .submit').bind('mouseup',function(){ this.src.replace('_search_down.png','_search.png'); });

    $('#weatherIcons td:even:not(.noBg)').css('background-color','#a6d2f4');
    $('#weatherIcons td:even:not(.noBg)').css('border-left-width','0px');
    $('#weatherIcons td:even:not(.noBg)').css('border-right-width','0px');

    $('#headlinesList li').bind('mouseover',function(e) {headlineTimer=setTimeout("changePreviewHeadline('"+e.currentTarget.id+"')",100);});
    $('#headlinesList li').bind('mouseout',function() {clearTimeout(headlineTimer);});

    

    $('#hotel_search_checkin').css('background','url(/images/shell_v2/newHomepage/bookdirect_formbg.gif) no-repeat -200px -20px');
    $('#hotel_search_checkout').css('background','url(/images/shell_v2/newHomepage/bookdirect_formbg.gif) no-repeat -200px -40px');
    $('#hotel_search_city').css('background','url(/images/shell_v2/newHomepage/bookdirect_formbg.gif) no-repeat -200px -60px');
    $('#hotel_search_type').css('background','url(/images/shell_v2/newHomepage/bookdirect_formbg.gif) no-repeat -200px -0px');


    $('#tagExpanderLinks .more').bind('click',function(){
      var list = $(this).parent('.expanderColumn').find('ul');
      if($(list).css('overflow-y')=='hidden')
      {
        $(list).css('overflow-y','scroll');
        $(list).scrollTo('li:eq(11)',150);
        $(this).text('Less »')
      }
      else
      {
        $(list).scrollTo('0px',500);
        $(list).css('overflow-y','hidden');
        $(this).text('More »')
      }
    });

    if($('#conditionalContent td:first div').length == 0) {
      $('#conditionalContent td:last div:first').css('width','722px');
    }
    else if($('#conditionalContent td:last div').length == 0) {
      $('#conditionalContent td:first div').css('width','722px');
    }
  });

  function changePreviewHeadline(itemId) {
    $('#headlines *').stop(true,true);
    clearTimeout(headlineTimer);
    var headline = $('#'+itemId);
    if(headline.hasClass('activeHeadline')) return null;
    $('#headlinesList li').removeClass('activeHeadline');
    var headlineType = headline.attr('className').replace(/ headline$/,'');
    headline.addClass('activeHeadline');
    $('#headlinesPreview *').fadeOut(50,function(){
    	if(headline.css('background-image').indexOf('_pp') > 0)
    	{
        	$('#previewIcon').css('background-image',$('#previewIcon').css('background-image').replace(/[a-z_]+\.png/i, headlineType + '_pp.png'));
        	$('#previewHeadline a').css('color','#ed037c');
        	$('#previewImage').css('border-color','#ed037c');
        }
        else
        {
        	$('#previewIcon').css('background-image',$('#previewIcon').css('background-image').replace(/[a-z_]+\.png/i, headlineType + '.png'));
        	$('#previewHeadline a').css('color','#00a4b4');
        	$('#previewImage').css('border-color','#00a4b4');
        }
        $('#previewHeadline a').text(headline.children('a').text());
        $('#previewHeadline a').attr('class',itemId);
        $('#previewImage img').attr('src',headline.children('.headlineImageUrl').text());
        $('#previewText').children('p').text(headline.children('p').text());
        $('#headlinesPreview a').attr('href',headline.children('a').attr('href'));
        $('#headlinesPreview *').fadeIn(200);
    });
  }
  
  function changePreviewHeadlineIE(itemId) {
    var headline = $('#'+itemId);
    if(headline.hasClass('activeHeadline')) return null;
    $('#headlines *').stop(true,true);
    clearTimeout(headlineTimer);
    $('#headlinesList li').removeClass('activeHeadline');
    var headlineType = headline.attr('className');
    headline.addClass('activeHeadline');
    $('#headlinesPreview *').hide();
    $('#previewIcon').css('background-image',$('#previewIcon').css('background-image').replace(/[a-z_]+\.png/i, headlineType + '.png'));
    $('#previewHeadline a').text(headline.children('a').text());
    $('#previewImage img').attr('src',headline.children('.headlineImageUrl').text());
    $('#previewText').children('p').text(headline.children('p').text());
    $('#headlinesPreview a').attr('href',headline.children('a').attr('href'));
    $('#headlinesPreview *').show();
  }


  function changeDeal(id)
  {
    var curr = $('.currentDeal').attr('id');
    if(id > 0 && $('#deal'+id).length>0)
      {
        var next = $('#deal'+id).attr('id');
      }
    else if(curr.replace('deal','')==$('.deal').length)
      {
        var next = $('.deal:first').attr('id');
      }
      else
      {
        var next = $('#'+curr).next().attr('id');
      }
     if(curr != next)
     {
       $('#'+next+',#'+curr).toggleClass('currentDeal');
       $('#'+curr).hide();
       $('#'+next).fadeOut(200);
       $('#'+next).fadeIn(500);
       $('#'+curr+'Link'+',#'+next+'Link').toggleClass('currentDealLink');
     }
  }

function showLogin(section)
{
  if(section.length == 0)
    section = 'users';
  $('#loginBox form').attr('action',$('#loginBox form').attr('action').replace(/section.[^\/]+/g,'')+'section.'+section);
  $('#loginBox .fbLogin').attr('rel',section);
  $.colorbox({inline:true,href:'#loginBox'});
}

// A gift for 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;
  }
}

