/* image player */
var traceEnabled = false;
var playStatus = new Object();
var intStateUpdates = 0;
var changingthumbs = new Array();
var thumbImgs = new Array();

function getUpdate(type,parameter1,parameter2) { 
    //if (++intStateUpdates > 100 || type != 'state') return;
    //trace('imgPlay1', 'Received a '+type+' update from the rotator');
    //trace('imgPlay1', 'Parameters: '+parameter1+' - '+ parameter2);
};

function createImagePlayer(id, container, woningAanbodId) {
	var s1 = new SWFObject("/imagerotate/imagerotator2.swf?v=3",id,"204","150","7");
	s1.addParam('wmode','opaque');
	s1.addVariable("file","/fotos.asp?woningaanbodid=" + woningAanbodId);
	s1.addVariable("enablejs", true);
	s1.addVariable("autostart", false);
	s1.addVariable("width","204");
	s1.addVariable("height","150");
	s1.addVariable("shownavigation", false);
	s1.addVariable("rotatetime", 0.75);
	s1.addVariable("showicons", false); 
	s1.addVariable("transition", "fade");
	s1.addVariable("repeat", true);
	s1.addVariable("shuffle", false);
	s1.addVariable("overstretch", "true");
	s1.addVariable("linkfromdisplay", true);
	s1.write(container);
	playStatus[id] = false;
}

/* maps */
var map;
var myPano;   
var panoClient;
var houseMarker;
var geocoder;
var latLong;

    function initialize() {
      panoClient = new GStreetviewClient();      
      geocoder = new GClientGeocoder();
      geocoder.getLatLng(getWoningAanbodAdres(mMapAdres,mAdres,mPlaats),
	      function(point) {
	      if (!point) {
	        // alert('Adres ' + getWoningAanbodAdres(mMapAdres,mAdres,mPlaats) + " niet gevonden");
	      } else {
			showDiv("subnav_kaart");
	       	latLong = point;
	       	map = new GMap2(document.getElementById("maps"));
		    map.setCenter(point, 15);
		    map.addControl(new GSmallMapControl());
		    map.addControl(new GMapTypeControl());
		 
		    houseMarker = new GMarker(point, {draggable: false});
		    map.addOverlay(houseMarker); 
		      
		    myPano = new GStreetviewPanorama(document.getElementById("streetview"));
		    panoClient.getNearestPanorama(houseMarker.getLatLng(), showPanoData);
		    GEvent.addListener(myPano, "error", handleStreetViewError);
	      }
    	}      
      );
    }
    
    function handleStreetViewError(errorCode) {
    	// alert(errorCode);
    	hideDiv("subnav_streetview");
    }
    
    function showPanoData(panoData) {
      if (panoData.code != 200) {
        // GLog.write('showPanoData: Server rejected with code: ' + panoData.code);
        hideDiv("subnav_streetview");
        return;
      }
      showDiv("subnav_streetview");
      var angle = computeAngle(houseMarker.getLatLng(), panoData.location.latlng);
      myPano.setLocationAndPOV(panoData.location.latlng, {yaw: angle});
    }
 
    function computeAngle(endLatLng, startLatLng) {
      var DEGREE_PER_RADIAN = 57.2957795;
      var RADIAN_PER_DEGREE = 0.017453;
 
      var dlat = endLatLng.lat() - startLatLng.lat();
      var dlng = endLatLng.lng() - startLatLng.lng();
      // We multiply dlng with cos(endLat), since the two points are very closeby,
      // so we assume their cos values are approximately equal.
      var yaw = Math.atan2(dlng * Math.cos(endLatLng.lat() * RADIAN_PER_DEGREE), dlat)
             * DEGREE_PER_RADIAN;
      return wrapAngle(yaw);
   }
 
   function wrapAngle(angle) {
    if (angle >= 360) {
      angle -= 360;
    } else if (angle < 0) {
     angle += 360;
    }
    return angle;
  };

/* maps */

function isPlaying(id) {
	return playStatus[id];
}

function setPlayStatus(id, value) {
	playStatus[id] = value;
}

