/***********************************************
* jQuery functions execute
***********************************************/
/*
    $("div").attr("id", function (arr) {
          return "div-id" + arr;
        })
        .each(function () {
          $("span", this).html("(ID = '<b>" + this.id + "</b>')");
        });
*/
$(document).ready(function() {
	$("a.delete-advert").click(DeleteAdvert)
	$("a.delete-classified").click(DeleteClassified)
  $("a.cancel-classified").click(cancelClassified)
  $("input.save-classified").click(saveClassified)
  $("input.buy-classified").click(buyClassified)

	function DeleteClassified() {
		if ( confirm('Are you sure you want to delete this Classified') ) {
			window.location=$(this).attr("href");
		}
		return false;
	};
  
	function cancelClassified() {
		if ( confirm('Are you sure you want to cancel creating this Classified') ) {
			window.location=$(this).attr("href");
		}
		return false;
	};
  
	function saveClassified() {
		if ( !confirm('You are about to save your Listing. This means the listing will not be visible to the public and no credit will be taken from your account.') ) {
      return false;
    }
	};
  
	function buyClassified() {
		if ( !confirm('You are about to buy your Listing. The listing will be visible by public and the required credit will be deducted.') ) {
      return false;
    }
	};
  
	function DeleteAdvert() {
		if ( confirm('Are you sure you want to delete this Advertisment') ) {
			window.location=$(this).attr("href");
		}
		return false;
	};

	$("#DeliveryDestination").change(function(event) {
		//ensure our changed event is the select box
		if ( $(event.target).is("option") || $(event.target).is("select") ) {
			//check to see if the country is australia
			if ( $(this).val() == "Australia" ) {
				//show the delivery postcode option
				$("#DeliveryPostcodeOption").show();
			}
			else {
				//hide the delivery postcode option
				$("#DeliveryPostcodeOption").hide();
			}
		}
	});

	// ********* INDUSTRY CHANGE FUNCTION *********
	//run on load
	industryChange()
	
	//run on change
	$("select.industry-select").change(function(event) {
		//ensure our changed event is the select box
		if ( $(event.target).is("option") || $(event.target).is("select") ) {
			//run function
			industryChange()
		}
	});

	//industry change function
	function industryChange() {
		//for the industry drop down
		if ( $("select.industry-select").val() == 39 && $("select.industry-select").val() != '' ) {
			$("input.industry-other").show()
			$("input.industry-other").attr("disabled","").removeClass("disabled")
		} else {
			$("input.industry-other").hide()
			$("input.industry-other").attr("disabled","disabled").addClass("disabled")
		}
	}

	// ********* PRICE PER CHANGE FUNCTION *********
	// run on load
	pricePer()

	//run on click
	$("input[@name=PricePer]").click(pricePer)

	//price per function
	function pricePer() {
		//for the delivery type
		if ( $("input[@name=PricePer]:checked").val() == "Other" ) {
			$("input[@name=PricePerOther]").attr("disabled","").removeClass("disabled")
		} else {
			$("input[@name=PricePerOther]").attr("disabled","disabled").addClass("disabled")
		}
	}


	$('a.list-btn').each(function () {
		$(this).attr("href","#" + $(this).attr("name"));
	});

	$('a.list-btn').click(function(){
		//grab the form element
		var oForm = $(this).parents("form")

		//set the form action
		oForm.children("input[@name=sAction]").val($(this).attr("name"))

		//submit the form
		oForm.submit();

		return false;
	});

	/*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";});
	
	/* Popup window - class="popup" rel="standardwindow" */
	var profiles = {standardwindow:{height:500,width:770,center:1,toolbar:0,menubar:1,scrollbars:1,resizable:0},smallwindow:{height:500,width:500,status:1,menubar:1,scrollbars:1,resizable:0,center:1}};
   $(function(){$(".popup").popupwindow(profiles);});

	/*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();
	});
	
	/*
	flashing text advert
		var blinkCount = 5000*2;//no of loops
		do {$('#flash-advert')['fade'+(blinkCount%2==0?'Out':'In')](500);} while (--blinkCount);
	*/

	
	/*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();
	
	/*mouseover map area highlight*/
	$.fn.maphilight.defaults = {
		fill: true,
		fillColor: 'ffffff',
		fillOpacity: 0.5,
		stroke: false,
		strokeColor: 'ffffff',
		strokeOpacity: 0,
		strokeWidth: 0,
		fade: true,
		alwaysOn: false
	}
	$('#aus-map').maphilight();
	$('#aus-map2').maphilight();
	
	/*tooptip for map*/
	$('.tip').tooltip({
		 track: true, 
		 delay: 0, 
		 showURL: false, 
		 showBody: " - ", 
		 opacity: 0.85 
	});
	
	/*accordion*/
	$('ul#account-nav').hide();
	$('ul#account-nav').accordion({
		active: false,
		header: '.firstlevel',
		navigation: true,
		autoheight: false,
		alwaysOpen: false
	});
	$('body#hp-login ul#account-nav').hide();
	$('body#hp-login ul#nav ul li#welcome-member').hide();
	$('body#hp-account ul#nav ul li#loginpage').hide();
	$('body#hp-account ul#account-nav').show();
	
	$('ul#nav ul').accordion({
		active: false,
		header: '.hassub',
		navigation: true,
		autoheight: false,
		alwaysOpen: false
	});
	
	/* FIRST NAME INPUT */
	$('input.first-name').click(function() {
		if ( $(this).val() == 'First Name' ) {
			$(this).val('')
		}
	});
	$('input.first-name').blur(function() {
		if ( $(this).val() == '' ) {
			$(this).val('First Name')
		}
	});

	/* SURNAME INPUT */
	$('input.surname').click(function() {
		if ( $(this).val() == 'Last Name' ) {
			$(this).val('')
		}
	});
	$('input.surname').blur(function() {
		if ( $(this).val() == '' ) {
			$(this).val('Last Name')
		}
	});

	/*date picker*/
	$('.date-pick').datepicker({dateFormat: 'dd/mm/yy', changeFirstDay: false, showOn: 'both', buttonImage: 'images/icons/calendar.gif', buttonImageOnly: true});

	/*set hover class for anything*/
	/*hover for post listings*/
	$('ul.post-listings li:odd').addClass('alt-bg');
	$('ul.directory-listings li:odd').addClass('alt-bg');
	$('ul.post-listings li').hover(function() {$(this).addClass('posthover');}, function() {$(this).removeClass('posthover');});
	/*hover for buttons*/
	$('a.send').hover(function() {$(this).addClass('sendhover');}, function() {$(this).removeClass('sendhover');});
	$('.clickable').hover(function() {$(this).addClass('clickhover');}, function() {$(this).removeClass('clickhover');});
	$('form.cmxform input.submit').hover(function() {$(this).addClass('submithover');}, function() {$(this).removeClass('submithover');});
	$('input.submit').hover(function() {$(this).addClass('submithover');}, function() {$(this).removeClass('submithover');});
	$('form.cmxform input.searchnow').hover(function() {$(this).addClass('searchnowhover');}, function() {$(this).removeClass('searchnowhover');});
	$('form.cmxform input.send1').css('cursor', 'pointer').hover(function() {$(this).addClass('sendhover1');}, function() {$(this).removeClass('sendhover1');});
	/*hover for nav*/
	$('ul#nav li').hover(function() {$(this).addClass('over');}, function() {$(this).removeClass('over');});
		
	/*simple hide and show*/
	$('#advanced-search').hide();
	$('a#openadvanced').click(function() {$('#standard-search').hide(); $('#advanced-search').slideDown('slow');});
	$('a#openstandard').click(function() {$('#advanced-search').hide(); $('#standard-search').slideDown('slow');});
	
	/*directory-listings view details*/
	$('#new-directory-listings').hide();
	$('a.open-new-directory').click(function() {$('#new-directory-listings').toggle(300);return false;});
	
	/*checkout view details*/
	$('#directory-details').hide();
	$('a.open-directory-details').click(function() {$('#directory-details').toggle(300);return false;});
	$('#banner-setup-details').hide();
	$('a.open-banner-details').click(function() {$('#banner-setup-details').toggle(300);return false;});
	
	/*classifieds setup page*/
	$('#setup-help').hide();
	$('a.open-setup-help').click(function() {$('#setup-help').toggle(300);return false;});
	
	/*ad-setup*/
	$('#ad-setup').hide();
	$('#ad-queue').hide();
	$('a.open-adsetup').click(function() {$('#ad-queue').hide(); $('#ad-setup').slideDown('slow');});
	$('a.open-adqueue').click(function() {$('#ad-setup').hide(); $('#ad-queue').slideDown('slow');});
	
	/*for selecting package*/
	$("#select-package").hide();
	$("input[@name='Step2']").click(function(){
	$("input[@name='Step2']:checked").val() == 1 ? $('#select-package').toggle(300) : $('#select-package').slideUp('fast');
	});
	
	/*for selecting tender types*/
	$("#detailed-tender-description").hide();
	$("input[@name='Tender_Types']").click(function(){
	$("input[@name='Tender_Types']:checked").val() == 1 ? $('#detailed-tender-description').toggle(300) : $('#detailed-tender-description').slideUp('fast');
	});

/***********************************************
* Automatic form submition
***********************************************/
	$("select.form-submit").change(function(){
		$(this).parents("form").submit()
	});

/***********************************************
* Lissing page paging
***********************************************/
	$("select.paging").change(function(){
		window.location = $(this).val()
	});
});


/***********************************************
* Safe email links
***********************************************/

function hideEmail() {
	var s1 = "admin";
			var s2 = "@";
			var s3 = "webtrades.com.au";
			var s4 = "?Subject=Enquiry%20from%20" + s3;
			var s5 = s1 + s2 + s3;
			document.write("<a href=" + "mail" + "to:" + s1 + s2 + s3 + s4 + ">" + s5 + "</a>");
}

