//Fix avatar on load
function avatarLoaded(){
	document.getElementById("user_avatar").style.background = 'none';	
}

function avatarFailed(){
	document.getElementById("user_avatar").src = 'http://s.twimg.com/a/1252980779/images/default_profile_4_normal.png';	
}

//Confirm deletion of site
function confirmSiteDelete(site_id,site_name){
	var result = confirm("Are you sure you want to delete the site \"" + site_name + "\"?\nAll site statistics will be deleted");
	if(result){
		//Redirect to delete the site
		window.location = "/site_delete/" + site_id;
	}
}

//Set the sites enabled/disabled status
function setSiteEnabled(site_id,enabled){
	var params = "";
	var url = "/ajax/site_enabled." + site_id + "." + enabled + ".htm";
	
	var callback = function(){
		if(xhr.readyState == 4){
			if(enabled == "0"){
				document.getElementById("site_" + site_id + "_enabled_0").style.display = "";
				document.getElementById("site_" + site_id + "_enabled_1").style.display = "none";
			}else if(enabled == "1"){
				document.getElementById("site_" + site_id + "_enabled_0").style.display = "none";
				document.getElementById("site_" + site_id + "_enabled_1").style.display = "";
			}else{
				alert("There was a problem enabling/disabling the site. Please try again.")	
			}
		}
	};
	
	//Send AJAX request
	executeXhr(url,params,callback);
}



//Confirm deletion of site
function confirmUserLoginDelete(login_type_user_id,login_type_id,login_type_name,login_type_username){
	var result = confirm("Are you sure you want to remove the the "+login_type_name+" account \"" + login_type_username + "\"?");
	if(result){
		//Redirect to delete the site
		window.location = "/user_login_remove/" + login_type_user_id + '/' + login_type_id;
	}
}

//Confirm deletion of account
function confirmAccountLoginDelete(account_id,account_name){
	var result = confirm("Are you sure you want to delete the whole account? This cannot be undone!\n\nThis is your last chance to cancel the delete process!\n\nThis means that noone else will be able to access the account and all stats will be deleted!");
	if(result){
		//Redirect to delete the site
		window.location = "/account_delete";
	}
}

//Confirm deletion of site
function confirmMessageDelete(msg_id){
	var result = confirm("Are you sure you want to delete the message?");
	if(result){
		//Redirect to delete the site
		window.location = "/message_delete/" + msg_id;
	}
}




//Open alert types
function openAlertTypes(site_id){
	var innerObj = document.getElementById("inner_" + site_id);
	//var arrowObj = document.getElementById("arrow_"+site_id)
	//if(arrowObj.src.indexOf("closed") >= 0){
	//	arrowObj.src = "/images/icon/open.png";
	//}else{
	//	arrowObj.src = "/images/icon/closed.png";
	//}
	toggleDiv(innerObj,null);
}

//Confirm deletion of site
function openMessage(msg_id){
	var innerObj = document.getElementById("inner_" + msg_id);
	toggleDiv(innerObj,null);
	
	//Mark message as read if needed
	if(document.getElementById("message_status_" + msg_id).innerHTML == 'unread'){
		//Update db and change to unread
		document.getElementById("message_status_" + msg_id).innerHTML = 'read';
		document.getElementById("message_" + msg_id).className = 'message_read';
		
		var params = "";
		var url = "/ajax/message_read/" + msg_id + ".xjpm";
		var callback = function(){
			if(xhr.readyState == 4){
				//alert(xhr.responseText);
			}
		};
		executeXhr(url,params,callback);
	}
}

function updateAlertTypes(site_id){
		
}


function toggleGraphs(type){
	var up = document.getElementById("uptime_graph")
	var down = document.getElementById("downtime_graph")
	var up_button = document.getElementById("uptime_button")
	var down_button = document.getElementById("downtime_button")

	if(type == "up"){
		down.style.display = "none";
		up.style.display = "";
		$(down_button).removeClass('selected');
		$(up_button).addClass('selected');
		
	}else if(type == "down"){
		up.style.display = "none";
		down.style.display = "";
		$(up_button).removeClass('selected');
		$(down_button).addClass('selected');
	}

}



