var map2;
var icon2;
var myPoint2;
var startMarker2;

var map3;
var icon3;
var myPoint3;
var startMarker3;

function createDivs() {
        
	thisCaption = document.createElement('div');
	thisCaption.setAttribute('id','thisCaption');
	document.body.appendChild(thisCaption);
	var thisText = document.createTextNode('');
	thisCaption.appendChild(thisText);
	thisCaption.style.backgroundColor = '#FFFFFF';
	thisCaption.style.padding = '2px';
	thisCaption.style.visibility = 'hidden';
	thisCaption.style.position = 'absolute';
        
}

function getApp(appNumber) {

	parent.appCallNumber = appNumber;
	parent.getResource(parent.document.getElementById('O2AppsMall'),'O2AppsMall',1)

}

function getUserProfile(userId) {

	parent.showStatusLoading("Getting User Profile");
	var query = new Object();
	query['feature'] = 'O2Apps';
	query['module'] = 'Website';
	query['method'] = 'getProfile';
	query['userId'] = userId;
	query['handler'] = 'showUserProfile';
	query['showDistance'] = 1;
	query['searcher'] = Number(getCookie("S_uid"));
	callWebService(query);

}

function launchChat(sessionId) {

	// send socket
	parent.startChat(sessionId);
	// parent.getResource('','Chat',1);
	parent.createIFrame('http://www.o2apps.com/Chat.cgi?instance='+getCookie('sessionId'),'web');
	
}

function myMouseCoords(e) {
                
	if (window.Event) {
         
		x = e.clientX;
		y = e.clientY;
        
	}
        
	else {
        
		x = window.event.clientX;
		y = window.event.clientY;   
        
	}
                
}

function profileMapLoad() {

	if (GBrowserIsCompatible()) {
	
		// this sets up the small icon to represent the precise location of a listing
		icon2 = new GIcon();
		icon2.image = "images/mapIcon.gif";
		icon2.iconSize = new GSize(16, 16);
		icon2.iconAnchor = new GPoint(8, 8);
		icon2.infoWindowAnchor = new GPoint(5, 1);
	
		map2 = new GMap2(document.getElementById("map2"));
		map2.addControl(new GLargeMapControl());
		map2.addControl(new GMapTypeControl());
		map2.setCenter(new GLatLng(45, -45), 2);
		
	}
	
}

function profileSetMap(lat,lon,firstName,lastName,photo) {

	map2.setCenter(new GLatLng(lat, lon), 5);
	myPoint2 = new GLatLng(lat,lon);
	startMarker2 = new GMarker(myPoint2, icon2)
	map2.addOverlay(startMarker2);
	var thisImage = '';
	if(photo) {
		thisImage = '<br/><img src="profiles/'+photo+'.jpg"/>';
	}
	startMarker2.openInfoWindowHtml("<span style='font-size: 14px; font-weight: bold;'>"+firstName+" "+lastName+"</span>"+thisImage);

}

function redirection(destination) {

	parent.getResource(parent.document.getElementById(destination),destination,1)

}

