var map, mozilla, xsl, username, password;


function init(){
	mozilla = window.XMLHttpRequest;
	
	//showMessage("Hi there!","work in progress, check back soon");
	
	
	// Setup google maps control
	map = new GMap2(document.getElementById("map"));
	map.setCenter(new GLatLng(54.8766066541087, -5.8447265625), 5);
	map.setMapType(G_SATELLITE_MAP);
	map.addControl(new GLargeMapControl());
	map.addControl(new GMapTypeControl());
	
	// Set locations div's display so hiding it works first time
	document.getElementById('locations').style.display = 'block';
	
	// Send an XMLHttpRequest for the xsl file
	loadXsl('group');
	toggleGroup(1);
}


function hidelocations(){
	var locations = document.getElementById('locations');
	var bookmarker = document.getElementById('bookmarker');
	if(locations.style.display == 'block'){
		locations.style.display = 'none';
		bookmarker.style.display = 'none';
		document.getElementById('map').style.width = '98%';
		document.getElementById('hider').src = 'images/show.gif';
	}else{
		document.getElementById('map').style.width = '70%';
		locations.style.display = 'block';
		bookmarker.style.display = 'block';
		document.getElementById('hider').src = 'images/hide.gif';
	}
}


function toggleGroup(groupID){
	var obj = (groupID === 1 ? document.getElementById('locations'): document.getElementById('group_'+groupID));
	
	var output, xml;
	if(!obj.getElementsByTagName('ul')[0]){
		var request = {
			method: "GET",
			address: 'getgroup.php?group='+groupID,
			success: function(response){
				if(mozilla){
					var processor = new XSLTProcessor();
					processor.importStylesheet(xsl);
					obj.appendChild(processor.transformToFragment(response, document));
				}else{
					obj.innerHTML += response.transformNode(xsl);
				}
			}
		};
		xmlReq(request);
	}else{
		obj.removeChild(obj.getElementsByTagName('ul')[0]);
	}
	return false;
}


function saveLocation(){
	var lat = map.getCenter().lat();
	var lng = map.getCenter().lng();
	var zoom = map.getZoom();
	var name = document.getElementById('savename').value;
	if(name == ''){
		showMessage("Error","You must enter a name to save this location");
	}else{
		var request = {
			method: "GET",
			address: "addlocation.php?name="+name+"&lat="+lat+"&long="+lng+"&zoom="+zoom,
			success: function(response){
				showMessage("Add location",parseValue(response,'message'));
				document.getElementById('savename').value='';
				toggleGroup(6);
				toggleGroup(6);
			}
		};
		xmlReq(request);
	}
}


function loadXsl(name){
	var request = {
		method: "GET",
		address: name+'.xsl',
		success: function(response){
			xsl = response;
		}
	};
	xmlReq(request);
}


function login(){
	var msgwin = document.createElement('div');
		msgwin.id = 'message';
	var msgtitle = document.createElement('h3');
		msgtitle.textContent = 'Login';
		msgwin.appendChild(msgtitle);
	var msgform = document.createElement('form');
	var msgtext = document.createElement('p');
		msgtext.textContent = 'Please enter your username and password';
		msgform.appendChild(msgtext);
	var userlbl = document.createElement('label');
		userlbl.setAttribute('for','txt_username');
		userlbl.textContent = 'Username:';
		msgform.appendChild(userlbl);
	var username = document.createElement('input');
		username.id = 'txt_username';
		username.setAttribute('type','text');
		msgform.appendChild(username);
	var passlbl = document.createElement('label');
		passlbl.setAttribute('for','txt_password');
		passlbl.textContent = 'Password:';
		msgform.appendChild(passlbl);
	var password = document.createElement('input');
		password.id = 'txt_password';
		password.setAttribute('type','password');
		msgform.appendChild(password);
		msgwin.appendChild(msgform);
	var msglogin = document.createElement('input');
		msglogin.setAttribute('class','btn2');
		msglogin.setAttribute('type','button');
		msglogin.setAttribute('value','Login');
		msglogin.setAttribute('onClick','doLogin()');
		msgwin.appendChild(msglogin);
	var msgclose = document.createElement('input');
		msgclose.setAttribute('class','btn1');
		msgclose.setAttribute('type','button');
		msgclose.setAttribute('value','Cancel');
		msgclose.setAttribute('onClick','closeMsgWin()');
		msgwin.appendChild(msgclose);
	document.getElementById('body').appendChild(msgwin);
}


function doLogin(){
	username = document.getElementById('txt_username').value;
	password = document.getElementById('txt_password').value;
	var request = {
		method: "GET",
		address: "login.php?username="+username+"&password="+password,
		success: function(response){
			include(parseValue(response,'script'));
		}
	};
	xmlReq(request);
	closeMsgWin();
}
	


function include(script_filename) {
    var head = document.getElementsByTagName('head')[0];
    var js = document.createElement('script');
		js.setAttribute('language', 'javascript');
		js.setAttribute('type', 'text/javascript');
		js.setAttribute('src', script_filename);
    head.appendChild(js);
    return false;
}


function parseValue(xml, tag){
	return xml.getElementsByTagName(tag)[0].firstChild.nodeValue;
}


function showMessage(title, msg){
	var msgwin = document.createElement('div');
		msgwin.id = 'message';
	var msgtitle = document.createElement('h3');
		msgtitle.textContent = title;
		msgwin.appendChild(msgtitle);
	var msgtext = document.createElement('p');
		msgtext.textContent = msg;
		msgwin.appendChild(msgtext);
	var msgclose = document.createElement('input');
		msgclose.setAttribute('class','btn1');
		msgclose.setAttribute('type','button');
		msgclose.setAttribute('value','Close');
		msgclose.setAttribute('onClick','closeMsgWin()');
		msgwin.appendChild(msgclose);
	document.getElementById('body').appendChild(msgwin);
}

function closeMsgWin(){
	document.getElementById('body').removeChild(document.getElementById('message'));
}


function getConfirm(confirmwin){
	var result;
	var msgwin = document.createElement('div');
		msgwin.id = 'message';
	var msgtitle = document.createElement('h3');
		msgtitle.textContent = confirmwin.title;
		msgwin.appendChild(msgtitle);
	var msgtext = document.createElement('p');
		msgtext.textContent = confirmwin.message;
		msgwin.appendChild(msgtext);
	var msgyes = document.createElement('input');
		msgyes.setAttribute('class','btn2');
		msgyes.setAttribute('type','button');
		msgyes.setAttribute('value','Yes');
		msgyes.setAttribute('onClick',confirmwin.callback+'('+confirmwin.id+')');
		msgwin.appendChild(msgyes);
	var msgno = document.createElement('input');
		msgno.setAttribute('class','btn1');
		msgno.setAttribute('type','button');
		msgno.setAttribute('value','No');
		msgno.setAttribute('onClick','closeMsgWin()');
		msgwin.appendChild(msgno);
	document.getElementById('body').appendChild(msgwin);
}


function xmlReq(request){
	var conn = mozilla ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
	conn.onreadystatechange = function(){
		if(conn.readyState == 4 && conn.status == 200){
			request.success(conn.responseXML);
		}
	}
	conn.open(request.method, request.address);
	conn.send(null);
}
