var map = null;
var geocoder = null;

function initialize() {
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("map-container"));
		map.setUIToDefault();
		geocoder = new GClientGeocoder();
	}
	showAddress('Joseph Plateaustraat 5 8400 Oostende, Belgium');
}

function showAddress(address) {

	var gdwIcon = new GIcon();
	gdwIcon.image = "./img/gmap-icon.png";
	gdwIcon.shadow = "./img/gmap-icon-shadow.png";
	gdwIcon.iconSize = new GSize(27, 35);
	gdwIcon.shadowSize = new GSize(43, 26);
	gdwIcon.iconAnchor = new GPoint(27, 35);

	markerOptions = { icon:gdwIcon };

	if (geocoder) {
		geocoder.getLatLng( address,
			function(point) {
				if (!point) {
					alert(address + " not found");
				} else {
					map.setCenter(point, 16);
					var marker = new GMarker(point, markerOptions);
					map.addOverlay(marker);
					GEvent.addListener(marker, "click", function() {
						// GO TO GOOGLE MAPS FOR ROUTE
						window.open('http://maps.google.be/maps?f=q&source=s_q&hl=nl&geocode=&q=' + address)
					});
				}
		});
	}
}

function check_btw(el) {
	land = $('#land-select option:selected').val();
	vat = el.val();
	input = el;
	
	$('input[name=valid_btw]').val('valid');
	if(vat.length >= 8) {
		$('#submit-btn').attr('disabled', 'true');
		el.addClass('loading').removeClass('error').removeClass('success');
		$.getJSON(
			'http://isvat.appspot.com/' + land + '/' + vat + '/?callback=?',
			function(data) {
				if(data) {
					input.removeClass('loading').addClass('success');
					$('input[name=valid_btw]').val('valid');
				} else {
					input.removeClass('loading').addClass('error');
					$('input[name=valid_btw]').val('invalid');
				}
				$('#submit-btn').removeAttr('disabled');
			}
		);
	} else {
		input.removeClass('loading')
	}
}

