var isIE6 = false;
if($.browser.msie && $.browser.version=="6.0") 
  isIE6 = true;

function toggleAdvancedSearch(){
  $("#search_advanced").toggle("slow");
  return false;
}
	

function pageinate(price_range_id, type_id, location_id, pagenum ){	


  document.getElementById('price_range_id').value = price_range_id;
  document.getElementById('type_id').value = type_id;
  document.getElementById('location_id').value = location_id;
  document.getElementById('pagenum').value = pagenum;
	

  if(document.getElementById('datepicker')){
    datepicker = document.getElementById('datepicker').value;
    document.getElementById('fromdate').value = datepicker;
  }
	
  document.getElementById('searchform').submit();
}

function mycarousel_initCallback(carousel) {
  $('.jcarousel-control a').bind('click', function() {
    carousel.scroll($.jcarousel.intval($(this).text()));
    return false;
  });
};

function mycarousel_itemFirstInCallback(carousel, item, idx, state) {
  $('.jcarousel-control a').each(function(i){
    //alert($(this).text() + "==" + idx);
    if ( $(this).text() == idx ) {
      $(this).addClass("current");
    } else {
      $(this).removeClass("current");
    }
  });
};
	
function dropShadows()
{
  $(".shadow").removeShadow();
  $(".shadow").dropShadow({
    left: 1,
    top: 1,
    opacity: 0.3,
    blur: 2,
    color: "black"
  });
}

$(document).ready(function() {
  
  // set class styles based on text value
  $('.flag-icons li a').each(function(){
    $(this).attr('class', $(this).text());
  });

  // home page video tabs
  $('#video_tabs a').live('click', function(){
    var thisLink = $(this).attr('href');
    var embed_html = '<iframe class="youtube-player" type="text/html" width="648" height="385" src="' + thisLink + '" frameborder="0">';
    if ( $(this).className != 'selected' ) {
      $('#video_home').html(embed_html);
      $('#video_tabs li a').removeClass();
      $(this).addClass('selected');
    }
    return false;
  });

  // fix PNGs in IE.stupid
  //$(document).pngFix();

  if ($('#mycarousel').length > 0) {
    $('#mycarousel').jcarousel({
      scroll: 4,
      initCallback: mycarousel_initCallback,
      itemFirstInCallback: mycarousel_itemFirstInCallback
    });
  }
  // homepage location list layout
  $('div.suburbs ul').addClass("twocol");

  // set the datepicker
  if(!isIE6) {
    $("#fromdate").datepicker({
      dateFormat: 'dd-mm-yy',
      minDate: 0,
      setDate: $("#fromdate").val()
      });
    $("#booking_move_in").datepicker({
      dateFormat: 'dd-mm-yy',
      minDate: 0,
      setDate: $("#booking_move_in").val()
      });
  } else {
    if ( $("#fromdate").val() == "" )
      $("#fromdate").val("dd-mm-yy");
    if ( $("#booking_move_in").val() == "" )
      $("#booking_move_in").val("dd-mm-yy");
  }
  // property search date picker
  //  $.getScript('http://furnishedproperty.amplify.com.au/lib/js/jquery/ui.datepicker.min.js', function(){
  //    var today = new Date();
  //    var day = today.getDate()+1 < 10 ? "0"+(today.getDate()+1) : today.getDate()+1;
  //    var month = today.getMonth()+1 < 10 ? "0"+(today.getMonth()+1) : today.getMonth()+1;
  //    var year = today.getYear() + 1900;
  //    var defaultDate = day+"/"+month+"/"+year;
  //    $('#movein').val(defaultDate).datepicker({
  //      minDate: '+1d',
  //      hideIfNoPrevNext: true,
  //      dateFormat: 'dd/mm/yy'
  //    });
  //  });
	
  // Load the superfish menu with a little bug fix for IE6
  //$('#mainnav > ul').superfish().bgiframe();
  $("#mainnav > ul").superfish({
    animation : {
      opacity:"show",
      height:"show"
    }
  })
  .find(">li[ul]")
  .mouseover(function(){
    $("ul", this).bgIframe({
      opacity:false
    });
  })
  .find("a")
  .focus(function(){
    $("ul",
      $("#mainnav > ul > li[ul]")).bgIframe({
      opacity:false
    });
  });
	
  //adds a div under subnav menu for style
  if ( $("#subnav").length > 0 ) {
    $("#subnav").append("<div class=\"subnav-bottom\"></div>");
  }
		
  // sort results
  $("#sort").change(function(){
    refineResults();
  });
  $("#fromdate").change(function(){
    refineResults();
  });
  $("#cleardate").click(function(){
    $("#fromdate").val("");
    refineResults();
    return false;
  });

  // setup modal box
  if ( $('#durationModal').length > 0 ) {
    $('#durationModal').jqm({
      ajax: '/templates/_durationModal.php'
    });
  }

  // stay length <30 days on enquiry form launches modal
  if ( $('#booking_stay_length').length > 0 ) {
    $('#booking_stay_length').change(function(){
      if ( $(this).val() == "Less than 30 days" ) {
        $('#durationModal').jqmShow();
      }
    });
  }

  // selecting short stay on search form launches modal
  if ($('#durationshort').length > 0) {
    $("#durationshort").change(function(){
      if ($("#durationshort:checked")){
        $('#durationModal').jqmShow();
      }
    });
  }

});

