var map = null;	//global map variable
var member_array = new Array();	//array for displaying members location 
var post_array=new Array();	//array of displaying posts

//latest updates
var latest_viewed_M=0;
var latest_viewed_B=0;
var latest_viewed_N=0;
var get_latest_updates_url="";
var interval_id=null;
var isPhotoVis=false;
var photoOverlay=null;

	
//base_icon_picture
var base_icon = new GIcon();
/*base_icon.shadow = "http://www.google.com/mapfiles/shadow50.png";
base_icon.iconSize =  new GSize(35, 35);
base_icon.shadowSize = new GSize(57, 54);
base_icon.iconAnchor = new GPoint(10, 34);*/
base_icon.shadow = "/chillr/pictures/markers/entry-marker-shadow.png";
base_icon.iconSize = new GSize(53, 71);
base_icon.shadowSize = new GSize(105, 72);
base_icon.iconAnchor = new GPoint(18, 48);	
base_icon.infoWindowAnchor = new GPoint(35, 2);
base_icon.infoShadowAnchor = new GPoint(18, 25);
base_icon.dragCrossAnchor = new GPoint(-1, 9);	// guess work - no documentation to date



function draw_member(point, icon_url,index,array) {	
	var icon = new GIcon(base_icon);
	//icon.image = "photos/u" + uid + "/j" + jid + "/mkr" + (index+1) + ".png";
	icon.image = icon_url;
	var marker = new GMarker(point, icon);

	// show entry info on mouse over
	//GEvent.addListener(marker, "mouseover", function () {
	//	showEntryInfo(index);
	//});
	// add onclick listener
	GEvent.addListener(marker, "click", function () {
		marker.openInfoWindowHtml(array[index]['info_html']);
	});
		
	array[index]['marker'] = marker;
	map.addOverlay(marker);
}	
// open up the info window of an entry
function open_marker(index,array) {
	array[index]['marker'].openInfoWindowHtml(array[index]['info_html']);
}

function show_photo(index) {
	//big phto
}

function show_blog(index){
	//draw_member(post_array[".$i."]['location'],'".$queryA['icon']."',".$i.",post_array);
}

function show_members(member_array){		
	var code="<table class='info_table'>";
	//create all of the cells rows
	for(var i=0;i<member_array.length;i++){
		code += "<tr onclick='open_marker(" + i + ", member_array)' class='" + "cell_info_table" + "'><td class='cell_member' align='center' valign='bottom'><img src='" + member_array[i]['m_icon'] + "' alt='Member image' class='member_image'/>" +
		"<br/>"+member_array[i]['m_nick_name']+"</td>" +
		"<td class='"+ "cell_info_table"+"'>" +
			"<div class='bubbleWrapper'>" +
				"<div class='bubbleTop'></div>" +
				"<div class='bubbleMiddle'>" +
					"<b>Location: </b>" + member_array[i]['l_name'] + "<br/>" +
					"<b>Moto: </b>" + member_array[i]['mo_text'] +
				"</div>" +
				"<div class='bubbleBottom'>" +
					"<div style='float: right; margin-top: 14px; color: black; font-size:x-small; font-style: italic;'>" + member_array[i]['cm_updated_at'] + "</div>" +
				"</div>" +
			"</div>" +
			"</td>" +
		"</tr>";
		//"<span style='font-size: smaller; font-style: italic;'>
		//Latitude: "+ member_array[i]['l_latitude'] +"<br/>"+
		//"Longitude: "+ member_array[i]['l_longitude']+ "</span>	
	}
	code += "</table>";
	document.getElementById("info_table_wraper").innerHTML = code;		
}

function show_posts(post_array){		
	var code="<table  class='info_table'>";
	//create all of the cells rows
	for(var i=0;i<post_array.length;i++){
		code += "<tr onclick='open_marker(" + i + ", post_array)' class='" + "cell_info_table" + "'><td class='cell_member' align='center' valign='bottom'><img src='" + post_array[i]['m_icon'] + "' alt='Member image' class='member_image'/>" +
		"<br/>"+post_array[i]['m_nick_name']+"</td>" +
		"<td class='"+ "cell_info_table"+"'>" +
			"<div class='bubbleWrapper'>" +
				"<div class='bubbleTop'></div>" +
				"<div class='bubbleMiddle'>" +
					"<b>" + post_array[i]['p_title'] + "</b><br/>" +
					post_array[i]['p_text'] +
				"</div>" +
				"<div class='bubbleBottom'>" +
					"<div style='float: right; margin-top: 14px; color: black; font-size:x-small; font-style: italic;'>" + post_array[i]['p_time'] + "</div>" +
				"</div>" +
			"</div>" +
			"</td>" +
		"</tr>";		
				
		/*code=code+"<tr onclick='open_marker("+i+",post_array)' class='"+ "cell_info_table"+"'><td align='center' class='cell_member'  rowspan='2' valign='bottom'><img src='"+post_array[i]['m_icon']+"' alt='Member image' class='member_image'/>"+
		"<br/><span style='font-size:x-small;'>"+post_array[i]['m_nick_name']+"<span/></td><td class='"+cell_class+"'>"+
		"<b>"+ post_array[i]['p_title']+ "</b><br/>"+
		post_array[i]['p_text']+ "</td></tr>"
		+ 	"<tr><td valign='top' align='right' colspan='2'><span style='font-size:x-small; font-style: italic;'>"+ post_array[i]['p_time']+"</span></td></tr>";	*/
	}
	code+="</table>";
	document.getElementById("info_table_wraper").innerHTML = code;		
}

