/**
 * @author author
 */

/* ::Enable Javascript-enabled stylesheet */
var head_css = $('<link rel="stylesheet" href="presentation/css/js-enabled.css" type="text/css" media="screen" />').appendTo($('head')[0]);

/* -------------------------------------- */


/* ::Document Ready function Callers  */
$(document).ready(function(){
	
	//FnSocialButtons.init();
	FnInputFocus.init();
	FnFixLegend.init();
	FnMenu.init();
	FnMaillistVal.init();	
	FnToolTip.init();	
	
});
/* -------------------------------------- */

/* ::Default Functions */
FnDefault = {
	init:function() {}	
	
	
	/* -------------------------------------- */
}

/* ::ToolTip Functions */
FnToolTip = {
	init:function() {
		// only do this when there are elements with classname 'tooltip' on the page
		// otherwise this will generate an error message.
		var numToolTips = $('a.tooltip').length;
		if (numToolTips > 0) {
			$('.sub-nav a.tooltip').tooltip({
				showURL	: false,
				left	: -80,
//				top		: 75,
				fixPNG: true
			});
			$('a.tooltip-taj').tooltip({
				showURL	: false,
				left	: -80,
//				top		: 85,
				extraClass: "cat",
				fixPNG: true
			});			
			$('#mn-catering').tooltip({
				showURL	: false,
				left	: -90,
//				top		: 85,
				extraClass: "cat",
				fixPNG: true
			});
		} else {
			return true;
		}	
	}	
	/* -------------------------------------- */
}


/* ::Menu Functions */
FnMenu = {
	init:function() {
		// disable click
		$('a.toggle').click(function() {
			return false;
		});

		$('#secondary-nav').hover(function() {
			return false;
		},
		function() {
			$(this).find('.hover').removeClass('hover');
			$(this).find('.show').removeClass('show');
		});
		
		$('#mn-catering').hover(function() {
			$(this).parent().parent().parent().find('.hover').removeClass('hover');
			$(this).parent().parent().parent().find('.show').removeClass('show');
		},
		function() {
			return false;							
		});
		
		$('#mn-catering').click(function() {
			var win=open($(this).attr('href'),'win','resizable=1,scrollbars=1,menubar=1,toolbar=1');
		});
		
		// hover 
		$('a.toggle').hover(function() {
			$('a.toggle').addClass('dim');
			
			//alert('over');
			$(this).parent().parent().find('.show').removeClass('show');
			$(this).parent().parent().find('.hover').removeClass('hover');
			$(this).parent().addClass('hover');
			//$(this).next().show();
			$(this).next().next().addClass('show');
		},
		function() {
			// timer?
			var thisSub = $(this);
			//setTimeout (hideSub,500); 
			t = setTimeout(function() { 
			        //thisSub.next().hide('fast');
         			$(this).next().next().removeClass('show');
					$('a.toggle').removeClass('dim');
			    }, 0); 
		});
		
		// sub nav rollovers
		$('.sub-nav').hover(function() {
			clearTimeout(t);
		},
		function() {
			//setTimeout(t);
			//close itself
			//$(this).hide('fast');
   			$(this).removeClass('show');	
			$('#secondary-nav').find('.hover').removeClass('hover');	
			$('a.toggle').removeClass('dim');
		});
	}	
	/* -------------------------------------- */
}

/* ::Social btns Functions */
FnSocialButtons= {
	init:function() {
		
		$('<ul class="social"></ul>')
			.appendTo('#primary-nav');
			
		FnSocialButtons.facebook();
		FnSocialButtons.twitter();		
	},
	facebook:function() {
		$('<li><a class="social facebook" href="#" type="icon" name="fb_share">facebook</a></li>')
			.appendTo('ul.social');
	},	
	twitter:function() {
		$('<li><a class="social twitter" href="#">Tweet this</a></li>')
			.appendTo('ul.social');
		$('a.twitter').click(function(){
			TwitThis.pop();	
		});		
	}
	
	/* -------------------------------------- */
}


/* FnInputFocus */
/* :: remove and replace text field example content */
FnInputFocus = {
	init:function() {
		
	// when text field gets clicked
	$(':input.example').focus(function() {
		// if not empty and if class example get value
		if ($ (this) .is('.example') && !(this.value == ''))  {
			// store value
			exampleValue = this.value;
			// empty value
			$(this).attr({'value' : ''})
			// remove class example
			$(this).removeClass('example');
		};
	}).blur(function() {
		// if empty: replace example value
		if (this.value == '') {
			$(this).addClass('example')
			$(this).attr({'value' : exampleValue})
		};
	});
	}
	
	/* -------------------------------------- */
}


/* ::Function to replace legend elem with h2 */
FnFixLegend = {
	init:function() {
	
		$('legend').each(function() {
			// get text
			var thisLegendText = $(this).text();
			// create h2 with text
			$('<h2 class="legend"></h2>')
				.text(thisLegendText)
				.insertAfter(this);
			$(this).remove();
		});
	}	
	
	
	/* -------------------------------------- */
}

/* ::Maillist validation Functions */
FnMaillistVal = {
	init:function() {
		//FnMaillistVal.checkField();		
		FnMaillistVal.formSubmit();		
	
	},
	checkField:function() {
		
		$('#frm-maillist :input').blur(function() {
			//alert('oi');
			$('#frm-maillist').removeClass('warning');
			$('.error-message').remove();
			if ($(this).is('#txt-ml-email')) {
				if (this.value == '') {
					//alert('error');
					var errormessage = 'This is a required field';
					$('<span></span>')
						.addClass('error-message')
						.text(errormessage)
						.appendTo('#primary-nav');
					$('#frm-maillist').addClass('warning');
				} else {
					if (this.value != '' && !/.+@.+\.[a-zA-Z]{2,4}$/.test(this.value)) {
						var errormessage = 'Please enter a valid email';
						$('<span></span>')
							.addClass('error-message')
							.text(errormessage)
							.appendTo('#primary-nav');
						$('#frm-maillist').addClass('warning');
					}
				}
			} 			
		});
	},
	formSubmit:function() {
		$('#frm-maillist').submit(function() {
			FnMaillistVal.checkField();
			$('#txt-ml-email').trigger('blur');
			var numWarnings = $('.warning').length;
				if (numWarnings) {
					return false;
				} else {
					$('<span></span>')
						.addClass('error-message')
						.html('<img src="presentation/images/icons/loading.gif" alt="Loading..." />')
						.appendTo('#primary-nav');
					$('#frm-maillist').addClass('warning');
					
					var dataString = 'email=' + $('input#txt-ml-email').val();
					
					$.ajax({
						type: 'POST',
						url: 'maillist.php',
						data: dataString,
						dataType : 'html',
						success: function(data) {
							$('#frm-maillist').removeClass('warning');
							$('.error-message').remove();
							
							$('<span></span>')
								.addClass('error-message')
								.html(data)
								.appendTo('#primary-nav');
							$('#frm-maillist').addClass('warning');
							
							$('input#txt-ml-email').val('');
						},
						error:function (xhr, ajaxOptions, thrownError) {
							//alert(xhr.status + ',' + xhr.statusText);
							
							$('#frm-maillist').removeClass('warning');
							$('.error-message').remove();
							
							var errormessage = 'An error has occurred.';
							$('<span></span>')
								.addClass('error-message')
								.text(errormessage)
								.appendTo('#primary-nav');
							$('#frm-maillist').addClass('warning');
						}  
					});
					
					return false;
				}
		});	
	}
	/* -------------------------------------- */
}



