window.addEvent('domready', function(){
	/*if(!Cookie.read('language')){
		setTimeout('doLanguagePicker()', 2000);
	}*/
});
function doLanguagePicker(){
	cover=new Element('DIV');
	cover.setStyles({
		width:'100%',
		height:'100%',
		backgroundColor:'#00311c',
		opacity:0,
		position:'fixed',
		left:0,
		top:0
	})
	cover.injectInside(document.body);
	new Fx.Tween(cover).start('opacity', .8);
	lang=new Element('DIV');
	lang.setStyles({
		width:500,
		height:340,
		marginLeft:-250,
		marginTop:-170,
		position:'fixed',
		left:'50%',
		top:'50%',
		backgroundImage:'url(static/images/framework/language-panel.png)',
		opacity:0
	});
	new Fx.Tween(lang).start('opacity', 1);
	lang.injectInside(document.body);
	lang.addEvent('click', function(){
		Cookie.write('language', 'set');
		window.location.reload();
	});
}

window.addEvent('load', function(){

	// CHECK THE PAGE AND LOAD IN GOOGLE MAPS API IF NECESSARY
	if($('google-map-holder-list')){
		sMaps=new Element('SCRIPT');
		sMaps.set('type', 'text/javascript');
		sMaps.set('src', 'http://maps.google.com/maps/api/js?sensor=false&callback=initMapForListPage');
		sMaps.injectInside(document.head);
	}
	if($('google-map-holder-location')){
		sMaps=new Element('SCRIPT');
		sMaps.set('type', 'text/javascript');
		sMaps.set('src', 'http://maps.google.com/maps/api/js?sensor=false&callback=loadTownForLocationPage');
		sMaps.injectInside(document.head);
	}

	if($('rolling-container')){
		$('rolling-container').setStyles({
			height:'100%'
		});
		allImages=$$('img.rolling');
		allImages.each(function(el){
			el.setStyles({
				position:'absolute',
				left:0,
				top:0
			});
			el.fader=new Fx.Morph(el, {duration:2000, onComplete:function(){
				doFade.delay(5000);
			}});
		});
		doFade.delay(5000);
	}
	
});



function doFade(){
	allImages=$$('img.rolling');
	if(allImages[allImages.length-1].getStyle('opacity')=='0'){
		allImages[allImages.length-1].dispose().inject('rolling-container', 'top').setStyles({
			opacity: 1
		});
		allImages=$$('img.rolling');
	}
	allImages[allImages.length-1].fader.start({
		opacity:0
	})
}






// #################   FUNCTIONS FOR PROPERTIES LIST PAGE   #######################
// Create map centered on Newtown and place it into the holder
function initMapForListPage(){
	loaderBox=new Element('DIV');
	loaderBox.set('html', '<img style="vertical-align:middle; padding-right:5px" src="/static/images/framework/loader.gif"> Please wait while the properties load');
	loaderBox.setStyles({
		backgroundColor:'#FFFFFF',
		padding:10,
		position:'absolute',
		top:150,
		left:270,
		border:'solid 1px #000',
		fontFamily:'arial, helvetica, sans-serif',
		fontWeight:'bold',
		opacity:.7
	});
	loaderBox.injectAfter($('google-map-holder-list'));
	
	var myLatlng = new google.maps.LatLng(52.5132, -3.3141);
	var myOptions = {
		zoom: 8,
		center: myLatlng,
		mapTypeId: google.maps.MapTypeId.ROADMAP
	}
	map = new google.maps.Map($('google-map-holder-list'), myOptions);
	//Create geocoder instace
	gc=new google.maps.Geocoder();
	markers=[];
	propertiesCounter=0;
	//start geocoding each property
	geocodePropertyForListPage(propertiesCounter);
}

function geocodePropertyForListPage(pCounter){
	//console.log(pCounter);
	//Create an address from the property details
	var gcOpts={
		address:properties[pCounter].location+', '+properties[pCounter].region,
		region:'GB'
	}
	//Geocode the address and send th result to addToMarkers
	//console.log(properties[pCounter].location+', '+properties[pCounter].region);
	gc.geocode(gcOpts, addToMarkersForListPage);
}

