var myPano;

function initializeStreet() {
	var panoOpts = {
	  features: {
		streetView: true,
		userPhotos: true
	  },
	  userPhotoOptions: {
		photoRepositories: [ 'panoramio', 'picasa']
	  }
	};

	var locatie = new GLatLng(iLatitude, iLongitude);
	panoramaOptions = { latlng:locatie };
	myPano = new GStreetviewPanorama(document.getElementById("earth"), panoramaOptions);
	GEvent.addListener(myPano, "error", handleNoFlash);
}

function handleNoFlash(errorCode) {
	$('#earth').hide();
	if (errorCode == FLASH_UNAVAILABLE) {
		alert("Error: Flash doesn't appear to be supported by your browser");
		return;
	}
}  


function setGeoLocation() {
	if (GBrowserIsCompatible()) {
		
		var map = new GMap(document.getElementById('maps'));
		map.setCenter(new GLatLng(iLatitude, iLongitude));
		map.setZoom(16); 
		
		map.addControl(new GSmallMapControl());

		var tinyIcon = new GIcon();
		tinyIcon.image = 'http://www.webkey6.nl/klant/jcvastgoed/images/template/maps-logo.png';
		tinyIcon.iconSize = new GSize(73, 45);
		tinyIcon.iconAnchor = new GPoint(45, 30);		
		
		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 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 showSub(){
	$('#menu li').hover(function() {
		if($(this).children('.submenu').length > 0) {
			$(this).children('.submenu').show();
		}
	}, function() {
		if($(this).children('.submenu').length > 0) {
			$(this).children('.submenu').hide();
		}
	});
}

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

function validateForm(p_sForm) {
	var iconOK = {
		'background-position' : '0px -32px'
	}
	var iconError = {
		'background-position' : '0px 0px'
	}
	
	var rules = {};
	rules[aFields[0]] = {
		required: true
	};
	rules[aFields[1]] = {
		required: true
	};
	rules[aFields[3]] = {
		required: true,
		postalcode: true
	};
	rules[aFields[4]] = {
		required: true,
		digits: true
	};
	rules[aFields[6]] = {
		required: true,
		email: true
	};
	rules[aFields[8]] = {
		required: true,
		number: true
	};
	rules[aFields[9]] = {
		required: true,
		nonumber: true
	};
	rules[aFields[10]] = {
		required: true,
		notags: true,
		minlength: 5
	};
	rules[aFields[11]] = {
		required: true,
		email: true
	};
	
	var messages = {};
	messages[aFields[0]] = {
		required: 'Vul uw naam in.'
	};
	messages[aFields[1]] = {
		required: 'Vul uw achternaam in.'
	};
	messages[aFields[3]] = {
		required: 'Vul een postcode in',
		postalcode: 'Vul een geldige postcode in.'
	};
	messages[aFields[4]] = {
		required: 'Vul uw telefoonnummer in',
		digits: 'Vul een geldig telefoonnummer in.'
	};
	messages[aFields[6]] = {
		required: 'Vul uw e-mailadres in.',
		email: 'Vul een geldig e-mailadres in.'
	};
	messages[aFields[8]] = {
		required: 'Vul geen getallen in',
		number: 'Dit veld mag alleen getallen bevatten'
	};
	messages[aFields[9]] = {
		required: 'Vul geen getallen in',
		nonumber: 'Dit veld mag geen getallen bevatten'
	};
	messages[aFields[10]] = {
		required: 'Vul iets in',
		notags: 'Er zijn geen tags toegestaan',
		minlength: 'Lengte minimaal 5 karakters'
	};
	messages[aFields[11]] = {
		required: 'Vul een e-mailadres in',
		email: 'Vul een geldig e-mailadres in'
	};
	$('#'+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.trigger('submit');
		}
	});	
}

function initSlider(){
	$('#sliderbutton').click(function(){
		if($(this).hasClass('open')){
			$(this).parent('#slider').animate({
				left: '858px'
			}, 200)
			$(this).removeClass('open');
		} else {
			$(this).parent('#slider').animate({
				left: '80px'
			}, 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 = ($('.thumb.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 = ($('.thumb.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 = ($('.thumb.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: '858px'
		}, 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
		});
	}
}

