/***********************************************
* jQuery functions execute
***********************************************/

$(document).ready(function() {                
	/*cmx form */
	if(jQuery.browser.mozilla) {
		$('form.cmxform').hide().find('label:not(.nocmx)').each(function(){
                          	var $labelContent = $(this).html();
                                       var $labelWidth = $(this).css('width');
                                       $(this).empty();
                                       $(this).append('<span style="display: block; width: '+$labelWidth+';">');
                                       $(this).prepend('</span>');
                                       $(this).css('display', '-moz-inline-box');
                                       $(this).find('span').html($labelContent);
                                       $('form.cmxform').show();
                                });
                };             


	/*external link*/
	$('a[rel$="external"]').click(function(){this.target = "_blank";});
	
	/*thickbox opacity*/	
	$('.thickbox').click(function() {$('.TB_overlayBG').css('opacity', '0.90');});
	
	/*email protect*/
	$("span.safemail").each(function(){
		exp = $(this).text().search(/\((.*?)\)/) != -1 ? new RegExp(/(.*?) \((.*?)\)/) : new RegExp(/.*/);
		match = exp.exec($(this).text());
		addr = match[1] ? match[1].replace(/ at /,"@").replace(/ dot /g,".") : match[0].replace(/ at /,"@").replace(/ dot /g,".");
		link = match[2] ? match[2] : addr;
		subject = $(this).attr('title') ? "?subject="+$(this).attr('title').replace(/ /g,"%20") : "";
		$(this).after('<a href="mailto:'+addr+subject+'">'+ link + '</a>');
		$(this).remove();
	});
	
	
	/*banner image transition*/
	$('#home-banner').cycle({ 
    fx:    'fade', 
    speed:  1000 
 	});	


	/*on focus for input boxes*/
	$.fn.search = function() {
		return this.focus(function() {if( this.value == this.defaultValue ) {this.value = "";}})
		.blur(function() {if( !this.value.length ) {this.value = this.defaultValue;}});};
	$("input#Keyword").search();
	$("input#Username").search();
	$("input#Password").search();
	
	/*set hover class for anything*/
	$('a.send').hover(function() {$(this).addClass('hover');}, function() {$(this).removeClass('hover');});
	$('input.searchnow').css('cursor', 'pointer').hover(function() {$(this).addClass('searchhover');}, function() {$(this).removeClass('searchhover');});
	$('input.send').css('cursor', 'pointer').hover(function() {$(this).addClass('inputhover');}, function() {$(this).removeClass('inputhover');});
	$('form.cmxform input.send').css('cursor', 'pointer').hover(function() {$(this).addClass('inputhover');}, function() {$(this).removeClass('inputhover');});
	
	/*ie6 dropdown fix*/
	$('ul#nav li').hover(function() {$(this).addClass('over');}, function() {$(this).removeClass('over');});
	$('ul#nav li li').hover(function() {$(this).addClass('over2');}, function() {$(this).removeClass('over2');});
	$('ul#nav ul').hover(function() {$('.over a').addClass('onsection');}, function() {$('.over a').removeClass('onsection');});
	$('ul#nav li li').hover(function() {$('.over a').addClass('onsection');}, function() {$('.over a').removeClass('onsection');});
	$('ul#nav ul ul').hover(function() {$('.over2 a').addClass('onsection2');}, function() {$('.over2 a').removeClass('onsection2');});
	$('ul#nav li li li').hover(function() {$('.over2 a').addClass('onsection2');}, function() {$('.over2 a').removeClass('onsection2');});
	
	/*advanced hide and show 1*/
	$('ul.address li> ul').hide();
	$('ul.address ul#perth').show();
	$('ul.address li h3.add').css('cursor', 'pointer');
	$('ul.address li h3.add').click(function() {
	$(this).next('ul').slideToggle('fast')
	.parent().siblings('li').find('ul:visible').slideUp('fast');});
	
	/*advanced hide and show 2*/
	$('ul.faq li> ul').hide();
	$('ul.faq li h3').css('cursor', 'pointer');
	$('ul.faq li h3').click(function() {
	$(this).next('ul').slideToggle('fast')
	.parent().siblings('li').find('ul:visible').slideUp('fast');});
	
	/* QBOX SECTION */
	
	/*advanced hide and show 3*/
	$('.benefits li > ul').hide();
	$('.benefits li h4').css('cursor', 'pointer');
	$('.benefits li h4').click(function() {
	$(this).next('ul').slideToggle('fast')
	.parent().siblings('li').find('ul:visible').slideUp('fast');});
	
	/*advanced hide and show 4*/
	$('ul#qbox-business li> ul').hide();
	$('ul#qbox-business li h4').css('cursor', 'pointer');
	$('ul#qbox-business li h4').click(function() {
	$(this).next('ul').slideToggle('fast')
	.parent().siblings('li').find('ul:visible').slideUp('fast');});
	
	/*advanced hide and show 5*/
	$('ul#qbox-services li> ul').hide();
	$('ul#qbox-services li h4').css('cursor', 'pointer');
	$('ul#qbox-services li h4').click(function() {
	$(this).next('ul').slideToggle('fast')
	.parent().siblings('li').find('ul:visible').slideUp('fast');});
	
	/*advanced hide and show 6*/
	$('ul#qbox-workflow li> ul').hide();
	$('ul#qbox-workflow li h4').css('cursor', 'pointer');
	$('ul#qbox-workflow li h4').click(function() {
	$(this).next('ul').slideToggle('fast')
	.parent().siblings('li').find('ul:visible').slideUp('fast');});	
});

