(function($){	
	var accountMenu = $('#accountMenu'),
	btnSignIn = $('#btnSignIn'),
	btnCreateIpUser = $('#btnCreateIpUser'),
	Message = $('#Page_Messaging'),
	btnUpgrade = $('#upgradeSubscription'),
	btnFreetrial = $('#submitFreeTrial'),
	btnGlobalSearch = $('#btnGlobalSearch'),
	btnContactUs = $('#btnContactUs'),
	btnShareArticle = $('#btnShareArticle'),
	btnResendVerification = $('#btnResendVerification'),
	btnContinueSubscribe = $('#btnContinueSubscribe');
	
	// Start some plugins
	$('#nav-search input[type=text]').clearField();
	$("#toplinks").largemenu();
	
	// Functions
	$.Public = {
		clickSubmit: function(btn){
			btn.click(function(e){
				e.preventDefault();
				$(this).prev('input[type=submit]').click();
			});
		},
		hidePageMessage: function(){
			Message.slideUp();
			clearTimeout(messageTimer);
		},
		fadeSuccessOut: function(){
			$('#Success').fadeOut();
		},
		validateIpPersonalization: function(){
			alert('hello');
		}
	}
	//if($.trim(Message.html()).length > 0){
		//var messageTimer = setTimeout('$.Public.hidePageMessage()', 5000);
	//}
	
	$('#navigation .user').toggle(
		function(){
			accountMenu.slideDown();
		}, 
		function(){
			accountMenu.slideUp();
	});
	
	btnGlobalSearch.click(function(){
		var textbox = $(this).parent('form').find('input[type=text]').eq(0);
		if(textbox.val() === textbox.attr('title')){
			textbox.val('');
		}
		$(this).prev('input[type=submit]').click();
		return false;
	});
	// $.Public.clickSubmit(btnUpgrade);
	// $.Public.clickSubmit(btnFreetrial);
	$.Public.clickSubmit(btnSignIn);
	$.Public.clickSubmit(btnResendVerification);
	$.Public.clickSubmit(btnShareArticle);
	$.Public.clickSubmit(btnContinueSubscribe);
	
	if(!$.browser.msie && !$.browser.version.substr(0,1)<7) {
		$("#toplinks li").hover(
			function(){
				$(this).addClass('hover');
			},
			function(){
				$(this).removeClass('hover');
			}
		);
	}
	
	btnContactUs.live('click', function(e){
		e.preventDefault();
		var $this = $(this),
		post_url = $this.attr('href'),
		form = $this.parents('form'),
		isvalid = form.find('input[type=hidden]').eq(0)
		$(form).validate();
		if(isvalid.val() === 'true'){
			$this.hide().after('<img src="../images/loading.gif" border="0" />');
			$.ajax({
		        type: "POST",
		        url: post_url,
		        data: form.serialize(),
				dataType: "text",
		        success: function(data){
					$('#Success').fadeIn();
					$this.show().next('img').remove();
					setInterval('$.Public.fadeSuccessOut()', 30000);
				},
				error: function(data){
					$this.show().next('img').remove();
					alert('There was an error trying to submit this form. Please try again.');
				}
			});
		}
	});
	
	btnCreateIpUser.live('click', function(e){
		e.preventDefault();
		var $this = $(this),
		post_url = $this.attr('href'),
		form = $this.parents('form#PersonalizeForm'),
		isvalid = form.find('input[type=hidden]').eq(0)
		// Validate Required Fields
		$(form).validate({'messageContainer':'PersonalizeFormValidation'});
		if(isvalid.val() === 'true'){
			$this.prev('input[type=submit]').click();
			/*$this.hide().after('<img src="images/loading.gif" border="0" />');
			$.ajax({
		        type: "POST",
		        url: post_url,
		        data: form.serialize(),
				dataType: "text",
		        success: function(data){
					$('#PersonalizeFormSuccess').fadeIn().find('p').text(data);
					$this.show().next('img').remove();
				},
				error: function(data){
					$this.show().next('img').remove();
					alert('There was an error trying to submit this form. Please try again.');
				}
			});
			*/
		}
	});
	
})(this.jQuery);

