var ie6 = (navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion) == 4 && navigator.appVersion.indexOf("MSIE 6.0") != -1);

$(document).ready(function()
{
    jQuery('.contact-details-container a').click(function(e) {
        jQuery('.contact-details').show();
        e.preventDefault();
    }).mouseout(function() {
        jQuery('.contact-details').delay(200).fadeOut();
    });
  $('.listing-details > .info > .left > ul > li > .tooltip-content').each(function()
  {
    if (jQuery.browser.msie && ie6)
    {
      var a = $(this).prev();
      var tooltip = $(this);
      $(this).slideUp(0);
      $(a).toggle(function()
      {
        tooltip.slideDown(1000);
      }, function()
      {
        tooltip.slideUp(1000);
      });
    }
    else
    {
      var a = $(this).prev();
      $(a).tooltip(
      {
        //position: 'top right',
        //offset: [ -145, -480 ],
        events: {
          def: 'click, mouseout'
        },
        effect: 'fade'
      });
    }
    $(a).click(function(){return false;});
  });
  
  if (jQuery.browser.msie && ie6)
  {
    //ie6
    $('.listing-details .listing-enquiry span.cancel').remove();
    $('.left a.enquiry').attr('href', '#listing-enquiry');
    $('.listing-details .listing-enquiry').before('<a name="listing-enquiry"></a>');
    
    var location = window.location+'';
    location = location.split('#');
    
    $('.listing-details .listing-enquiry form').attr('action', location[0]);
  }
  else
  {
    var enquiry = $('.listing-details .listing-enquiry');
    $('.left a.enquiry').click(function()
    {
      width = enquiry.width();
      enquiry.css(
      {
        position: 'absolute',
        top: 0,
        left: width*-1,
        height: $('.listing-details').height()
      });
      
      enquiry.show();
      
      enquiry.animate(
      {
        left: 0
      }, width * 1.5);
      
      return false;
    });
    
    $('.listing-details .listing-enquiry span.cancel').click(function()
    {
      width = enquiry.width();
      enquiry.animate(
      {
        left: width*-1
      }, width * 1.5);
      
      return false;
    });
    
    enquiry.hide();
  }
  
  handleStarRating();
});


function enquiryCallback(form, responseText, statusText)
{
  var responseTextSplit = responseText.split('|||');
  var error = (responseTextSplit[0] == 'ERROR');
  
  $('#ajax-message-dialog').remove();
  $('body').append('<div id="ajax-message-dialog" title="Enquiry"><p>'+responseTextSplit[1]+'</p></div>');
  var messageDialog = $('#ajax-message-dialog');
  messageDialog.dialog(
  {
    buttons: {'OK': function() {$(this).dialog('close');if (!error) {$('.listing-details .listing-enquiry span.cancel').trigger('click');}}},
    draggable: false,
    modal: true,
    resizable: false
  });
  
  messageDialog.bind('dialogclose', function(event, ui) {
    if (jQuery.browser.msie && ie6)
    {
      $('select').css('visibility', 'visible');
    }
  });
}


var starsOffSrc = '';
var starsOnSrc = '';

function handleStarRating()
{
  /*
  
  ok, so we have a few things going on
  1. When you hover over a star, it and any previous stars should highlight. This takes precedence over any current selection
  2. When you click on a star, it and any previous stars should highlight as their "default state" (when not hovered).
  
  */
  
  //Some basic variables we'll be using throughout
  var defaultSrc = $('.submit-review form ul li .rating-stars .star img').attr('src');
  starsOffSrc = defaultSrc;
  starsOnSrc = starsOffSrc.split('Off').join('On');
  
  //Sets all the stars to have a default state of Off
  $('.submit-review form ul li .rating-stars .star img').attr('defaultState', 'off');
  setStarsDefault();
  setStarsLoad();
  $('.submit-review form ul li .rating-stars .star img').each(function()
  {
    $(this).bind('mouseenter', function()
    {
      $(this).parent().parent().find('img').each(function()
      {
        $(this).attr('src', starsOffSrc);
      });
      
      //highlight this star, and any previous stars
      var starCount = $(this).attr('class');
      starCount = parseInt(starCount.split('-')[1]);
      
      $(this).parent().parent().find('img').each(function()
      {
        var otherStarCount = $(this).attr('class');
        otherStarCount = parseInt(otherStarCount.split('-')[1]);
        
        if (otherStarCount <= starCount)
        {
          //highlight
          $(this).attr('src', starsOnSrc);
        }
      });
    });
    
    $(this).bind('mouseleave', function()
    {
      $(this).parent().parent().find('img').each(function()
      {
        setStarDefault(this);
      });
      
      var starCount = $(this).attr('class');
      starCount = parseInt(starCount.split('-')[1]);
      
      $(this).parent().parent().find('img').each(function()
      {
        var otherStarCount = $(this).attr('class');
        otherStarCount = parseInt(otherStarCount.split('-')[1]);
        
        if (otherStarCount <= starCount)
        {
          //highlight
          setStarDefault(this);
        }
      });
    });
    
    $(this).bind('click', function()
    {
      $(this).parent().parent().find('img').each(function()
      {
        $(this).attr('defaultState', 'off');
        setStarDefault(this);
      });
      
      var starCount = $(this).attr('class');
      starCount = parseInt(starCount.split('-')[1]);
      
      //change the hidden input value to be this star count
      $(this).parent().parent().find('input').val(starCount);
      
      $(this).parent().parent().find('img').each(function()
      {
        var otherStarCount = $(this).attr('class');
        otherStarCount = parseInt(otherStarCount.split('-')[1]);
        
        if (otherStarCount <= starCount)
        {
          //highlight
          $(this).attr('defaultState', 'on');
        }
      });
      
      $(this).trigger('mouseenter');
    });
  });
}

function setStarsDefault()
{
  $('.submit-review form ul li .rating-stars .star img').each(function()
  {
    //based on what their defaultState is, it will change their src (to on/off for highlight etc)
    setStarDefault(this);
  });
}

function setStarDefault(element)
{
  var defaultState = $(element).attr('defaultState');
    
  if (defaultState == 'off')
  {
    $(element).attr('src', starsOffSrc);
  }
  else if (defaultState == 'on')
  {
    $(element).attr('src', starsOnSrc);
  }
}
function setStarsLoad()
{
    //find the hidden value for each star rating
    jQuery('input.hiddenstar').each(function(){
        var starCount = this.value;
        //go through all of the images in that row
        jQuery(this).parent('.rating-stars').find('img').each(function(){
            var otherStarCount = $(this).attr('class');
            otherStarCount = parseInt(otherStarCount.split('-')[1]);
            if (otherStarCount <= starCount)
            {
              //highlight
              jQuery(this).attr('defaultState', 'on');
            }
        });
    });
    //call function to set images
    setStarsDefault();
}

