function initNewsletterForm(){
	$newsletterFrm = $('#subForm');
	$newsletterFrm.validate({
		rules: {
			"cm-name": "required",
			"cm-f-tuduju": "required",
			"cm-kjjuur-kjjuur": {
				required: true,
				email: true
			}
		},
		messages: {
			"cm-name": "Please enter your name",
			"cm-f-tuduju": "Please enter your surname",
			"cm-kjjuur-kjjuur": "Please enter a valid email address"
		},
		submitHandler: function() { 
			var action		= $newsletterFrm.attr("action"),
				name		= $('#name').attr('value'),
				surname		= $('#Surname').attr('value'),
				email		= $('#kjjuur-kjjuur').attr('value'),
				str 		= $newsletterFrm.serialize(),
				data 		= str + "&action=" + action;
						
			submitNewsletter(data);
			return false;
		}
	});	
}

function submitNewsletter (data) {
	$.ajax({  
		type: "POST",  
		url: "/proxy.php",  
		data: data,  
		success: function(html){
			$("#nyroModalContent").html('<p class="thanks">Thank you for signing up for our newsletter!</p>');
		}  
	});
}

function nyroHideContent(elts, settings, callback) {
	$("#closeBut").hide();
	elts.content.hide();
	elts.contentWrapper
		.animate({
			height: '50px',
			width: '50px',
			marginTop: (-(25+settings.borderH)/2 + settings.marginScrollTop)+'px',
			marginLeft: (-(25+settings.borderW)/2 + settings.marginScrollLeft)+'px'
		}, {duration: 420, complete: function() {
			elts.contentWrapper.hide();
			callback();
		}});
}

$(document).ready(function(){
	$("body").removeClass("no-script");
	
	$("#nav-social a").hover(
		function() {
			$(this).next("em").stop(true, true).animate({opacity: "show", top: "-26"},250);
		}, 
		function() {
			$(this).next("em").animate({opacity: "hide", top: "-21"},100);
		}
	);
	
	/* Create default popover styles for featured item links */
	$.nyroModalSettings({
		debug:false,
		width:640, 
		height:620,
		hideContent:nyroHideContent,
		contentLoading:'<span class="loading">Loading...</span><br /><a href="#" class="nyroModalClose">Cancel</a>',
		contentError:'<div id="errorContent">This page could not be loaded.<br />Please try again.<br /><a href="#" class="nyroModalClose">Close</a></div>'
	});	
	
	/* Add popover call to featured links */
	$('.link-featured').nyroModal().each(function(i){
		$(this).attr("href",$(this).attr("href")+"#popup-content");
	});
	
	/* Add popover call to menu links and click action for popover */
	$('.link-menu').append('<span class="hover"></span>')
					.bind("mouseenter focus", function() {				
						$(this)
							.stop()
							.animate({'color':'#999999'},350);
						$(this).find("span.hover").fadeIn(300);
					})
					.bind("mouseleave blur", function() {
						$(this)
							.stop()
							.animate({'color':'#990600'},300);
						$(this).find("span.hover").fadeOut(300);
					})
					.click(function(e) {
						e.preventDefault();
						linkUrl = $(this).attr("href")+"#popup-content";
					
						$.nyroModalManual({
							url: linkUrl,
							width:540,
							minHeight:600,
							hideContent:nyroHideContent
						});
						return false;
					});

	$('.link-menu-direct').append('<span class="hover"></span>')
					.bind("mouseenter focus", function() {				
						$(this)
							.stop()
							.animate({'color':'#999999'},350);
						$(this).find("span.hover").fadeIn(300);
					})
					.bind("mouseleave blur", function() {
						$(this)
							.stop()
							.animate({'color':'#990600'},300);
						$(this).find("span.hover").fadeOut(300);
					});
	
	
	/* Add popover call to signup links */
	$('#nav15, .link-signup').click(function(e) {
		e.preventDefault();
		linkUrl = $(this).attr("href")+"#newsletter-content";
		
		$.nyroModalManual({
			url: linkUrl,
			windowResize: false,
			width:425,
			height: 380,
			minHeight:400,
			hideContent:nyroHideContent,
			endShowContent: initNewsletterForm
		});
		return false;
	 });
	
	/* Add hover colour change to block signup links */
	$(".link-signup").append('<span class="hover"></span>')
						.bind("mouseenter focus", function() {				
							$(this)
								.stop()
								.animate({'color':'#999999'},350);
							$(this).find("span.hover").fadeIn(300);
						})
						.bind("mouseleave blur", function() {
							$(this)
								.stop()
								.animate({'color':'#990600'},300);
							$(this).find("span.hover").fadeOut(300);
						});
	
	/* Add hover border colour change to featured items */
	$("div.section-btm div.panel-link").bind("mouseenter focus", function() {				
							$(this).find("img")
								.stop()
								.animate({'borderColor':'#520400'},550);
						})
						.bind("mouseleave blur", function() {
							$(this).find("img")
								.stop()
								.animate({'borderColor':'#000000'},450);
						});
	
	/* Add hover colour change to main nav links*/
	$("#nav-main a").before('<span class="hover"></span>')
					.bind("mouseenter focus", function() {				
						$(this).siblings("span.hover").fadeIn(350);
					})
					.bind("mouseleave blur", function() {
						$(this).siblings("span.hover").fadeOut(250);
					});
	
	/* Add hover colour change to header links*/
	$("#nav-site a").bind("mouseenter focus", function() {		
							$(this)
								.stop()
								.animate({'color':'#FFFFFF'},250);
						})
						.bind("mouseleave blur", function() {
							$(this)
								.stop()
								.animate({'color':'#999999'},200);
						});
	
	/* Add hover colour change to footer links*/
	$("#nav-footer a")
		.bind("mouseenter focus", function() {				
			$(this)
				.stop()
				.animate({'color':'#990600'},280);
		})
		.bind("mouseleave blur", function() {
			$(this)
				.stop()
				.animate({'color':'#666666'},200);
		});
	
});
