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();
				}				
			});
			
			var formSubmit = false;
			$('#contactForm').ajaxForm({
				target: '#alert',
				beforeSubmit: function(){
					$('#contactForm').find('input[type="submit"]').hide();
					if(formSubmit === false){
						formSubmit = true;
						return true;
					}else{
						return false;
					}
				},
				success: function() {
					
					$('#contactForm').find('input[type="submit"]').show();
					$('.message').hide('slow').show('slow');
					$('.hint').input_hint();
					formSubmit = false;
				}
			});
			
			$(".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'		: 797,
				'frameHeight'		: 346,
				'padding'			: 0,
				'hideOnContentClick': false,
				'overlayShow'		: true
			});
			
			$("a#joinemail-box").fancybox({
				'frameWidth'		: 797,
				'frameHeight'		: 346,
				'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'		: 797,
				'frameHeight'		: 346,
				'hideOnContentClick': false,
				'overlayShow'		: true
			});
			
			$("a#single_image").fancybox();
			
			$("a.kidsactivity-box").fancybox({
				'frameWidth'		: 400,
				'frameHeight'		: 440,
				'padding'			: 20,
				'hideOnContentClick': false,
				'overlayShow'		: true
			});

			$("a#video_1").fancybox({
				'frameWidth'		: 640,
				'frameHeight'		: 385,
				'padding'			: 10,
				'hideOnContentClick': false,
				'overlayShow'		: true
			});
			
			$("a#video_2").fancybox({
				'frameWidth'		: 640,
				'frameHeight'		: 385,
				'padding'			: 10,
				'hideOnContentClick': false,
				'overlayShow'		: true
			});
			
			
			
			var countedFields = $('textarea');
			$(countedFields).each(function() {
				
				if ($(this).attr('maxchars') != null) {
					var maxcount = $(this).attr('maxchars');
					var countField = $('<div class="countField"></div>');
					var input = $(this);					
					var count = maxcount;					
					var tmpVal = $(input).val();
					count = maxcount - tmpVal.split(' ').length;
					countField.html('Words Left: '+ count);
					countField.insertAfter(this);
					
					$(this).keypress(function(event) {
		
					tmpVal = $(input).val();
					count = maxcount - tmpVal.split(' ').length;
					
						if (event.keyCode == 13) {
							input.val(input.val() + ' ');
						}
											
						if (count < 1) {
	
							input.val(tmpVal.split(" ",maxcount).join(' ') + ' ');
							count = 0 ;
							
						} 
						
						
						if (count < 10) {
							$(countField).addClass('red');
						} else {
							$(countField).removeClass('red');
						}
						
					countField.html('Words Left: '+ count);	
							
					})
							
				}
				
				
			});
			
		});
		
		// 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();
			});
		}
	});
}

function pt_show(divid) {
	//alert (divid);
	$('#pt_submenu li').removeClass('selected');
	$('#pt_submenu_'+divid).addClass('selected');
	$('#pt_content div').slideUp('fast');
	$('#pt_content_'+divid).slideDown('slow');
}
