// JavaScript Document
function URLDecode (encodedString) {
  var output = encodedString;
  var binVal, thisString;
  var myregexp = /(%[^%]{2})/;
  while ((match = myregexp.exec(output)) != null
             && match.length > 1
             && match[1] != '') {
    binVal = parseInt(match[1].substr(1),16);
    thisString = String.fromCharCode(binVal);
    output = output.replace(match[1], thisString);
  }
  return output;
}
var site={
	enquire:function(){
		// add * to required field labels
		$('label.required').append('&nbsp;<strong>*</strong>&nbsp;');
		$('#EnquiryForm').validate();	
	},
	contact:function(){
		// add * to required field labels
		$('label.required').append('&nbsp;<strong>*</strong>&nbsp;');
		$('.datepicker').datepicker({
			showOn: 'both', 
			buttonImage: '/img/icons/date.gif', 
			buttonImageOnly: true, 
			changeMonth: true, 
			changeYear: true, 
			dateFormat:'yy-mm-dd'
		});
		// accordion functions	
		var accordion = $("#stepForm").accordion({ autoHeight: false }); 
		var current = 0;
		
		$.validator.addMethod('pageRequired', function(value, element) {
			var $element = $(element)
			function match(index) {
				return current == index && $(element).parents('#sf' + (index + 1)).length;
			}
			if (match(0) || match(1) || match(2)) {
				return !this.optional(element);
			}
			return 'dependency-mismatch';
		}, $.validator.messages.required);
		
		var v = $("#ContactForm").validate({
			/*onkeyup: false,
			onblur: false,
			submitHandler: function() {
				alert("Submitted, thanks!");
			}*/
			onkeyup: false,
			onblur: false
		});
		
		// back buttons do not need to run validation
		$('#sf2 .prevbutton').click(function(){
			accordion.accordion('activate', 0);
			current = 0;
		}); 
		/*$('#sf3 .prevbutton').click(function(){
			accordion.accordion('activate', 1);
			current = 1;
		});*/
		// these buttons all run the validation, overridden by specific targets above
		$('.open2').click(function() {
		  if (v.form()) {
		    accordion.accordion('activate', 2);
		    current = 2;
		  }
		});
		$('.open1').click(function() {
		  if (v.form()) {
		    accordion.accordion('activate', 1);
		    current = 1;
		  }
		});
		$('.open0').click(function() {
		  if (v.form()) {
		    accordion.accordion('activate', 0);
		    current = 0;
		  }
		});
	},
	gallery:function(){
		$('.tb').click(function(){
			$('#displayDetails .p1').html('<img src="/img/ajax-loader-b.gif" style="border:none; background:none" />');
			var id = $(this).attr('id');
			var imageSrcStr = $(this).children('img').attr('src'); // get image srouce
			imageSrcStr = imageSrcStr.replace('/resize/image/w:110/h:110/q:100/zc:1', '/resize/image/w:360/h:300/q:100/zc:2') // replace resize settings
			imageTitleStr = $(this).children('img').attr('alt'); // get image title (alt)
			$('#displayDetails .p1').html('<img src="'+imageSrcStr+'" style="border:none; background:none" />');
			$('#displayDetails .p2').html(URLDecode(imageTitleStr)+'<br /><a href="/enquiry/'+id+'">Contact us for pricing (click here)</a>');
		});
	}
};