function initialize_map(){
	map = new GMap2(document.getElementById("map_canvas"));
	map.addControl(new GSmallMapControl());
	map.addControl(new GMapTypeControl());
	map.addControl(new GScaleControl());
	map.enableDoubleClickZoom();
	map.enableScrollWheelZoom();
	map.setCenter(new GLatLng(0, 0), 0);
	map.setMapType(G_NORMAL_MAP);
}

//get filename without extension
function get_file_name(file_path){
	var start=file_path.lastIndexOf("/")+1;					
	var file_name =	file_path.substr(start, file_path.length-start-4);
	return file_name;
}

function initialize_index_view(url,session_viewed_M,session_viewed_B,session_viewed_N){
	latest_viewed_M=session_viewed_M;
	latest_viewed_B=session_viewed_B;
	latest_viewed_N=session_viewed_N;
	get_latest_updates_url=url;
	get_latest_updates(url,session_viewed_M,session_viewed_B,session_viewed_N);
	//alert("url: "+url);
	if(interval_id!=null)
		clearInterval(interval_id);
	interval_id=setInterval("update_navigation()",30000);
}

function update_navigation(){
	get_latest_updates(get_latest_updates_url,latest_viewed_M,latest_viewed_B,latest_viewed_N);
}

function get_latest_updates(url,latest_viewed_M,latest_viewed_B,latest_viewed_N){	
	GDownloadUrl(url, doit);					
	function doit (doc){//get the query resaults array
		var new_content=false;
		//alert("doc: "+doc);
		var json_data = eval('(' + doc + ')'); 	
		
   		if(latest_viewed_M<json_data.array[0].latest_updated_M){//we have new content
   			new_content=true;
   			show_div("map_image_new");
   			hide_div("map_image");
   		}else{
   			hide_div("map_image_new");
   			show_div("map_image");
   		}
   		if(latest_viewed_B<json_data.array[0].latest_updated_B){
   			new_content=true;
   			show_div("blog_image_new");
   			hide_div("blog_image");
   		}else{
   			hide_div("blog_image_new");
   			show_div("blog_image");
   		}
   		if(latest_viewed_N<json_data.array[0].latest_updated_N){  
   			new_content=true;
   			show_div("note_image_new");
   			hide_div("note_image");   				
   		}else{
   			hide_div("note_image_new");
   			show_div("note_image");   			
   		}
   		
   		//set the background on the update
   		if(new_content){
			document.getElementById("main_navigation_wraper").className ="main_navigation_wraper_blue";
				//document.getElementById("main_navigation_wraper").style.background="black";				
   		}
		else
			document.getElementById("main_navigation_wraper").className ="main_navigation_wraper";
	} 			
}
	
function load(view,url) {
	if (GBrowserIsCompatible()) {	
			
		// view window bounds
		var bounds = new GLatLngBounds();	
		GDownloadUrl(url, onload);
						
		onload=function(doc){//get the query resaults array
			initialize_map();				
			var json_data = eval('(' + doc + ')');  
			
			if(view==0){//location_view	
				/*base_icon.shadow = "/chillr/pictures/markers/entry-marker-shadow.png";
				base_icon.iconSize = new GSize(53, 71);
				base_icon.shadowSize = new GSize(105, 72);
				base_icon.iconAnchor = new GPoint(18, 48);	*/	
				for(var i=0; i<json_data.array.length; i++) {
					var point=new GLatLng(json_data.array[i].l_latitude,json_data.array[i].l_longitude);
					bounds.extend(point);
					draw_member(point,json_data.array[i].m_marker,i,json_data.array); 	
				}				
				member_array=json_data.array;							
				document.getElementById("info_title").innerHTML = "Members on the map";
				show_members(member_array);
							
			}else if(view==1){//latest posts view
				for(var i=0; i<json_data.array.length; i++) {
					var point=new GLatLng(json_data.array[i].p_latitude,json_data.array[i].p_longitude);
					bounds.extend(point);
					draw_member(point,json_data.array[i].m_marker,i,json_data.array); 	
				}	
				post_array=json_data.array;
				document.getElementById("info_title").innerHTML = "<b>Latest blogs</b>";
				show_posts(post_array);					
			}else if(view==2){//One persons blog view
				//define base marker
				/*base_icon.shadow = "/chillr/pictures/markers/entry-marker-shadow.png";
				base_icon.iconSize = new GSize(53, 71);
				base_icon.shadowSize = new GSize(105, 72);
				base_icon.iconAnchor = new GPoint(18, 48);*/
				if(json_data.array.length!=0){		
					for(var i=0; i<json_data.array.length; i++) {
						var point=new GLatLng(json_data.array[i].p_latitude,json_data.array[i].p_longitude);
						//parse picture_destination to create marker path					
						var marker_path ="/chillr/pictures/mkr/mkr-"+ 
								get_file_name(json_data.array[i].p_picture)+".png";					
						bounds.extend(point);
						draw_member(point,marker_path,i,json_data.array); 	
					}
					post_array=json_data.array;
					document.getElementById("info_title").innerHTML = "<b>"+post_array[0].m_nick_name+"'s blogs: </b>";
					show_posts(post_array);
				}
			}			
									
			//fit viewport to bounds
			map.setZoom(map.getBoundsZoomLevel(bounds)-1, new GSize(200, 200));
			map.setCenter(bounds.getCenter());
			map.savePosition();				
		}			
	}
}

