/*
 * for inline thickbox implementation
 */
function change_tb_href()
{
  var qString = $("a.thickbox").attr('rel');
  $("a.thickbox").attr('href', '#TB_inline?' + qString);
}

/*
 * auto submit form that has class of "auto" when any select is changed
 */
function form_auto_submit()
{
  $('form.auto select').change(function(){
    // added this to null second select if first is changed
    if (this.id == 'f1') {
      $('select#f2').attr('selectedIndex',0);
    }
    var $form = $(this).parents('form');
    $form.submit();
  });
}


function formMagic()
{
  // remove "required" class on labels
  $('form label.required').removeClass('required');
  // addptional class to input and textarea with value of "optional"
  $("form label.optional ~ input[type=text][value='optional']").addClass('optional');
  $("form label.optional ~ textarea[value='optional']").addClass('optional');
  // look for optional labels that have an empty input field or textarea as sibling
  // and add optional as value to field. remove value of optional on focus
  $("form label.optional ~ input[type=text][value='']").val('optional').addClass('optional').click(function(){
    if (this.value == 'optional') {
      $(this).val('').removeClass('optional');
    }
  }).blur(function(){
    if ($.trim(this.value) == '') {
      $(this).val('optional').addClass('optional');
    }
  });
  // ... and for textarea
  $("form label.optional ~ textarea[value='']").val('optional').addClass('optional').click(function(){
    if (this.value == 'optional') {
      $(this).val('').removeClass('optional');
    }
  }).blur(function(){
    if ($.trim(this.value) == '') {
      $(this).val('optional').addClass('optional');
    }
  });

  // remove field value of optional on submit
  $("form").submit(function(){
    $("form label.optional ~ input[type=text][value='optional']").val('').removeClass('optional');
    $("form label.optional ~ textarea[value='optional']").val('').removeClass('optional');
    return true;
  });

}

function doAction(id) {
	$('#' + id + '_hotspot').toggleClass('js_active');
	$('#' + id + '_thumb').toggleClass('js_active');
	$('#' + id + '_text').toggleClass('txt_active');
}

function doPromo()
{
	var active;
	$("[class~=promo-hover]").hover(
		function() {
			active = $(this).attr('id').split('_');
			doAction(active[0]);
		},
		function() {
			active = $(this).attr('id').split('_');
			doAction(active[0]);
			
		}
		);
}

String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g, ''); };

function saveCookie(city) {

  exp = new Date();
  date = exp.getTime() + 15724800000;
  exp.setTime(date);
  document.cookie = 'locality=' + city + '; expires=' + exp + '; path=/';
}

function retrieveCookie() {
  data = document.cookie;
  var raw;
  pairs = data.split(';');
  for(i = 0; i < pairs.length; i++) {
    raw = pairs[i].split('=');

    if(raw[0].trim() == 'locality') {
      return raw[1];
    }
  }
  
  return false;
}

function townTabs() {
  $('ul.tab-nav').tabs('div.panes > div');
  var api = $('ul.tab-nav').tabs();
  var locality = retrieveCookie();
  if(locality) {
    api.click(locality);
  }

  api.onClick(function(){
    var tabs = api.getTabs();
    var tab = api.getCurrentTab().attr('href');
    tab = tab.substr(1,tab.length);
    if(tab == 'cairns') {
      saveCookie('townsville');
    } else {
      saveCookie('cairns');
    }
  });
}


/* onDocumentReady Call */
$(function(){
  if ($('a.thickbox').hasClass('inline')) {
      change_tb_href();
  }

//  $('.date').datepicker();

//  if (typeof(hoverIntent) != 'undefined') {
//    drop_nav();
//  }

  form_auto_submit();
  doPromo();
//  drop_nav();
  formMagic();
//  $(".fancybox").fancybox({
//    'frameWidth':680,
//    'frameHeight':510,
//    'hideOnContentClick': false
//  });
});


