$(document).ready(function() {
	//----------------- General  ---------------------												 
	$(".datepicker").datepicker({ dateFormat: 'dd/mm/y' });
	
													 
	//----------------- Login tab show hide ---------------------
	$('#login').click(function(e) {
		e.preventDefault(); 
		$('#login_sub').toggle(300);                          
	});
	
	//stop click on a elemets hiding login_sub
	$('#login_sub a').click(function(e) { e.stopPropagation(); });	
	
	//----------------- Results Table TR hover and default tr click ---------------------			
	$('.results a').click(function(e) { e.stopPropagation(); });
	$(".results tr")
		.click(function() {	
			if( $(this).children("td.action") && $(this).children("td.action").children("a:first").attr('href') ) {
				window.location = $(this).children("td.action").children("a:first").attr('href');	
			}															
		})
		.mouseover(function() { 
			$(this).addClass("hover");			
		})
		.mouseout(function() {					
			$(this).removeClass("hover");												
	});	
	
	//----------------- Displays confirm prompt when click using title as question ---------------------	
	$(".yes_no_prompt").click(function(e) {			
		var ok = confirm($(this).attr('title') + '?');
		if (!ok) {                
			e.preventDefault(); 
		}
	});	
	
	
	//----------------- Fancy Box ---------------------
	$(".signin").fancybox({
      'width'				: 565,
			'height'			: 400,
			'autoScale'     	: false,
			'autoScale'		: false,
			'transitionIn'	: 'fade',
			'transitionOut'	: 'fade',
			'speedIn'		: 600, 
			'speedOut'		: 200,
			'overlayOpacity': .7,
			'overlayColor'	: '#333',
			'type'				: 'iframe',
			'padding'		:	'20px'
		});

		$(".inline").fancybox({
      'width'				: 565,
			'height'			: 350,
			'autoScale'     	: false,
			'autoScale'		: false,
			'transitionIn'	: 'fade',
			'transitionOut'	: 'fade',
			'speedIn'		: 600, 
			'speedOut'		: 200,
			'overlayOpacity': .7,
			'overlayColor'	: '#333',
			'padding'		:	'20px'
		});

		$(".iframe").fancybox({
			'width'				: 700,
			'height'			: '85%',
			'autoScale'     	: false,
			'autoScale'		: false,
			'transitionIn'	: 'fade',
			'transitionOut'	: 'fade',
			'speedIn'		: 600, 
			'speedOut'		: 200,
			'overlayOpacity': .7,
			'overlayColor'	: '#333',
			'type'				: 'iframe',
			'padding'		:	'20px'
		});
		
		$(".iframe_wide").fancybox({
			'width'				: 800,
			'height'			: '85%',
			'autoScale'     	: false,
			'autoScale'		: false,
			'transitionIn'	: 'fade',
			'transitionOut'	: 'fade',
			'speedIn'		: 600, 
			'speedOut'		: 200,
			'overlayOpacity': .7,
			'overlayColor'	: '#333',
			'type'				: 'iframe',
			'padding'		:	'20px'
		});		
		
		$(".inline.submit").fancybox({
      'width'				: 565,
			'height'			: 350,
			'autoScale'     	: false,
			'autoScale'		: false,
			'transitionIn'	: 'fade',
			'transitionOut'	: 'fade',
			'speedIn'		: 600, 
			'speedOut'		: 200,
			'overlayOpacity': .7,
			'overlayColor'	: '#333',
			'padding'		:	'20px',
			'onClosed'		: function() {
				$("#submit").trigger('click');
			}
		});	
		
		$("a.vt_tour").click(function() {
			widthString = $(this).attr('rel');
			var widthInt = parseInt(widthString);
			if( widthInt == '950') {
            var heightInt = parseInt(680);  
         }
         else {
            var heightInt = parseInt(660);
         }			
			
			$.fancybox({
					'padding'		: 0,
					'autoScale'		: false,
					'transitionIn'	: 'elastic',
					'transitionOut'	: 'elastic',
					'speedIn'		: 600, 
					'speedOut'		: 200,
					'overlayOpacity': .7,
					'overlayColor'	: '#333',
					'width'			: widthInt,
					'height'		   : heightInt,
					'href'			: this.href,
					'type'			: 'iframe'
				});
		
			return false;
		});
		
		$("a.vt_video").click(function() {
			$.fancybox({
					'padding'		: 0,
					'autoScale'		: false,
					'transitionIn'	: 'elastic',
					'transitionOut'	: 'elastic',
					'speedIn'		: 600, 
					'speedOut'		: 200,
					'overlayOpacity': .7,
					'overlayColor'	: '#333',
					'width'			: 680,
					'height'		: 495,
					'href'			: this.href.replace(new RegExp("watch\\?v=", "i"), 'v/'),
					'type'			: 'swf',
					'swf'			: {
						 'wmode'		: 'transparent',
						'allowfullscreen'	: 'true'
					}
				});
		
			return false;
		});
	
});//end document.ready
	
//--------------- Custom Functions --------------

//Company Filter
$.fn.companyFilter = function() {
	this.change(function () {									
		val = $(this).find("option:selected").val();
		if( val == 'all') {
			$('.company').show();	
		}
		else {
			$('.company').hide();
			$('#company_'+ val).show();
		}			
	}).trigger('change')
}

//Job Pack qty adding and removal
$.fn.addPack = function() {
	this.click(function () { 
		if ( $(this).siblings(".qty").val() < 1) {
			$(this).siblings(".qty").val(1)
		}
		else {
			$(this).siblings(".qty").val( parseInt($(this).siblings(".qty").val()) + 1)
		}
		$('#calc').trigger('click');
	})
}

$.fn.removePack = function() {
	this.click(function () { 
		if ( $(this).prev('.qty').val() <= 1) {
			$(this).siblings(".qty").val(null)
		}
		else {
			$(this).siblings(".qty").val( parseInt($(this).siblings(".qty").val()) - 1)
		}
		$('#calc').trigger('click');
	})
}	

//Show hide for parent child 
$.fn.subSelectSwap = function() {
	this.change(function () { 
			
		//find all select boxes with class mathcing id 	
		$('select.'+$(this).attr('id')).each(function(){ 
			$(this).hide();
			//disable all options to prevent value being posted
			$(this).children().each(function(){	$(this).attr("disabled", "disabled");	});
		});			
		
		//Show select box with id of "select box id_selected option class"
		$('#'+$(this).attr('id')+'_'+$(this).find("option:selected").attr('value')).show();
		//remove disabled from all options of sub select
		$('#'+$(this).attr('id')+'_'+$(this).find("option:selected").attr('value')).children().each(function(){	$(this).removeAttr("disabled"); });

	})
	.trigger('change');

}


//Get URL parameters
$.extend({
  getUrlVars: function(){
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
      hash = hashes[i].split('=');
      vars.push(hash[0]);
      vars[hash[0]] = hash[1];
    }
    return vars;
  },
  getUrlVar: function(name){
    return $.getUrlVars()[name];
  }
});





	
	
	
			