function showUserProfile(xmlObject) {

	module = 'Google';
	S_createSearchDiv();
	
	var result = getXPath(xmlObject,"//username");
	username = getTextNode(result[0]);
	result = getXPath(xmlObject,"//firstName");
	var firstName = getTextNode(result[0]);
	result = getXPath(xmlObject,"//lastName");
	var lastName = getTextNode(result[0]);
	result = getXPath(xmlObject,"//company");
	var company = getTextNode(result[0]);
	result = getXPath(xmlObject,"//email");
	var email = getTextNode(result[0]);
	result = getXPath(xmlObject,"//password");
	result = getXPath(xmlObject,"//url");
	var url = getTextNode(result[0]);
	result = getXPath(xmlObject,"//alert");
	var alerts = Number(result[0].childNodes[0].nodeValue);

	result = getXPath(xmlObject,"//lat");
	var lat = Number(result[0].childNodes[0].nodeValue);
	result = getXPath(xmlObject,"//lon");
	var lon = Number(result[0].childNodes[0].nodeValue);
	
	result = getXPath(xmlObject,"//distance");
	var distance;
	if(result[0].childNodes[0]) {
		distance  = Number(result[0].childNodes[0].nodeValue);
		distance = Math.round(distance);
	}
	
	result = getXPath(xmlObject,"//photo");
	var photo = Number(result[0].childNodes[0].nodeValue);	
	result = getXPath(xmlObject,"//userProfile");
	var profile;
	
	try {
		profile = decodeURIComponent(result[0].childNodes[0].nodeValue);
	}
	catch(exp) {
		// do nothing
	}
	
	if(!profile) {
	
		try {
			profile = getTextNode(result[0]);
		}
		catch(exp) {
			profile = '';
		}
	
	}
	
	result = getXPath(xmlObject,"//joined");
	var joined = Number(result[0].childNodes[0].nodeValue);
	var joinedShow = new Date(joined*1000);
	
	parent.hideStatusLoading();
	S_setText(firstName+' '+lastName);
	
	var e = document.getElementById('S_searchDisplay');
		
	var	sp = document.createElement('div');
		
	if(photo) {

		if((!lat) && (!lon)) {
			var img = document.createElement('img');
			img.className = 'profileImage';
			img.setAttribute('src','profiles/'+photo+'.jpg');
			sp.appendChild(img);
			e.appendChild(sp);
		}
		
	}
		
	sp = document.createElement('span');
	var v = document.createElement('span');
	v.innerHTML = '<b>Name</b>: '+firstName+' '+lastName+'<br/>';
	v.innerHTML+='<b>Joined</b>: '+joinedShow.toLocaleString()+'<br/>';
	
	if(username) {
		v.innerHTML+='<b>Username</b>: '+username+'<br/>';
	}
	
	if(distance) {
		var km = Math.round(distance*1.609344);
		v.innerHTML+='<b>Distance From Me</b>: '+distance+' miles ('+km+' km)<br/>';
	}
	
	if(company) {
		v.innerHTML+='<b>Company</b>: '+company+'<br/>';
	}
	
	if(url) {
		v.innerHTML+='<b>URL</b>: <a href="javascript:void(0);" onclick="S_internalBrowser(\''+url+'\');">'+url+'</a><br/>';
	}
	
	if(profile) {
		v.innerHTML+='<b>Profile</b>: '+profile+'<p/>';
	}
	
	if((lat) && (lon)) {
	
		v.innerHTML+='<b>Location</b><br/><div id="map2" style="width: 400px; height: 400px;"/>';
		
	
	}
	
	sp.appendChild(v);
	
	e.appendChild(sp);
	
	if((lat) && (lon)) {
		profileMapLoad();
		profileSetMap(lat,lon,firstName,lastName,photo);
		
	}
	
	// published apps
	result = getXPath(xmlObject,"//app");
	
	if(result.length>0) {
	
		var ap = document.createElement('table');
		ap.style.width = '100%';
		
		var tb = document.createElement('tbody');
		var tr = document.createElement('tr');
		var td = document.createElement('td');
		td.style.fontWeight = 'bold';
		td.className = 'appTable';
		td.setAttribute('width','80%');
		td.appendChild(document.createTextNode('App'));
		tr.appendChild(td);
		td = document.createElement('td');
		td.style.fontWeight = 'bold';
		td.className = 'appTable';
		td.setAttribute('width','20%');
		td.appendChild(document.createTextNode('Version'));
		tr.appendChild(td);
		// ap.appendChild(tr);
		tb.appendChild(tr);
	
		for(var i = 0;i<result.length;i++) {
	
			var appNumber = result[i].getAttribute("id");
			var appId = result[i].getAttribute("appId");
			var version = result[i].getAttribute("version");
			var icon = result[i].getAttribute("icon");
			var status = Number(result[i].getAttribute("status"));
			var appName = getTextNode(result[i]);
			
			var tr = document.createElement('tr');
			tr.setAttribute('valign','top');
			var td = document.createElement('td');
			td.className = 'appTable';
			td.setAttribute('width','80%');
			if(icon) {
				var img = document.createElement('img');
				img.className = 'profileImage';
				img.setAttribute('src','apps/'+appNumber+'/'+icon);
				img.style.borderStyle = 'none';
				td.appendChild(img);
			}
			
			var a = document.createElement('a');
			a.setAttribute('href','javascript:void(0);');
			a.appNumber = appNumber;
			a.onclick = function() {
				S_hideSearch();
				getApp(this.appNumber);
			}
			
			a.appendChild(document.createTextNode(appName));
			td.appendChild(a);
			tr.appendChild(td);
			td = document.createElement('td');
			td.className = 'appTable';
			td.setAttribute('width','20%');
			td.appendChild(document.createTextNode(version));
			tr.appendChild(td);		
			// ap.appendChild(tr);
			tb.appendChild(tr);
	
		}
		
		ap.appendChild(tb);
		e.appendChild(ap);

	}


}

function webSearch(device,search) {

	frames.iFrame.module = device
	frames.iFrame.S_createSearchDiv();
	frames.iFrame.S_setText(search);
	frames.iFrame.S_startSearch(search,0,1);
	frames.iFrame.S_fullScreen();

}