//Show/hide a div
function toggleDiv(obj,show){
	
	if(show){
		$(obj).stop().slideDown(300);
		//Effect.BlindDown(obj.id)
		//Effect.BlindDown(obj, {duration:0.3});	
	}else if(show == false){
		$(obj).stop().slideUp(300);
		//Effect.BlindUp(obj.id)
		//Effect.BlindUp(obj, {duration:0.3})
	}else{
		$(obj).stop().slideToggle();
	}
}

function toggleAuth(obj,show){
	toggleDiv(obj,show);
	if(show == false){
		document.getElementById("auth_username").value = "";
		document.getElementById("auth_password").value = "";	
	}
}

//Show accounts

function showAccounts(){
	var obj = document.getElementById("accounts");
	toggleDiv(obj,true);
}

function hideAccounts(){
	var obj = document.getElementById("accounts");
	toggleDiv(obj,false);
}



//Get invite url
function generateInvite(){
	var params = "";
	var url = "/ajax/generate_invite.htm";
	var callback = function(){
		if(xhr.readyState == 4){
			document.getElementById("invite_url").value = xhr.responseText;
			document.getElementById("loading_animation").style.display = "none";
		}
	};
	//Show loading icon
	document.getElementById("loading_animation").style.display = "";
	
	//Send AJAX request
	executeXhr(url,params,callback);
}


function statsSummaryTime(site_id,year,month,day){
	//Up
	var params = "";
	var up_url = "/site_stats_uptime/" + site_id + "/"+year+"/"+month+"/"+day+".htm";
	var up_callback = function(){
		if(xhr.readyState == 4){
			if(xhr.responseText.length < 30){
				var response_array = xhr.responseText.split("---");
				document.getElementById("downtime").innerHTML = response_array[1];
				document.getElementById("uptime").innerHTML = response_array[0];
				//alert(xhr.responseText);
				statsSummaryResponse(site_id,year,month,day)
			}
		}
	};
	executeXhr(up_url,params,up_callback);
	
}
function statsSummaryResponse(site_id,year,month,day){
	//Response
	var params = "";	
	var response_url = "/site_stats_response_time/" + site_id + "/"+year+"/"+month+"/"+day+".htm";
	var response_callback = function(){
		if(xhr.readyState == 4){
			if(xhr.responseText.length < 30){
				//alert(xhr.responseText);
				document.getElementById("response").innerHTML = xhr.responseText;
			}
		}
	};
	executeXhr(response_url,params,response_callback);
}

function statsLog(site_id,year,month,day){
	document.getElementById("day_stats").innerHTML = '<img src="' + filesUrl + 'images/other/loading.gif" />';
	if(day != '0'){
		document.getElementById("chosen_day").innerHTML = day + "-";
		document.getElementById("chosen_day_2").innerHTML = day + "-";
	}else{
		document.getElementById("chosen_day").innerHTML = "";
		document.getElementById("chosen_day_2").innerHTML = "";
	}
	var params = "";
	var url = "/site_stats_log/" + site_id + "/"+year+"/"+month+"/"+day+".";
	document.getElementById("export_csv").href = url + "csv";
	document.getElementById("export_excel").href = url + "xls";
	var callback = function(){
		if(xhr.readyState == 4){
			if(xhr.responseText.substr(0,6) != '<table' ){
				document.getElementById("day_stats").innerHTML = "Ooops! There was a problem getting your stats!";
			}else{
				document.getElementById("day_stats").innerHTML = xhr.responseText;
			}		}
	};
	executeXhr(url+"html",params,callback);
}

function statsResponseTime(site_id,year,month,day){
	document.getElementById("day_stats").innerHTML = '<img src="' + filesUrl + 'images/other/loading.gif" />';
	if(day != '0'){
		document.getElementById("chosen_day").innerHTML = day + "-";
		document.getElementById("chosen_day_2").innerHTML = day + "-";
	}else{
		document.getElementById("chosen_day").innerHTML = "";
		document.getElementById("chosen_day_2").innerHTML = "";
	}
	var params = "";
	var url = "/site_stats_response_time/" + site_id + "/"+year+"/"+month+"/"+day+".";
	document.getElementById("export_csv").href = url + "csv";
	document.getElementById("export_excel").href = url + "xls";
	//window.location = url;
	var callback = function(){
		if(xhr.readyState == 4){
			if(xhr.responseText.substr(0,4) != '<div' ){
				document.getElementById("day_stats").innerHTML = "Ooops! There was a problem getting your stats!";
			}else{
				document.getElementById("day_stats").innerHTML = xhr.responseText;
			}
		}
	};
	executeXhr(url+"html",params,callback);
}