/*INDEX PAGE*/
//var member_windows=new Array('member_window_0','member_window_1');
var member_windows=new Array();
var next_member_window=0;
function hide_all(member_windows){
	//loop through the array and hide each element by id
	for (var i=0;i<member_windows.length;i++){
		hide_div(member_windows[i]);
	}	
}

function hide_div(id){
	if (document.getElementById) { // DOM3 = IE5, NS6
		document.getElementById(id).style.display = 'none';
	}	
}

function show_div(id){	
	//safe function to show an element with a specified id		  
	if (document.getElementById) { // DOM3 = IE5, NS6
		document.getElementById(id).style.display = 'block';
	}	
}

function show_next(){
	next_member_window++;
	if(next_member_window>=member_windows.length){
		next_member_window=0;
	}	
	hide_all(member_windows);
	show_div("member_window_"+next_member_window);
}

function show_previous(){
	next_member_window--;
	if(next_member_window<0){
		next_member_window=member_windows.length-1;	
	}
	hide_all(member_windows);
	show_div("member_window_"+next_member_window);	
}

/*function checkTheBox(i){
	alert("checkbox Clicked: "+i);
	/*if(document.getElementById("checkbox_"+i).checked==0)
		document.getElementById("checkbox_"+i).checked=1;
	else
		document.getElementById("checkbox_"+i).checked=0;
}*/

/*function initialize_index_view(web){	
	GDownloadUrl("/chillr/db_query.php?view=3", onload);
	
	onload=function(doc){//get the query resaults array
		var json_data = eval('(' + doc + ')'); 	
		var html="";	
		for(var i=0; i<json_data.array.length; i++) {//for all data
			var class_member="member_right";
			if(i%2==0){
				html=html+'<div class="member_window" id="member_window_'+i/2+'">';
				class_member="member_left";
				member_windows.push("member_window_"+i/2);
			}
			
			html+='<a href="/chillr/map_view.php?view=2&username='+json_data.array[i].username+'&web='+web+'">';
			html+='<div class="'+class_member+' member" align="center"><img src="'+json_data.array[i].m_icon+'" alt="Member image" class="member_image"/>';
			html+='<br/>'+json_data.array[i].m_nick_name+'</div></a>';							
			if(i%2==1 || i==json_data.array.length-1){
				html=html+'</div>';				
			}
		}
		html+='<img onclick="show_previous()" src="/chillr/images/arrow_left_50.png" alt="Left arrow" class="arrow arrow_left"/>';
		html+='<img onclick="show_next()" src="/chillr/images/arrow_right_50.png" alt="Right arrow"  class="arrow arrow_right"/>';
							
		document.getElementById("main_navigation_people").innerHTML =html;
		next_member_window=0;
		hideAll(member_windows)
		show_div("member_window_0");
	}
}*/

function validateRegistration(){
	var ok=true;
	document.getElementById("error_password").innerHTML="";
	document.getElementById("error_email").innerHTML="";
	if (/*document.getElementById("first_name").value=="" || document.getElementById("last_name").value=="" ||*/ document.getElementById("nick_name").value=="" || document.getElementById("e-mail").value=="" || document.getElementById("password").value=="" || document.getElementById("retyped_password").value==""){
		alert("All fields need to be field!");
		ok=false;		
	}	
	if(document.getElementById("password").value!=document.getElementById("retyped_password").value){
		document.getElementById("error_password").innerHTML="Passwords do not mach! ";
		ok=false;
		
	}	
	if(document.getElementById("password").value.length<6){
		document.getElementById("error_password").innerHTML="Provided password is to short. Minimum length is 6 characters. ";
		ok=false;		
	}	
	if(echeck(document.getElementById("e-mail").value)==false){
		document.getElementById("error_email").innerHTML="Provided e-mail is not valid. ";
		ok=false;		
	}	
	return ok;
}



