$(document).ready(function(){
	tabs();
	getWindowHeight();
	customSelect();
});
$(window).resize(function() {
	getWindowHeight();
});

function customSelect(){
        $("select").each(function(){
               $(this).wrap("<div class='selector'></div>");
               // Store the value of title in the variable spanText
               var spanText = $(this).attr('title');
               // Use the variable span text as the default for the select field
               var span = "<span class='select'>" + spanText + "</span>";
               $(this).before(span)
               .fadeTo(1, 0)
               .focus(function(){
                       $(this).parents(".selector").addClass("focus");
               })
               .change(function(){
                       $(this).siblings("span").text($(this).children(":selected").text());
               }).click ( function (){
                       $(this).siblings("span").text($(this).children(":selected").text());   
               }) 
               .blur(function(){
                       $(this).parents(".selector").removeClass("focus");
               });     
        });
        $('select').trigger("change");
}

function getWindowHeight(){
	var nHeight = (document.body.clientHeight) - 149;
	$('#site').css('min-height', nHeight);
}

function setGeoLocation($p_Maptype) {
	if (GBrowserIsCompatible()) {

		
		var map = new GMap(document.getElementById('maps'));
		map.setCenter(new GLatLng(iLatitude, iLongitude));
		map.setZoom(16); 
		map.setMapType($p_Maptype);

		//map.disableContinuousZoom();
		//map.disableDoubleClickZoom();
		
		var tinyIcon = new GIcon();
		tinyIcon.image = 'http://www.webkey6.nl/klant/bezuijen/images/template/logo-maps.png';
		tinyIcon.iconSize = new GSize(65, 65);
		tinyIcon.iconAnchor = new GPoint(30, 65);		
		
		function createMarker(point) {
			markerOptions = { 'icon':tinyIcon };	
          	
            var marker = new GMarker(point, markerOptions);
			
			
			return marker;
        }

		function markertoevoegen(lat, len) {
			geo = new GLatLng(lat,len);
			marker = createMarker(geo);
			map.addOverlay(marker);
		}
  
        markertoevoegen(iLatitude, iLongitude);
   	}
}

function initCheckbox(){
	$('.checkbox_wrapper a.fakecheck').click(function(){
		var sId = $(this).attr('id').replace('fake_','');
		var oThis = $(this);
		if(sId == 'alles'){
			if($(oThis).hasClass('checked')){
				$('input.checken').attr('checked','checked');
			} else {
				$('input.checken').removeAttr('checked');
			}
		} else {
			var oCheckbox = $('input#input_'+sId);
			if($(oThis).hasClass('checked')){
			   oCheckbox.attr('checked','checked');  
			} else {
			   oCheckbox.removeAttr('checked');
			}
		}
	});
}


function initSlider(){
	$('#sliderbutton').click(function(){
		if($(this).hasClass('open')){
			$(this).parent('#slider').animate({
				left: '616px'
			}, 200)
			$(this).removeClass('open');
		} else {
			$(this).parent('#slider').animate({
				left: '97px'
			}, 300)
			$(this).addClass('open');
		}
	});
}

function initThumbPage(){
	$('#thumb_pages').cycle({
		fx:		'scrollHorz',
		speedIn:  400, 
		speedOut: 400,
		timeout: 0,
		before: function() {
			//alert('test');
		},	
		allowPagerClickBubble: true,
		next: '.nextpage',
		prev: '.prevpage'
	});
}

