var alertFallback = true;

if (typeof console === "undefined" || typeof console.log === "undefined") {
	console = {};
	if (alertFallback) {
		console.log = function(msg) {
			alert(msg);
		};
	} else {
		console.log = function() {};
	}
}

/**
 * Google map bij contact info
 */
function initContactMap() {
	$("#googlemap").goMap({
		navigationControl: true, 
		mapTypeControl: false, 
		scrollwheel: false,
		address: 'Baekelandstraat 4, Zwolle', 
		zoom: 15,
		maptype: 'ROADMAP',
		
		markers: [{  
			address: 'Baekelandstraat 4, Zwolle',
			icon: {
				image: new google.maps.MarkerImage (
					'/static/img/icons/googlemarker.png',
					new google.maps.Size(51, 34),
					new google.maps.Point(0,0),
					new google.maps.Point(20, 30)
				)
			},
			html: { 
				content: '<strong>Maré-didakt</strong><br />Baekelandstraat 4<br />8013 ND Zwolle<br />Marslanden E5', 
				popup: true,
				hasShadow: false
			},
        }],
	});
}

function initMenu() {
	
	$('div.header div.menu ul li.top-item a.top-link').click(function (ev) {
		
		ev.preventDefault();

		var liElm = $(this).parent();
		
		var subMenu = liElm.find('div.submenu');
		
		if(subMenu.is(':visible'))
		{
			subMenu.hide();
			liElm.css('background','transparent');
			
			if(liElm.hasClass('last')) {
				$('div.header .menu').css({
					'background-image': 'url(/static/img/menu/menu-right.gif)'
				});
			}
		}
		else
		{
	
			// Eerst alle andere dichtklappen
			var activeLi = $('div.header div.menu ul li.top-item[rel=active]')
			activeLi.find('div.submenu').hide();
			activeLi.css('background','transparent');
	
			if(activeLi.hasClass('last')) {
				$('div.header .menu').css({
					'background-image': 'url(/static/img/menu/menu-right.gif)'
				});
			}
	
			subMenu.show();
			
			liElm.css('background','#000');
			
			if(liElm.hasClass('last')) {
				$('div.header .menu').css({
					'background-image': 'url(/static/img/menu/menu-right-hover.gif)'
				});
			}
		}
		
		liElm.attr('rel','active');
		
		liElm.hover(function () {

		},function () {

			$(this).find('div.submenu').hide();

			$(this).css('background','transparent');

			if($(this).hasClass('last')) {
				$('div.header .menu').css({
					'background-image': 'url(/static/img/menu/menu-right.gif)'
				});
			}
		});

		//console.log('click');
		
	})
	
	/*$('div.header div.menu ul li.top-item').hover(function () {
		$(this).find('div.submenu').show();
		$(this).css('background','#000');
		
		if($(this).hasClass('last')) {
			$('div.header .menu').css({
				'background-image': 'url(/static/img/menu/menu-right-hover.gif)'
			});
		}
		
	},function () {
		$(this).find('div.submenu').hide();
		
		$(this).css('background','transparent');
		
		if($(this).hasClass('last')) {
			$('div.header .menu').css({
				'background-image': 'url(/static/img/menu/menu-right.gif)'
			});
		}
	});*/
}

function productSearch() {
	
	$('form#productSearchForm').submit(function (ev) {
		
		if($(this).find('.textfield').val() == "")
		{
			ev.preventDefault();
		}
		
	});
	
	$('form#productSearchForm .textfield').keyup(function (data) {
		

		deleteSearchButton();
		
	});
	
}

function deleteSearchButton() {
	
	var textField = $('div.header input.textfield');
	
	if(textField.val() != "" && !textField.hasClass('placeholder'))
	{
		var a = $('<a />').attr('href','#').attr('id','removeSearch').html('<img src="/static/img/icons/remove-search.png" />');
		
		a.click(function (ev) {
			ev.preventDefault();
			
			textField.val("").focus();
			
			$(this).remove();

			$.ajax({
				url: '/webshop/emptyzoek'
			});
		});
		
		textField.after(a);
	}
	else
	{
		$('div.header #removeSearch').remove();
	}
	
}

$(document).ready(function () {
	
	$('div.your-benefits div.employee').click(function (ev) {
		location.href = '/over-mare-didakt';
	});
	
	$('div.header li.cart').click(function (ev) {
		location.href = '/webshop/winkelwagen';
	});
	
	$('a.colorbox').colorbox();
	$('a.colorpopup').colorbox();
	
	$('div.header input.textfield').enablePlaceholder();
		
	// Boeken link
	$('div.study-books').click(function () {
		 location.href = '/boeken';
	});
	
	deleteSearchButton();
	initContactMap();
	initMenu();
	productSearch();

});