/**
 * DHTML email validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */

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 validateEmail(id){
	var ok=true;
	if(echeck(document.getElementById(id).value)==false){
		alert("Provided e-mail is not valid.");
		ok=false;		
	}
	return ok;
}	

function validate_c_name(id){
	var ok=true;
	if(document.getElementById(id).value==""){
		alert("Set the chillr name and click Register Chillr.");
		ok=false;		
	}
	return ok;
}

function validate_search(id){
	var ok=true;
	if(document.getElementById(id).value==""){
		alert("Set a search string.");
		ok=false;		
	}
	return ok;
}
	
/*Will code*/	
	
function showFullPhoto(index) {
	//var num = index + 1;
	if(!isPhotoVis) {
		isPhotoVis = true;
		photoOverlay = document.createElement("div");
		photoOverlay.setAttribute("id", "photo_overlay");
		/*var transOverlay = document.createElement("div");
		transOverlay.setAttribute("id", "trans_overlay");*/
		/*transOverlay.innerHTML = "<div style='width: 100%; height: 100%'></div>";*/
		//photoOverlay.innerHTML = "<img src='/chillr/pictures/full_size/photo_p.coulton@lancaster.ac.uk_1217591390.jpg' alt='photo' style='width:auto; height:452px; margin:0 0 0 0; padding: 8px; background: #fff; border: 1px solid #000'/>";
		photoOverlay.innerHTML = "<img src='"+post_array[index]['p_picture']+"' alt='photo' style='width:auto; height:452px; margin:0 0 0 0; padding: 8px; background: #fff; border: 1px solid #000'/>";	
		
		//photoOverlay.innerHTML = "<img src='photos/u4/j32/img2.jpeg' alt='photo' style='padding: 8px; background: #fff; border: 1px solid #000' width='640' height='480' /><p style='padding: 8px; background: #fff; width: 256px; margin: 8px auto; border: 1px solid #000'>Click anywhere to close the photo.</p>";
		//document.getElementById('wrapper').appendChild(transOverlay);
		document.getElementById('full_photo_wrapper').appendChild(photoOverlay);
		//document.getElementById('wrapper').style.zindex = '999';
		
		// get rid of the blighter
		photoOverlay.onclick = function () {
			isPhotoVis = false;
			document.getElementById('full_photo_wrapper').removeChild(photoOverlay);
			//document.getElementById('wrapper').removeChild(transOverlay);
			//document.getElementById('wrapper').style.zindex = '-1';
		}
		
		//overlayDiv.style.visibility = "visible";
		//
	}
}

function showFullPhotoUrl(url) {
	//var num = index + 1;
	if(!isPhotoVis) {
		isPhotoVis = true;
		var photoOverlay = document.createElement("div");
		photoOverlay.setAttribute("id", "photo_overlay");
		var transOverlay = document.createElement("div");
		transOverlay.setAttribute("id", "trans_overlay");
		/*transOverlay.innerHTML = "<div style='width: 100%; height: 100%'></div>";*/
		//photoOverlay.innerHTML = "<img src='/chillr/pictures/full_size/photo_p.coulton@lancaster.ac.uk_1217591390.jpg' alt='photo' style='width:auto; height:452px; margin:0 0 0 0; padding: 8px; background: #fff; border: 1px solid #000'/>";
		photoOverlay.innerHTML = "<img src='"+url+"' alt='photo' style='width:auto; height:452px; margin:0 0 0 0; padding: 8px; background: #fff; border: 1px solid #000'/>";	
		
		//photoOverlay.innerHTML = "<img src='photos/u4/j32/img2.jpeg' alt='photo' style='padding: 8px; background: #fff; border: 1px solid #000' width='640' height='480' /><p style='padding: 8px; background: #fff; width: 256px; margin: 8px auto; border: 1px solid #000'>Click anywhere to close the photo.</p>";
		//document.getElementById('wrapper').appendChild(transOverlay);
		document.getElementById('full_photo_wrapper').appendChild(photoOverlay);
		//document.getElementById('wrapper').style.zindex = '999';
		
		// get rid of the blighter
		photoOverlay.onclick = function () {
			isPhotoVis = false;
			document.getElementById('full_photo_wrapper').removeChild(photoOverlay);
			photoOverlay=null;
			//document.getElementById('wrapper').removeChild(transOverlay);
			//document.getElementById('wrapper').style.zindex = '-1';
		}
		
		//overlayDiv.style.visibility = "visible";
		//
	}
}

