var Site = {

	// this vars should be set in <head> server-side
	config: {
		base_url: '',
		site_url: ''
	},
	
	// this method is called on every page
	init: function() {
		
		// On Dom Ready
		jQuery(function($) {

			jQuery.fn.log = function (msg) {
			  console.log("%s: %o", msg, this);
			  return this;
			};
			
			jQuery.fn.input_hint = function() {
				return this.each(function() {
					var $this = $(this);

					if ($this.attr('value').length == 0)
						$this.attr('value', $this.attr('title'));

					$this.focus(function() {
						if ($this.attr('value') == $this.attr('title'))
							$this.attr('value', '');
					}).blur(function() {
						if ($this.attr('value') == '')
							$this.attr('value', $this.attr('title'));
					}).parents('form').submit(function() {
						if ($this.attr('value') == $this.attr('title'))
							$this.attr('value', '');
					});
				});
			};

			$('.hint').input_hint();
			$('#datepicker').datepicker({
				onSelect: function(date, instance) {
					$('#date').attr('value', date);
					$("form#submit").submit();
				}				
			});
			
			$('#contactForm').ajaxForm({
				target: '#alert',
				success: function() {
					$('.message').hide('slow').show('slow');
					$('.hint').input_hint();
				}
			});
			
			$(".validate").blur(function(){
				var el = $(this);
				$.ajax({
					type: 'POST',
					url: '/index/validate/',
					data: { action: 'validate', rule: el.attr('rel'), value: el.attr('value') },
					async: false,
					success: function(str) {
						el.next('span').remove();

						var result = str.split("|");
						var valid = result[0];
						var tip = result[1];

						if(valid) {
							el.after('<span style="margin:10px; float:left;"><img src="/images/icons/accept.png"></span>') ;
							el.css( 'background-color', '#FFFFFF' );
						} else if(str.length > 0) {
							el.after('<span class="tooltip" title="' + tip + '" style="margin:10px; float:left;"><img src="/images/icons/exclamation.png" border="0"></span>'); 
							el.css( 'background-color', '#FFFF99' );
							$('.tooltip').bt();
						}
					}
				});
			});	
			
			// lightbox
			$("a#findaclub-box").fancybox({
				'frameWidth'		: 777,
				'frameHeight'		: 325,
				'padding'			: 0,
				'hideOnContentClick': false,
				'overlayShow'		: true
			});
			
			$("a.careers-applynow").fancybox({
				'overlayShow'		 : true,
				'hideOnContentClick' : false,
				'padding'			 : 0,
				'frameWidth'		 : 500,
				'frameHeight'		 : 300
			});
			
			$("a#contact_us").fancybox({
				'overlayShow'		 : true,
				'hideOnContentClick' : false,
				'padding'			 : 0,
				'frameWidth'		 : 350,
				'frameHeight'		 : 400
			});
			
			$("a#lightbox").fancybox({
				'overlayShow'		 : true,
				'hideOnContentClick' : false,
				'padding'			 : 0
			});
			
			$("a#inline").fancybox({
				'frameWidth'		: 777,
				'frameHeight'		: 325,
				'hideOnContentClick': false,
				'overlayShow'		: true
			});
			
			$("a#single_image").fancybox(); 
			
		});
		
		// On Window Load
		jQuery(window).load(function ($) {

		});
		
		// Load Immediately
		(function($) {
			
		})(jQuery);
	}
};

Site.init();

function Link(url)  {
  document.location.href = url;
}

function confirm(message, callback) {
	$('#confirm').modal({
		closeHTML:"<a href='#' title='Close' class='modal-close'>x</a>",
		position: ["20%",],
		overlayId:'confirm-overlay',
		containerId:'confirm-container', 
		onShow: function (dialog) {
			$('.message', dialog.data[0]).append(message);

			// if the user clicks "yes"
			$('.yes', dialog.data[0]).click(function () {
				// call the callback
				if ($.isFunction(callback)) {
					callback.apply();
				}
				// close the dialog
				$.modal.close();
			});
		}
	});
}