function statsUptime(site_id,year,month,day){
	document.getElementById("day_stats").innerHTML = '<img src="' + filesUrl + 'images/other/loading.gif" />';
	var params = "";
	var url = "/site_stats_uptime/" + site_id + "/"+year+"/"+month+"/0.";
	document.getElementById("export_csv").href = url + "csv";
	document.getElementById("export_excel").href = url + "xls";
	//window.location = url;
	var callback = function(){
		if(xhr.readyState == 4){
			if(xhr.responseText.substr(0,4) != '<div' ){
				document.getElementById("day_stats").innerHTML = "Ooops! There was a problem getting your stats!";
			}else{
				document.getElementById("day_stats").innerHTML = xhr.responseText;
			}
		}
	};
	executeXhr(url+"html",params,callback);
}



//Facebook
function createFacebookLogin(){
	//<fb:login-button size="large" length="long" onlogin='facebookLogin()'>
	FB.init("716492ccd5674d8fba951a85399bb9d2", "/pages/facebook/xd_receiver.htm");
	//FB.Connect.logout(function(){});
	/*var newElement = document.createElement('fb:login-button');
	newElement.setAttribute('size', 'medium');
	newElement.setAttribute('v', '2');
	newElement.setAttribute('onlogin', 'facebookLogin()');
	newElement.innerHTML = "Sign in with Facebook";*/
	//document.getElementById("fbConnect").appendChild(newElement);
	document.getElementById("fbConnect").innerHTML = '<a href="javascript:return false;" title="Sign in with Facebook" onclick="FB.Connect.requireSession(function(){facebookLogin()}); return false;"><img id="fb_login_image" src="' + filesUrl + 'images/logins/facebook.png" alt="Sign in with Facebook" alt="Sign in with OpenID" width="164" height="25" /></a>';
	
	
	
	//Logout
}
function facebookLogin(){
	var session = FB.Facebook.apiClient.get_session();
	window.location="/logincallback/facebook/"+session.uid
}


//Ajax stuff
var xhr
function executeXhr(url,params,callback){
    var xmlHttpReq = false;

	if(window.XMLHttpRequest){
        xhr = new XMLHttpRequest();
    }else if(window.ActiveXObject){
        xhr = new ActiveXObject("Microsoft.XMLHTTP");
    }
	
    xhr.open('POST', url, true);
    xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    xhr.onreadystatechange = callback;
    xhr.send(escape(params));
}



//Map stuff
var mapIcon
var map
//Load the map into the DIV provided
function MapLoad(){
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("map"));
		map.setCenter(new GLatLng(42.931761862112516,1.0546875), 1);
		var largeMapControl = new GLargeMapControl();
		var largeMapControlPosition = new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(10,10));
		map.addControl(largeMapControl, largeMapControlPosition);
        
		var largeMapTypeControl = new GMapTypeControl();
		var largeMapTypeControlPosition = new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(10,10));		
		map.addControl(largeMapTypeControl, largeMapTypeControlPosition);
	
        // Create a base icon for all of our markers that specifies the
        //mapIcon = new GIcon();
        //mapIcon.image = "/images/icon/monitor_32.png"
		//mapIcon.shadow = "";
        //mapIcon.iconSize = new GSize(32, 32);
        //mapIcon.shadowSize = new GSize(0, 0);
        //mapIcon.iconAnchor = new GPoint(16, 16);
        //mapIcon.infoWindowAnchor = new GPoint(30, 2);
	}
}

function AddNewPoint(lat,long,id,name,description){
	// Set up our GMarkerOptions object
	markerOptions = { }//icon:mapIcon };
    var marker = new GMarker(new GLatLng(lat,long), markerOptions);
	var windowHtml = "<div class=\"map_window\"><h2 style=\"margin-top:0px;font-size:18px;\">" + name + "</h2><p style=\"margin-bottom:0px;\">" + description + "</p></div>";
	//Add and event listener
	GEvent.addListener(marker, "click", function() {marker.openInfoWindowHtml(windowHtml);});

	map.addOverlay(marker);
}

function MapLoaded(){
	MapLoad();
	document.getElementById("map").style.display = "";
	document.getElementById("mapLoading").style.display = "none";
	MapLoad();
	ReadRSS('georss.xml?rand=' + Math.floor(Math.random()*11))
}