//This next is for the register interest page
$(document).ready(function() {                
  $("input.update-cost").click(validate_selection)
  
  function update_cost() {
    var iPriceTotal       = 0
    var iPriceEarlyTotal  = 0
    var iPriceTotalCount  = 0
    var iEarlyBirdtotal
    var iCurrVal

    $("input.update-cost:checked").each(function(){
      iCurrVal          = ($(this).parent("td").find("input[name=Price]").val() * 1)
      iPriceTotal       = iPriceTotal + iCurrVal
      
      iCurrVal          = ($(this).parent("td").find("input[name=PriceEarlyBird]").val() * 1)
      iPriceEarlyTotal  = iPriceEarlyTotal + iCurrVal
      
      iPriceTotalCount  = iPriceTotalCount + 1
    });

    $("td#total-count span.total").html(iPriceTotalCount)
    $("input[name=_100_Total_Amount_of_Items]").val(iPriceTotalCount)
    $("td#total-early span.total").html((iPriceEarlyTotal * .8).toFixed(2))
    $("input[name=_101_Total_Earlybird_Price]").val((iPriceEarlyTotal * .8).toFixed(2))
    $("td#total-price span.total").html(iPriceTotal.toFixed(2))
    $("input[name=_102_Total_Price]").val(iPriceTotal.toFixed(2))
  };
    
  function validate_selection() {
    start_date    = $(this).parent().find("input[name=StartDate]").val()
    end_date      = $(this).parent().find("input[name=EndDate]").val()


    // Make sure the date is valid
    if ( !IsValidDate(start_date) || !IsValidDate(end_date) ) {
      alert("This cannot be selected, there are currently no valid dates set.");
      return false;
    }
    start_date    = new Date(start_date)
    end_date      = new Date(end_date)
    
    valide_against_other_sessions(start_date,end_date,$(this))
    
    update_cost()
  }

  function valide_against_other_sessions(start_date,end_date,oItem) {
    $("input.update-cost:checked").each(function(){
      checkbox_name = oItem.parent().parent().find("input[type=checkbox]").attr("name")
      selecte_name  = oItem.parent().parent().find("span.name").html()
    
      current_checkbox_name = $(this).attr("name")
      
      if ( current_checkbox_name != checkbox_name ) {
        current_start_date    = $(this).parent().find("input[name=StartDate]").val()
        current_end_date      = $(this).parent().find("input[name=EndDate]").val()
        current_name          = $(this).parent().parent().find("span.name").html()
        
        // Make sure the date is valid
        if ( IsValidDate(current_start_date) && IsValidDate(current_end_date) ) {
          current_start_date = new Date(current_start_date)
          current_end_date = new Date(current_end_date)

          // grab the difference between the 2 days
          var difference_start_date = start_date - current_start_date;
          var difference_end_date   = start_date - current_end_date;

          // do some math
          var days_start_date = Math.round(difference_start_date/(1000*60*60*24));
          var days_end_date   = Math.round(difference_end_date/(1000*60*60*24));

          if ( days_start_date >= 0 && days_end_date <= 0 ) {
            alert("You may not select " + selecte_name + " as this conflicts with " + current_name + ".");
            oItem.removeAttr("checked")
          }
        }
      }
    });
  }
});

/***********************************************
* Safe email links - hide Email To field
***********************************************/

function hide_EmailForm1() {
	var s1 = 'info';
	var s2 = '@';
	var s3 = 'acquire.com.au';
	document.write('<input type="hidden" name="Em' + 'ail_To" value="' + s1 + s2 + s3 + '" />');
}

function hide_EmailForm2() {
	var s1 = 'careers';
	var s2 = '@';
	var s3 = 'acquire.com.au';
	document.write('<input type="hidden" name="Em' + 'ail_To" value="' + s1 + s2 + s3 + '" />');
}

function hide_EmailForm3() {
	var s1 = 'logins';
	var s2 = '@';
	var s3 = 'acquire.com.au';
	document.write('<input type="hidden" name="Em' + 'ail_To" value="' + s1 + s2 + s3 + '" />');
}

function hide_EmailForm4() {
	var s1 = 'support';
	var s2 = '@';
	var s3 = 'acquire.com.au';
	document.write('<input type="hidden" name="Em' + 'ail_To" value="' + s1 + s2 + s3 + '" />');
}