function addToMarkersForListPage(response){
	//console.log(response);
	//Add a marker to the list
	if(response){
		var marker={};
		//Create latlng object from geocode response
		marker.latLng=response[0].geometry.location; //new google.maps.LatLng(response[0].geometry.location.lat(),response[0].geometry.location.lng());
		//add details to marker object
		marker.details=properties[propertiesCounter];
		//add marker to list
		markers.push(marker);
		plotMarkersForListPage(markers.length-1);
		propertiesCounter++;
	}
	//check if all the markers are loaded and coded, if so plot them, if not then code the next one
	//console.log(propertiesCounter+' of '+properties.length+' loaded');
	if(propertiesCounter<properties.length-1){
		setTimeout('geocodePropertyForListPage('+propertiesCounter+')', 300);
	} else {
		finishLoadingList();
	}
}
function plotMarkersForListPage(gI){
	//for(gI=0; gI<markers.length; gI++){
		pDetails="<div class=\"main-content\"><h2>"+markers[gI].details.location+"</h2>";
		pDetails+=markers[gI].details.content+'<br/>';
		pDetails+="<a href=\""+markers[gI].details.link+"\">More information</a></div>";
		//Dirty workaround to get functions tied to markers...sorry about using eval()
		if(markers[gI].details.region=='Powys'){
			icon=ROOT+'/static/images/framework/map/powys.png';
		} else if(markers[gI].details.region=='Ceredigion'){
			icon=ROOT+'/static/images/framework/map/ceredigion.png';
		} else {
			icon=ROOT+'/static/images/framework/map/shropshire.png';
		}
		//document.body.innerHTML=pDetails;
		eval("pIcon_"+gI+"=new google.maps.MarkerImage(icon,new google.maps.Size(41, 60),new google.maps.Point(0, 0),new google.maps.Point(20, 53));");
		eval("pMarker_"+gI+" = new google.maps.Marker({position:markers[gI].latLng,map:map,title:markers[gI].details.location,clickable:true, icon:pIcon_"+gI+"});");
		eval("infoWindow_"+gI+"=new google.maps.InfoWindow({content: '"+pDetails+"'});");
		eval("goToLocation_"+gI+"=function(){window.location.href=markers["+gI+"].details.link;}");
		eval("showLocation_"+gI+"=function(){infoWindow_"+gI+".open(map, pMarker_"+gI+");}");
		eval("google.maps.event.addListener(pMarker_"+gI+", 'click', function(event){showLocation_"+gI+"();});");
	//}
}

function finishLoadingList(){
	loaderBox.dispose();
}

// #################   FUNCTIONS FOR PROPERTIES LOCATION PAGE   #######################
// Create map centered on Newtown and place it into the holder
function loadTownForLocationPage(){
	var gcOpts={
		address:lMarker.location+', '+lMarker.region,
		region:'GB'
	}
	//Geocode the address and send th result to addToMarkers
	gc=new google.maps.Geocoder();
	gc.geocode(gcOpts, initMapForLocationPage);
}
function initMapForLocationPage(response){
	var myLatLng = response[0].geometry.location; //new google.maps.LatLng(response[0].geometry.location.b,response[0].geometry.location.c);
	var myOptions = {
		zoom: 14,
		center: myLatLng,
		mapTypeId: google.maps.MapTypeId.ROADMAP
	}
	map = new google.maps.Map($('google-map-holder-location'), myOptions);
	townMarker=new google.maps.Marker({position:myLatLng, map:map, title:PARAMS[1]});
	markers=[];
	streetCount=0;
	createMarkerForLocationPage(streets[streetCount]);
}
function createMarkerForLocationPage(street){
	var gcOpts={
		address:street.name,
		region:'GB'
	}
	//console.log(street.name);
	gc.geocode(gcOpts, addToMarkersForLocationPage);
}
function addToMarkersForLocationPage(response){
	//console.log(response);
	for(var i=0; i<markers.length; i++){
		if(markers[i].b==response[0].geometry.location.b && markers[i].c==response[0].geometry.location.c){
			//console.log('match');
			response[0].geometry.location.b-=0.00001;
			response[0].geometry.location.c+=0.00001;
		}
	}
	markers.push({b:response[0].geometry.location.b,c:response[0].geometry.location.c});

	var latLng=response[0].geometry.location;//new google.maps.LatLng(response[0].geometry.location.b,response[0].geometry.location.c);
	var image = new google.maps.MarkerImage(ROOT+'/static/images/framework/map/'+streets[streetCount].type+'.png',
		new google.maps.Size(23, 23),
		new google.maps.Point(0, 0),
		new google.maps.Point(12, 12)
	);
  var marker = new google.maps.Marker({
        position: latLng,
        map: map,
        icon: image,
		title: streets[streetCount].name
    });
	streetCount++;
	if(streetCount<streets.length){
		createMarkerForLocationPage(streets[streetCount]);
	}
}