function acitvatePhotoslider(){	
	var nTotalImage = $('.thumb').length;
	var nPerPage = 18;
	var nPages = Math.ceil(nTotalImage / nPerPage);
	var nActivePage = 1;
	
	$('.totaal_fotos').html(nTotalImage);
	
	$('.thumb img').click(function(){
		var aActive = ($('.active').children().attr('class')).split('-');
		var aPhoto = ($(this).attr('class')).split('-');
		$('.thumb').removeClass('active');
		$(this).parent('.thumb').addClass('active');
		$('#sliderbutton').removeClass('open');
		nPage = Math.ceil(aPhoto[1] / nPerPage);
		if(nPage != nActivePage) {
			nActivePage = nPage;
		}
		initPhotoslider(aPhoto[1], aActive[1]);
	});
	$('.volgende').click(function(){
		if(!$(this).hasClass('inactive')) {
			var aActive = ($('.active').children().attr('class')).split('-');
			var nVolgende = parseInt(aActive[1])+1;
			nPage = Math.ceil(nVolgende / nPerPage);
			if(nPage > nActivePage) {
				$('#thumb_pages').cycle('next');
				nActivePage = nPage;	
			}
			initPhotoslider(nVolgende, aActive[1]);
		}
	});
	$('.vorige').click(function(){
		if(!$(this).hasClass('inactive')) {
			var aActive = ($('.active').children().attr('class')).split('-');
			var nVorige = parseInt(aActive[1])-1;
			nPage = Math.ceil(nVorige / nPerPage);
			if(nPage < nActivePage) {
				$('#thumb_pages').cycle('prev');
				nActivePage = nPage;
			}
			initPhotoslider(nVorige, aActive[1]);
		}
	});
}

function initPhotoslider(p_nShow, p_nHide){
	if(p_nShow != p_nHide) {
		$('.foto img:not(.large-'+p_nHide+')').css('z-index', '1');
		$('.large-'+p_nShow).css('z-index', '20');
		$('.large-'+p_nShow).fadeIn('fast');
		$('.large-'+p_nHide).fadeOut('fast');
		$('.thumb').removeClass('active');
		$('.thumb-'+p_nShow).parent('.thumb').addClass('active');
		$('.actieve_fotonummer').html(p_nShow);
		$('#slider').animate({
			left: '616px'
		}, 400)
		initButtons(p_nShow);
	}
}

function initButtons(p_nShow){
	var nTotal = $('.thumb').length;
	if(p_nShow > 1){
		$('.vorige').removeClass('inactive');
	} else {
		$('.vorige').addClass('inactive');
	}
	if(p_nShow < nTotal){
		$('.volgende').removeClass('inactive');
	} else {
		$('.volgende').addClass('inactive');
	}
}


function initGallery() {
	$('#fotos').cycle({ 
		fx:     'scrollHorz', 
		speedIn:  400, 
		speedOut: 400,
		timeout: 0,
		next:   '.volgende', 
		prev:   '.vorige',
		after:	omschrijving
	});
}

function initOverzichtslider(p_nAantal){
	for(nI = 1; nI <= p_nAantal; nI++){
		$('.object_'+nI).cycle({
			fx:     'scrollHorz', 
			speedIn:  400, 
			speedOut: 400,
			timeout: 0,
			next:   '.next_'+nI,
			prev:   '.prev_'+nI
		});
	}
}
	
function omschrijving() { 
	$('#plaatsprijs').html(this.alt); 
}

function tabs() {
	$('.tab').click(function(){
		if(!$(this).hasClass('active')) {
			var nIndex = 50;
			$('.tab').each(function(){
				$(this).removeClass('active').css('z-index',nIndex);
				nIndex--;
			});
			$(this).addClass('active').css('z-index','100');
		}
		
		var sTab = $(this).attr('id');
		$('.tab_content').hide();
		$('#'+sTab+'_content').show();

	});
	
}

function initSelectboxes(){
	$('div.select select').selectbox();
	var zIndex = 9999;
	$('div.form div.entry').each(function(){
		$(this).css('z-index',zIndex);
		zIndex--;
	});
}

//Select met checkboxen
function initCheckselect(){
	$('.multiselect').click(function(){
		$('.selectcheck').slideToggle('fast');
	});
	$('.okay').click(function(){
		$('.selectcheck').slideUp('fast');
	});
	$('.tab').click(function(){
		$('.selectcheck').slideUp('fast');
	});
}

