//
//	var isiPad = navigator.userAgent.match(/iPad/i) != null;
//	var agent=navigator.userAgent.toLowerCase();
//	var is_iphone = ((agent.indexOf('iphone')!=-1));
//
//if(is_iphone){
//	alert('a');
//}

$(document).ready(function () {
	
	var isiPad = navigator.userAgent.match(/iPad/i) != null;
	
	if (isiPad)
	{
		$('#nav li.mega > a').each(function()
		{
			$(this).removeAttr('href');
		});
		
		$('#nav li.mega').click(function()
		{
			$('#nav li.mega').each(function()
			{
				$(this).removeClass('jsHover');
			});
			
			$(this).addClass('jsHover');
		});
	}
	else
	{
		$('li.mega').hover(function() {
		
			$('#nav li.mega').each(function(){
				$(this).removeClass('jsHover');
			});

			try { clearTimeout(hover_delay); } catch(e) { }
			$(this).addClass('jsHover');
		}, function() {

			li_obj = $(this);

			hover_delay = setTimeout(function() {

				var li_id = li_obj.attr('id');
				$('#'+li_id).removeClass('jsHover');
			}, 500);
		});
	}


	
	
	$('ul.childs li.subnav_items').mouseenter(function() {
		
		$(this).parent().find('li div.banners_right div.banner').hide();

		var li_hover = 'hover_' + $(this).attr('id').replace('menu_', '');
		$(this).parent().find('li div.banners_right div#' + li_hover).show();			
	});

	$('#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;	
	});
});

function addMega() {$(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 = shoppingbag_messages.newsletterUrl;

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

	$('#'+showdiv).html('');

	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>');

				if(data.error !='double'){
					_gaq.push(['_trackPageview', '/virtual/newsletter-registration/']);
				}
			}, "json");
			// MJF - add GA virtual page tracking
		}else{

			$('#'+showdiv).html(shoppingbag_messages.emailIncorrect);
		}

	}else{

		$('#'+showdiv).html(shoppingbag_messages.emailIncorrect);
	}

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

		if ($('#newsletter_p').is(':visible')) {

			$("#backgroundPopup").fadeOut("slow");
			$("#newsletter_p").fadeOut("slow");
		}
	});
}

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;
		});
	}
}