$(document).ready(function() {
	
	/* --------------------------------------------------------- */
	/* FORM
	/* --------------------------------------------------------- */
	$('#tell-a-friend-form').validate({submitHandler:function(form) { $(form).ajaxSubmit({target:'#tell-a-friend-form-feedback', success:function() { $('#tell-a-friend-form-feedback').show(); }}) }});
	$('#info-request-form').validate({submitHandler:function(form) { $(form).ajaxSubmit({target:'#info-request-form-feedback', success:function() { $('#info-request-form-feedback').show(); }}) }});
	$('#mail-list-form').validate({submitHandler:function(form) { $(form).ajaxSubmit({target:'#mail-list-form-feedback', success:function() { $('#mail-list-form-feedback').show(); }}) }});
	
	/* --------------------------------------------------------- */
	/* SLIDE UP AND DOWN OF CALENDAR
	/* --------------------------------------------------------- */
	var calendarOpen = false;
	$('#calendar-content').animate({'top':'80px'}, 500);
	$('#btn-calendar').click(function(e) {
		e.preventDefault();
		if(calendarOpen)
		{
			$('#calendar-content').animate({'top':'80px'}, 500);
			$(this).removeClass('active');
			calendarOpen = false;
		}
		else
		{
			$('#calendar-content').animate({'top':'0px'}, 500);
			$(this).addClass('active');
			calendarOpen = true;
		}
	});
	
	/* --------------------------------------------------------- */
	/* SET POP UP IMAGES
	/* --------------------------------------------------------- */
	$('#thumbnails-container a').mouseover(function() { showPopup($(this)); });
	$('#thumbnails-container-webshop a').mouseover(function() { showWebshopPopup($(this)); });
	
	function showPopup( el )
	{
		/* --------------------------------------------------------- */
		/* DESTROY ALL ACTIVE POPUPS
		/* --------------------------------------------------------- */
		$('.popup').each(function() { $(this).fadeOut('fast', function() { $(this).remove(); }); });
		/* ----------------------------------------------------- */
		/* SET NEW POPUP
		/* ----------------------------------------------------- */
		var pos		= el.offset()
		var id		= "popup-"+el.attr('id');
		var item	= el.find('img').attr('alt');
		var type	= el.attr('class');
		var src		= el.find('img').attr('name');
		var tip		= '<div id="' + id + '" class="popup png"><img src="' + src + '" width="225" height="150" /><p class="' + type + '">' + item + '</p></div>';
		$(document.body).append(tip);
		var x = (Math.round(pos.left) + 40) - 118;
		var y = (Math.round(pos.top) + 40) - 80;
		$('#'+id).css({'top':y,'left':x, 'display':'none', 'cursor':'pointer', 'background-image':'none'});
		$('#'+id).fadeIn('fast', function() { $('#'+id).css({'background-image':'url(img/bg-big-pic.png)'}); });
		$('#'+id).mouseout(function() { $('#'+id).css({'background-image':'none'}); $(this).fadeOut('fast', function() { $(this).remove(); });});
		$('#'+id).click(function() { window.location = el.attr('href'); });
	}
	
	function showWebshopPopup( el )
	{
		/* --------------------------------------------------------- */
		/* DESTROY ALL ACTIVE POPUPS
		/* --------------------------------------------------------- */
		$('.popup-wb').each(function() { $(this).fadeOut('fast', function() { $(this).remove(); }); });
		/* ----------------------------------------------------- */
		/* SET NEW POPUP
		/* ----------------------------------------------------- */
		var pos		= el.offset()
		var id		= "popup-" + el.attr('id');
		var item	= el.find('img.data').attr('alt');
		var type	= el.attr('class');
		var src		= el.find('img.data').attr('longdesc');
		var tip		= '<div id="' + id + '" class="popup-wb png"><img src="' + src + '" width="225" height="150" /><p>' + item + '</p></div>';
		$(document.body).append(tip);
		var x = (Math.round(pos.left) + 40) - 118;
		var y = (Math.round(pos.top) + 40) - 80;
		$('#'+id).css({'top':y,'left':x, 'display':'none', 'cursor':'pointer'});
		$('#'+id).fadeIn('fast');
		$('#'+id).mouseout(function() { $(this).fadeOut('fast', function() { $(this).remove(); });});
		$('#'+id).click(function() { window.location = el.attr('href'); });
	}
	
	function hidePopup( el )
	{
		var id  = "popup-"+el.attr('id');
		$('#'+id).fadeOut('fast', function() { $('#'+id).remove(); });
	}
	
	$("div.scrollable").scrollable({size:1});
	
	/* --------------------------------------------------------- */
	/* INFO AANVRAAG EN TELL A FRIEND APPEAR + DISAPPEAR
	/* --------------------------------------------------------- */
	$('#btn-friend').click(function(e) {
		e.preventDefault();
		$('#info-request').hide();
		$('#tell-a-friend').toggle();
	});
	
	$('#btn-info').click(function(e) {
		e.preventDefault();
		$('#tell-a-friend').hide();
		$('#info-request').toggle();
	});
	
	/* --------------------------------------------------------- */
	/* ITEM WRAPPERS
	/* --------------------------------------------------------- */
	$('div.events div.item-wrapper').each(function() {
		$(this).click(function() {
			window.location = $(this).find('a').attr('href');
		});
	});

	/* --------------------------------------------------------- */
	/* CARS INTERACTIVITY
	/* --------------------------------------------------------- */
	$('#cars div.hover').each(function(index) {
		$(this).css({'cursor':'pointer'});
		$(this).click(function() {
			window.location = $('h1 a', $(this)).attr('href');
		});
		$(this).hover(function() {
			$(this).find('div.lbl-info').fadeIn();
		},
		function () {
			$(this).find('div.lbl-info').fadeOut();
		});
	});
	
	
	/* --------------------------------------------------------- */
	/* INIT GMAP SHIT
	/* --------------------------------------------------------- */
	if($('#content.contact').size() > 0) { initialize() }
	
	/* --------------------------------------------------------- */
	/* BOXSHADOWS FOR IE!
	/* --------------------------------------------------------- */
	$("#slideshow div.scrollable div img").center();
	$('div.scrollable div.items div img').boxShadow( 0, 0, 8, '#999');
	
	/* --------------------------------------------------------- */
	/* CYCLE SLIDESHOW
	/* --------------------------------------------------------- */
    $('#video-container').cycle({fx: 'fade', pause: true});
    $('div.slideshow').cycle({fx: 'fade', pause: true});

	/* --------------------------------------------------------- */
	/* EQUAL HEIGHTS
	/* --------------------------------------------------------- */
	$('#bottom-bar div').equalHeights();
	$('#check-out-button').click(function(e) { e.preventDefault(); });
	$('#shop-button').click(function(e) { e.preventDefault(); });
  	$('#edit-button').click(function(e) { e.preventDefault(); });
  	$('#print-button').click(function(e) { e.preventDefault(); window.print(); });
	
	/* --------------------------------------------------------- */
	/* FORM
	/* --------------------------------------------------------- */
    if($('#checkbox-adres').size() > 0) {
        if($('#checkbox-adres').is(':checked')) $('.verzending').attr('disabled', true).addClass('disabled');
    }

	$('#checkbox-adres').click(function () {
		if ($(this).is(':checked')) { 
			$('.verzending').attr('disabled', true).addClass('disabled');
		} else { 
			$('.verzending').attr('disabled', false).removeClass('disabled');
		}
	});
	
	if($('li.btw').size() > 0) {
		$('li.btw input[name=faktuur_btw]').bind('keyup blur', function() { check_btw($(this)); });
		$('#land-select').change(function() {
			check_btw($('li.btw input[name=faktuur_btw]'));
		});
		check_btw($('li.btw input[name=faktuur_btw]'));
	}
	
});