//Selectbox in select activeren
function initSelectboxen(){
	$('a.fakecheck').click(function(){
		$(this).toggleClass('checked');
		if(!$(this).hasClass('checked')){
			$('a.alles').removeClass('checked');	
		}
	});
	$('a.alles').click(function(){
		if($(this).hasClass('checked')){
			$('a.fakecheck').addClass('checked');		
		} else {
			$('a.fakecheck').removeClass('checked');		
		}
	});
}


function validateForm(p_sForm) {

	switch(p_sForm) {
		case 'contactform':
			var rules = {};
			rules[oFields[p_sForm][2]] = {
				required: true,
				notags: true,
				minlength: 2
			};
			rules[oFields[p_sForm][3]] = {
				required: true,
				notags: true,
				telephone: true
			};
			rules[oFields[p_sForm][4]] = {
				required: true,
				email: true
			};
			rules[oFields[p_sForm][6]] = {
				required: true
			};
			
			var messages = {};
			messages[oFields[p_sForm][2]] = {
				required: 'Vul uw naam in.',
				notags: 'U mag geen tags invoeren.',
				minlength: 'Vul minimaal 2 karakters in.'
			};
			messages[oFields[p_sForm][3]] = {
				required: 'Vul uw telefoonnummer in',
				notags: 'U mag geent tags invoeren',
				telephone: 'Vul een geldig telefoonnummer in.'
			};
			messages[oFields[p_sForm][4]] = {
				required: 'Vul een e-mail adres in.',
				email: 'Vul een geldige e-mail adres in.'
			};
			messages[oFields[p_sForm][6]] = {
				required: 'Vul a.u.b. betreft in.'
			};
		break;
		case 'reageer':
			var rules = {};
			rules[oFields[p_sForm][1]] = {
				required: true,
				notags: true,
				minlength: 2
			};
			rules[oFields[p_sForm][2]] = {
				required: true,
				notags: true,
				telephone: true
			};
			rules[oFields[p_sForm][3]] = {
				required: true,
				email: true
			};
			rules[oFields[p_sForm][4]] = {
				required: true,
				minlength: 2
			};
			
			var messages = {};
			messages[oFields[p_sForm][1]] = {
				required: 'Vul uw naam in.',
				notags: 'U mag geen tags invoeren.',
				minlength: 'Vul minimaal 2 karakters in.'
			};
			messages[oFields[p_sForm][2]] = {
				required: 'Vul uw telefoonnummer in',
				notags: 'U mag geent tags invoeren',
				telephone: 'Vul een geldig telefoonnummer in.'
			};
			messages[oFields[p_sForm][3]] = {
				required: 'Vul een e-mail adres in.',
				email: 'Vul een geldige e-mail adres in.'
			};
			messages[oFields[p_sForm][4]] = {
				required: 'Vul een betreft in.',
				email: 'Vul een geldige betreft in.'
			};
		break;
	}
	$('#'+p_sForm).validate({
		rules: rules,
		messages: messages,
		errorPlacement: function(error, element) {
			var obj = element.parent().next('div.icon');
			error.insertAfter(obj);
			obj.attr('class','icon invalid');
		},
		success: function(label) {
			label.prev('div.icon').attr('class','icon valid');
			label.remove();
		},
		submitHandler: function(form) {
			form.submit();
		}
	});	
}

function clearInput() {
	$('.fade').focus(function() {
		$(this).select();
		$(this).prev().fadeTo(150, 0.25);
		$(this).keydown(function() {
			$(this).css('background-repeat', 'repeat');			 
		});
	});
	$('.fade').blur(function() {
		if ( $(this).val() == '' ) {
			$(this).css('background-repeat', 'no-repeat');
			$(this).prev().fadeTo(150, 1);
		}
	});
	$('.fade').each(function(){
		if ( $(this).val() != '') {
			$(this).css('background-repeat', 'repeat');
		};
	});
}