function stopPlay(id){
	if (document.getElementById(id)) {
		if (typeof document.getElementById(id).sendEvent == 'function'){
			// trace(id, id + ': stopPlay checks play, result  ' + isPlaying(id));
			if (isPlaying(id)) document.getElementById(id).sendEvent('playpause');
			setPlayStatus(id, false);
			// trace(id, id + ': set playstatus to false');
			// trace(id, id + ': playstatus is now ' + isPlaying(id));
			return true;
		}
		// trace(id, id + ': sendEvent is not a function');
		return false;
	}
	// trace(id, id + ': element not found');
	return false;
}

function playPause(id){
	if (document.getElementById(id)) {
		if (typeof document.getElementById(id).sendEvent == 'function'){
			document.getElementById(id).sendEvent('playpause');
			setPlayStatus(id, true);
			// trace(id, id + ': set playstatus to true');
			// trace(id, id + ': playstatus is now ' + isPlaying(id));
			return true;
		}
		// trace(id, id + ': sendEvent is not a function');
		return false;
	}
	// trace(id, id + ': element not found');
	
	return false;
}


function pausePlayer(id, count) {
	if (--count < 0) return;
	if (!stopPlay(id)) setTimeout("pausePlayer('" + id + "', " + count + ");", 100);
}

function createVideoPlayer(id, container, video, previewImage, height, width, autoplay, link) {
	var so = new SWFObject('/mediaplayer/player.swf',id,height,width,'9');
	so.addParam('allowfullscreen','true');
	so.addParam('allowscriptaccess','always');
	so.addParam('wmode','opaque');
	so.addVariable('file',video);
	// so.addVariable('image','/getimage.asp?img=' + previewImage + '&' + new Date().getTime());
	so.addVariable('link',link);
	so.addVariable('linktarget','_self');
	so.addVariable('controlbar','none');
	so.addVariable('displayclick','link');
	so.addVariable('icons','false');
	so.addVariable('autostart',autoplay);
	so.addVariable('stretching','fill');
	so.write(container);
}

function createFlowPlayer(id, container, video, previewImage, height, width, autoplay, link) {
	flowplayer(container,  
	{ 
		src: '/flowplayer/flowplayer.commercial-3.1.5.swf',
    	wmode: 'opaque'
    }, {
    	key: '#$2660952a9d3e551e9d3', 
 		buffering: true,
 		clip: {
    		url: video,
    		autoPlay: 0,
			bufferLength: 1,
			autoBuffering: false,
			linkUrl: link
		},
		play: null,
    	plugins: { 
    		controls: null
    	},	
    	onMouseOver: function() {
			document.getElementById(container).style.cursor='pointer';
			this.play();
		},
		onMouseOut: function() {
			this.stop();
			// this.style.cursor='default';
		}
	});
}

function createYouTubePlayer(id, container, videoId, previewImage, height, width, autoplay, link) {
	var params = { allowScriptAccess: "always"};
    var atts = { id: id, wmode:"transparent"};
    swfobject.embedSWF("http://www.youtube.com/v/" + videoId + "?enablejsapi=1&version=3&playerapiid=" + id, container, height, width, "8", null, null, params, atts);	
}

function onYouTubePlayerReady(playerId) {
	player = document.getElementById(playerId);
	player.playVideo();
}

function playYt(playerId) {
	player = document.getElementById(playerId);
	player.playVideo();
}

function stopYt(playerId) {
	player = document.getElementById(playerId);
	player.pauseVideo();
	player.seekTo(0);
}

function playVideo(id){
	if (document.getElementById(id)) {
		if (typeof document.getElementById(id).sendEvent == 'function'){
			document.getElementById(id).sendEvent('PLAY', true);
			return true;
		}
	}
	return false;
}

function stopVideo(id){
	// $f(id).stopBuffering();
	// $f(id).close();
	if (document.getElementById(id)) {
		if (typeof document.getElementById(id).sendEvent == 'function'){
			document.getElementById(id).sendEvent('STOP');
			return true;
		}
	}
	return false;
}

function hideDiv(id) {
	document.getElementById(id).style.display='none';
	
}

function showDiv(id) {
	document.getElementById(id).style.display='block';	
}

function hideBg(id) {
	document.getElementById(id).className = 'inactive';
}

function showBg(id) {
	document.getElementById(id).className = 'active';
}

function setActivePage(id) {
	document.getElementById(id).className = 'pagingitem pagingitemactive';
}

function unsetActivePage(id, activePage) {
	if (id == "pagingitem" + activePage) return;
	document.getElementById(id).className = 'pagingitem pagingiteminactive';
}

