$(document).ready(function () {

	$('#newsletterinput2').live('keypress', function (e) {
		if (e.which && e.which == 13) {
			$('#newsletterbuttonfooterbutton').click();
		}
	});

	$('#newsletterbuttonfooterbutton').live('click', function() {
		signupnewsletter('newsletterinput2', 'newsletterfooterdiv');
	});
	
	$('#backToTop').click(function() {
		 $('html, body').animate({scrollTop: '0px'}, 'fast');

	     return false;	
	});
	
	var megaConfig = {
		interval: 250,
		sensitivity: 5,
		over: addMega,
		timeout: 500,
		out: removeMega
	}
	
	$('li.mega').hoverIntent(megaConfig);
	//$('li.mega').hoverIntent(addMega, removeMega);
	
	$('#nav > li:gt(0) ').each(function() {
		
		var currentLi = $(this).attr('id');
		
		if (currentLi) {
			// When clicking on main li
			$('#'+currentLi).click(function() {
			
				// Remove jsHover from other li's
				$('#nav').children('li:gt(0)').removeClass('jsHover');

				// Add clas jsHover
				$(this).addClass('jsHover');			
			});		
		}
	});
	
	$('#nav li.mega > div').each(function() {

		var divHeight = $(this).height();
		$(this).find('.topMenuDivider').height(divHeight);
	});
	
	
	/*
	// Show subnav div
	$('#nav li:gt(0)').each(function () {		
		
		var li = $(this).attr('id');		
		
		$('#'+li+' a:first').click(function () {	
			
			$('#nav').children('li:gt(0)').addClass('jsHover');
				
			$('#nav').mouseleave(function() {
			
//				$('#nav').children('li:gt(0)').removeClass('jsHover');
			});
						
			return false;			
		});
	});
	
	*/
});


function addMega() {
		
	// Remove jsHover from other li's which where clicked
	$('#nav').children('li:gt(0)').removeClass('jsHover');	
	
	$(this).addClass('jsHover');		
}

function removeMega() {
	
	$(this).removeClass('jsHover');
}


function isValidEmailAddress(emailAddress) {
	var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
	return pattern.test(emailAddress);
}


function signupnewsletter(inputfield, showdiv){

	postUrl = '/en/newsletter/';

	var email = $('#'+inputfield).val();

	if(email != 0){

		if(isValidEmailAddress(email) == true){
			$.post(postUrl+"subscribe_ajax/", {

				"email" : email

			},
			function(data){

				//console.log(data.time); //  2pm
				$('#'+showdiv).html('<p>'+data.msg+'</p>');
			}, "json");




		}else{

			$('#'+showdiv).html('Your email address is incorrect');

		}
	}else{

		$('#'+showdiv).html('Your email address is invalid');

	}


	$('#'+showdiv).animate({
		height: 'toggle'
	}, 600).delay(3000).animate({
		height: 'toggle'
	}, 600, function(){

	});

}

function changeLanguage(old_taal, new_taal) {

	if (old_taal != new_taal) {

		var url = window.location.pathname;
		$.post('/'+old_taal+'/language/', {
			url: url, 
			old_taal: old_taal, 
			new_taal: new_taal
		}, function (returnData) {
				
			window.location = returnData;
		});
	}
}

function ajax_modal(url, data)
{
       $('#ajaxload').css("display", "block");
	
 
$('#ajaxload').css( 'opacity', 0.8 );

    if( url.substr(0,1) != '/' )
    {
        url = '/nl/'+url;
    }

	$.post(url, data, function(return_data){

        JSON_correct = true;
        try {
            return_data = $.parseJSON(return_data);
        } catch(e) {
            JSON_correct = false;
        }

       $('#ajaxload').css("display", "none");

        if( JSON_correct )
        {
            modal_width = 300;
            modal_height = 200;
            modal_html = '';
            modal_close = true;
            modal_onShow = '';
            modal_caption = '';
            modal_description = '';

            if( return_data.html )
            {
                modal_html = return_data.html;
            }
            if( return_data.width )
            {
                modal_width = return_data.width;
            }
            if( return_data.height )
            {
                modal_height = return_data.height;
            }
            if( return_data.caption )
            {
                modal_caption = return_data.caption;
            }
            if( return_data.description )
            {
                modal_description = return_data.description;
            }
            if( return_data.close )
            {
                if( return_data.close == 'false' )
                {
                    modal_close = false;
                }
                else
                {
                    modal_close = true;
                }
            }
            if( return_data.onShow )
            {
                modal_onShow = return_data.onShow;
            }

            $.modal.close();
            $.modal(modal_html,{
                    minWidth: modal_width,
                    maxWidth: modal_width,
                    minHeight:modal_height,
                    maxHeight:modal_height,
                    close: modal_close,
                    escClose: modal_close,
                    caption: modal_caption,
                    description: modal_description,
                    onShow: function () {eval(modal_onShow)}
            });
        } else {
            alert("JSON incorrect!");
            $.modal.close();
            $.modal( return_data );
        }

    });

}