function getHash() {
	if (self.document.location.hash != undefined && self.document.location.hash.length > 0) {
		return self.document.location.hash.substring(1);
	}
	return "";
}

function woningAanbodOnLoad() {
	initMapVars();
	initialize();
	if (mHasVideos) {
		document.getElementById("special_videos").style.display = ( getHash() == ""  || getHash() == "videos" ? "block" : "none" );
		showBg("subnav_videos");
		document.getElementById("special_fotos").style.display = ( getHash() == "fotos" ? "block" : "none" );
	} else {
		document.getElementById("special_fotos").style.display = ( getHash() == ""  || getHash() == "fotos" ? "block" : "none" );
		showBg("subnav_fotos");
	}
	document.getElementById("special_maps").style.display = ( getHash() == "maps" ? "block" : "none" );
	document.getElementById("special_streetview").style.display = ( getHash() == "streetview" ? "block" : "none" );
}

function showWoningAanbodDiv(item) {
	if (item == "videos") {
		hideDiv("special_fotos");
		hideBg("subnav_fotos");
		hideDiv("special_maps");
		hideBg("subnav_kaart");
		hideDiv("special_streetview");
		hideBg("subnav_streetview");
		if (mHasVideos) {
			showDiv("special_videos");
			showBg("subnav_videos");
		}
	}
	if (item == "fotos") {
		if (mHasVideos) {
			hideDiv("special_videos");
			hideBg("subnav_videos");
		}
		hideDiv("special_maps");
		hideBg("subnav_kaart");		
		hideDiv("special_streetview");
		hideBg("subnav_streetview");
		showDiv("special_fotos");
		showBg("subnav_fotos");		
	}
	if (item == "maps") {
		hideDiv("special_fotos");
		hideBg("subnav_fotos");
		hideDiv("special_streetview");
		hideBg("subnav_streetview");
		if (mHasVideos) {
			hideDiv("special_videos");
			hideBg("subnav_videos");
		}
		showDiv("special_maps");
		showBg("subnav_kaart");
		map.checkResize();
		map.setCenter(latLong, 15);
	}
	if (item == "streetview") {
		if (mHasVideos) {
			hideDiv("special_videos");
			hideBg("subnav_videos");
		}
		hideDiv("special_maps");
		hideBg("subnav_kaart");
		hideDiv("special_fotos");
		hideBg("subnav_fotos");
		showDiv("special_streetview");
		showBg("subnav_streetview");
	}
}

function getWoningAanbodAdres(mapadres, adres, plaats) {
	var gMapAdres;
	if (mapadres && mapadres.length > 0) {
		gMapAdres = mapadres;
	} else {
		if (adres && plaats && adres.length > 0 && plaats.length > 0) {
			gMapAdres = adres + ", " + plaats;
		}
	}
	return gMapAdres;
}
function echeck(str) {
	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1){
	   return false
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	   return false
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
	    return false
	}

	 if (str.indexOf(at,(lat+1))!=-1){
	    return false
	 }

	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
	    return false
	 }

	 if (str.indexOf(dot,(lat+2))==-1){
	    return false
	 }
	 
	 if (str.indexOf(" ")!=-1){
	    return false
	 }

	 return true					
}

function ValidateForm(){
	var emailID=document.frmSample.txtEmail
	
	if ((emailID.value==null)||(emailID.value=="")){
		alert("Please Enter your Email ID")
		emailID.focus()
		return false
	}
	if (echeck(emailID.value)==false){
		emailID.value=""
		emailID.focus()
		return false
	}
	return true
 }
 
function validateContactForm(form) {
	if (form.naam.value == '' || (form.telefoon.value == "" && form.email.value == "")) {
		alert('Vul alstublieft een naam en een telefoonnummer (of e-mailadres) in.');
		return false;
	}
	return true;
}

function changeThumb(id, i)
{
	var aThumbs = thumbImgs[id];
	if (changingthumbs[id]) {
		i = i % aThumbs.length;
		document.getElementById(id).src = aThumbs[i];
		i++;
		setTimeout("changeThumb('" + id + "'," + i + ")", 900);
	}
}

function startThumbChange(id)
{
	changingthumbs[id] = true;
	changeThumb(id, 0);
}
 
 
 
function stopThumbChange(id, previewimg)
{
	changingthumbs[id] = false;
	document.getElementById(id).src = previewimg;
}