addEvent(window, "load", makeNiceTitles);
addEvent(window, "load", makeIntroTitles);

var showNiceTitleLinks = false;									//cpo added parameter to show/hide urls under title text

var XHTMLNS = "http://www.w3.org/1999/xhtml";
var CURRENT_NICE_TITLE;
var CURRENT_INTRO_TITLE;
var CURRENT_INTRO_NUMBER = 0;


var browser = new Browser();


// code for nice title like mouse overs on areas of intro graphic

function makeIntroTitles() {
	//alert('makeIntroTitles');
	
	var image = document.getElementById('pointer_div');


	//alert('almost add events');
	if(image){
		//alert('add events');
		addEvent(image,"mousedown",joinTgl);

		addEvent(image,"mousemove",showIntroTitle);
		addEvent(image,"mouseover",showIntroTitle);
		addEvent(image,"onClick",joinTgl);
//		 addEvent(image,"mouseout",hideIntroTitle);
//		 addEvent(image,"focus",showIntroTitle);
//		 addEvent(image,"blur",hideIntroTitle);
		 
	}
}

function joinTgl(e) {
	pos_x = e.offsetX?(e.offsetX):e.pageX-document.getElementById("pointer_div").offsetLeft;
	pos_y = e.offsetY?(e.offsetY):e.pageY-document.getElementById("pointer_div").offsetTop;



	//alert('Coords'+pos_x+' '+pos_y);


	// BIG HEART JOIN
/*
	if (pos_x>542 && pos_x<648 && pos_y>94 && pos_y<206){
			document.body.style.cursor =  "wait";
		window.location.href = '/manage/traderooms/exchange';
	}
	if (pos_x>433 && pos_x<531 && pos_y>86 && pos_y<166){
			document.body.style.cursor =  "wait";
		window.location.href = '/manage/swapshops/exchange';
	}
	if (pos_x>422 && pos_x<565 && pos_y>212 && pos_y<293){
			document.body.style.cursor =  "wait";
		window.location.href = '/manage/listings/exchange';
	}
*/
	if (pos_x>340 && pos_x<680 && pos_y>55 && pos_y<225){						// shopping exchange
			document.body.style.cursor =  "wait";
			window.location.href = '/manage/traderooms/exchange';
	}


	if (pos_x>422 && pos_x<594 && pos_y>203 && pos_y<337){					// personal quarter
			document.body.style.cursor =  "wait";
			window.location.href = '/manage/users/exchange';
	}

	if (pos_x>35 && pos_x<320 && pos_y>65 && pos_y<249){							// Skills Exchange
			document.body.style.cursor =  "wait";
			window.location.href = '/manage/courses/exchange';
	}


	if (pos_x>71 && pos_x<302 && pos_y>264 && pos_y<390){					// media quarter
			document.body.style.cursor =  "wait";
			window.location.href = '/manage/libraries/exchange';
	}



	if (pos_x>279 && pos_x<406 && pos_y>171 && pos_y<254){				// bank
			document.body.style.cursor =  "wait";
			window.location.href = '/bank/banks/statement';
	}



	if (pos_x>357 && pos_x<463 && pos_y>303 && pos_y<359){				// messages
			document.body.style.cursor =  "wait";
			window.location.href = '/manage/users/exchange';
	}

/*
	if (pos_x>49 && pos_x<191 && pos_y>408 && pos_y<563){
			document.body.style.cursor =  "wait";
		window.location.href = '/manage/users/exchange';
	}


	if (pos_x>350 && pos_x<640 && pos_y>368 && pos_y<563){
			document.body.style.cursor =  "wait";
		window.location.href = '/manage/libraries/exchange';
	}
	if (pos_x>262 && pos_x<418 && pos_y>26 && pos_y<141){
			document.body.style.cursor =  "wait";
		window.location.href = '/bank/banks/statement';
	}
		

	if (pos_x>51 && pos_x<225 && pos_y>54 && pos_y<181){				// Courses
			document.body.style.cursor =  "wait";
		window.location.href = '/manage/courses/quarter';
	}
	if (pos_x>125 && pos_x<247 && pos_y>258 && pos_y<353){				// ideas
			document.body.style.cursor =  "wait";
		window.location.href = '/manage/ideas/exchange';
	}
	if (pos_x>239 && pos_x<351 && pos_y>139 && pos_y<249){				// projects
			document.body.style.cursor =  "wait";
		window.location.href = '/manage/projects/exchange';
	}
	if (pos_x>21 && pos_x<109 && pos_y>212 && pos_y<345){				// timebank
			document.body.style.cursor =  "wait";
		window.location.href = '/manage/timebanks/exchange';
	}
		
*/	

















}






function showIntroTitle(e) {
	//if(CURRENT_JOIN_LOAD ==1){
	//	return;
	//}
	//alert('showIntroTitle');
//	if (CURRENT_INTRO_TITLE) hideIntroTitle(CURRENT_INTRO_TITLE);
	var image = document.getElementById('pointer_div');
	//alert('almost add events');
	if(!image){
		return;
	}
	
	pos_x = e.offsetX?(e.offsetX):e.pageX-document.getElementById("pointer_div").offsetLeft;
	pos_y = e.offsetY?(e.offsetY):e.pageY-document.getElementById("pointer_div").offsetTop;
	//alert("x"+ pos_x +"y "+ pos_y);
	
	
	
	
	
	nicetitle = 'this is the not shown';
	cx=0; cxy=0;
	var introNumber=0;
	

	
	option1='';
	option2='';

	var xoptions = new Array();
	
	// courses
	if (pos_x>0 && pos_x<340 && pos_y>0 && pos_y<280){
		cx =10; cy=5;
		nicetitle = 'SKILLS';
		introNumber=1;
	}
	// add/edit
	if (pos_x>340 && pos_x<680 && pos_y>0 && pos_y<280){
		cx = 355; cy=5;
		nicetitle = "SPEND";
		option1	=	' -Shop';
		option2	=	' -Sell';


		introNumber=2;
	}
	
	// myTGL
	if (pos_x>0 && pos_x<340 && pos_y>280 && pos_y<560){
		cx = 20; cy=310;
		nicetitle = 'PERSONAL';
		introNumber=3;
	}
	
	// Messages
	if (pos_x>340 && pos_x<680 && pos_y>280 && pos_y<560){
		cx = 355; cy=310;
		nicetitle = 'MEDIA';
		introNumber=4;
	}
	
	
	/*
	// BIG HEART JOIN
	if (pos_x>395 && pos_x<495 && pos_y>235 && pos_y<308){
		cx = 395; cy=235;
		nicetitle = 'Join tgl.tv';
		introNumber=14;
		document.body.style.cursor =  "pointer";

	}else{
		
		document.body.style.cursor = "default";
	}
	*/
	
	document.body.style.cursor = "default";
	// BIG HEART JOIN
/*
	if (pos_x>542 && pos_x<648 && pos_y>94 && pos_y<206){
		document.body.style.cursor =  "pointer";
	}
	if (pos_x>433 && pos_x<531 && pos_y>86 && pos_y<166){
		document.body.style.cursor =  "pointer";
	}
	if (pos_x>422 && pos_x<565 && pos_y>212 && pos_y<293){
		document.body.style.cursor =  "pointer";
	}
	if (pos_x>188 && pos_x<343 && pos_y>368 && pos_y<460){
		document.body.style.cursor =  "pointer";
	}
	
	if (pos_x>49 && pos_x<191 && pos_y>408 && pos_y<563){
		document.body.style.cursor =  "pointer";
	}


	if (pos_x>350 && pos_x<640 && pos_y>368 && pos_y<563){				// Libraries Media
		document.body.style.cursor =  "pointer";
	}
	if (pos_x>262 && pos_x<418 && pos_y>26 && pos_y<141){				// L Bank
		document.body.style.cursor =  "pointer";
	}


	if (pos_x>51 && pos_x<225 && pos_y>54 && pos_y<181){				// Courses
		document.body.style.cursor =  "pointer";
	}
	if (pos_x>125 && pos_x<247 && pos_y>258 && pos_y<353){				// ideas
		document.body.style.cursor =  "pointer";
	}
	if (pos_x>239 && pos_x<351 && pos_y>139 && pos_y<249){				// projects
		document.body.style.cursor =  "pointer";
	}
	if (pos_x>21 && pos_x<109 && pos_y>212 && pos_y<345){				// timebank
		document.body.style.cursor =  "pointer";
	}


	*/


	if (pos_x>340 && pos_x<680 && pos_y>55 && pos_y<225){						// shopping exchange
		document.body.style.cursor =  "pointer";
	}


	if (pos_x>422 && pos_x<594 && pos_y>203 && pos_y<337){					// personal quarter
		document.body.style.cursor =  "pointer";
	}

	if (pos_x>35 && pos_x<320 && pos_y>65 && pos_y<249){							// Skills Exchange
		document.body.style.cursor =  "pointer";
	}


	if (pos_x>71 && pos_x<302 && pos_y>264 && pos_y<390){					// media quarter
		document.body.style.cursor =  "pointer";
	}



	if (pos_x>279 && pos_x<406 && pos_y>171 && pos_y<254){				// bank
		document.body.style.cursor =  "pointer";
	}



	if (pos_x>357 && pos_x<463 && pos_y>303 && pos_y<359){				// messages
		document.body.style.cursor =  "pointer";
	}






	
	
	
	
	if(introNumber==0){
		return;
	}
	if (CURRENT_INTRO_NUMBER != introNumber){
		hideIntroTitle(CURRENT_INTRO_TITLE);
		CURRENT_INTRO_NUMBER = introNumber;
	}else{
		return;
	}

	cx += document.getElementById("pointer_div").offsetLeft;
	cy += document.getElementById("pointer_div").offsetTop;
	
	
/*	
	var d = document.createElementNS(XHTMLNS,"div");
	d.className = "nicetitle";
	tnt = document.createTextNode(nicetitle);
	pat = document.createElementNS(XHTMLNS,"p");
	pat.className = "titletext";
	pat.appendChild(tnt);
	pat.appendChild(document.createElement("br"));
	pat.appendChild(document.createTextNode('    '+option1));
	pat.appendChild(document.createElement("br"));
	pat.appendChild(document.createTextNode('    '+option2));

//this works for click events-	pat.setAttribute("onClick", "alert(nicetitle+'  do search')"); 
*/




/*

  // -  create the form element and it's attributes
  var f=document.createElement('form');
  f.setAttribute('name','myForm');
//  f.setAttribute('action','createElement_test.html');
//  f.setAttribute('action','search/channels/all?location=worldwide&search_filter=&popularsearches=0&q=fred');
  f.setAttribute('action','search/channels/all');


  f.setAttribute('target','_blank');
  f.setAttribute('method','get');
  
  var formText=document.createTextNode('Search for: ');

  // -  next, create a text input where you'll enter your name
  var yourName=document.createElement('input');
  yourName.setAttribute('type','text');
  yourName.setAttribute('name','q');
  yourName.setAttribute('value','');
  yourName.setAttribute('style','width:150px;');

	// tgl specific elements
  var param1=document.createElement('input');
  param1.setAttribute('type','hidden');
  param1.setAttribute('name','location');
  param1.setAttribute('value','worldwide');

	
  // -  create a submit input to submit the form
  var b=document.createElement('input');
  b.setAttribute('type','submit');
  b.setAttribute('value','click to search');
  b.setAttribute('style','width:150px;');

  // -  finally, append the text node, the text input, and the submit input to the form 
  f.appendChild(formText);
  // -  put a line break between the text node and the inputs
  f.appendChild(document.createElement('br'));
  f.appendChild(yourName);
	// tgl elements
  f.appendChild(param1);
	

  f.appendChild(b);
//  document.getElementById('formTest').appendChild(f);
  pat.appendChild(f);



*/














/*


	d.appendChild(pat);
	
	STD_WIDTH = 300;
	h = nicetitle.length;
	if (nicetitle.length) {
		t = nicetitle.length;
	}
	h_pixels = h*6; t_pixels = t*10;
	
	if (h_pixels > STD_WIDTH) {
		w = h_pixels;
	} else if ((STD_WIDTH>t_pixels) && (t_pixels>h_pixels)) {
		w = t_pixels;
	} else if ((STD_WIDTH>t_pixels) && (h_pixels>t_pixels)) {
		w = h_pixels;
	} else {
		w = STD_WIDTH;
	}
	
	
*/	
	
	if (introNumber==14){
		
		
	}else{
		w=250;
	}
	d.style.width = w + 'px';    
	
	// sort the screen postion out
	mx = cx;
	my = cy;
	//xy = getMousePosition(e);
	//mx = xy[0]; my = xy[1];
	
	//	d.style.left = (mx+15) + 'px';
	d.style.left = (mx+15) + 'px';
	d.style.top = (my+35) + 'px';
	if (window.innerWidth && ((mx+w) > window.innerWidth)) {
		d.style.left = (window.innerWidth - w - 25) + "px";
	}
	if (document.body.scrollWidth && ((mx+w) > document.body.scrollWidth)) {
		d.style.left = (document.body.scrollWidth - w - 25) + "px";
	}
	
	document.getElementsByTagName("body")[0].appendChild(d);

	CURRENT_INTRO_TITLE = d;
}



function hideIntroTitle(e) {
	if (!document.getElementsByTagName) return;
	if (CURRENT_INTRO_TITLE) {
		document.getElementsByTagName("body")[0].removeChild(CURRENT_INTRO_TITLE);
		CURRENT_INTRO_TITLE = null;
	}
}


//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//
// original nice title follows
//

function makeNiceTitles() {
//	alert('makeNiceTitles');
    if (!document.createElement || !document.getElementsByTagName) return;
    // add namespace methods to HTML DOM; this makes the script work in both
    // HTML and XML contexts.
    if(!document.createElementNS)
    {
        document.createElementNS = function(ns,elt) {
            return document.createElement(elt);
        }
    }

    if( !document.links )
    {
        document.links = document.getElementsByTagName("a");
    }
    for (var ti=0;ti<document.links.length;ti++) {
        var lnk = document.links[ti];
        if (lnk.title) {
            lnk.setAttribute("nicetitle",lnk.title);
            lnk.removeAttribute("title");
            addEvent(lnk,"mouseover",showNiceTitle);
            addEvent(lnk,"mouseout",hideNiceTitle);
            addEvent(lnk,"focus",showNiceTitle);
            addEvent(lnk,"blur",hideNiceTitle);
        }
    }
    var instags = document.getElementsByTagName("ins");
    if (instags) {
    for (var ti=0;ti<instags.length;ti++) {
        var instag = instags[ti];
        if (instag.dateTime) {
            var strDate = instag.dateTime;
            var dtIns = new Date(strDate.substring(0,4),parseInt(strDate.substring(4,6)-1),strDate.substring(6,8),strDate.substring(9,11),strDate.substring(11,13),strDate.substring(13,15));
            instag.setAttribute("nicetitle","Added on "+dtIns.toString());
            addEvent(instag,"mouseover",showNiceTitle);
            addEvent(instag,"mouseout",hideNiceTitle);
            addEvent(instag,"focus",showNiceTitle);
            addEvent(instag,"blur",hideNiceTitle);
        }
    }
    }
}

function findPosition( oLink ) {
  if( oLink.offsetParent ) {
    for( var posX = 0, posY = 0; oLink.offsetParent; oLink = oLink.offsetParent ) {
      posX += oLink.offsetLeft;
      posY += oLink.offsetTop;
    }
    return [ posX, posY ];
  } else {
    return [ oLink.x, oLink.y ];
  }
}



function showNiceTitle(e) {
    if (CURRENT_NICE_TITLE) hideNiceTitle(CURRENT_NICE_TITLE);
    if (!document.getElementsByTagName) return;
    if (window.event && window.event.srcElement) {
        lnk = window.event.srcElement
    } else if (e && e.target) {
        lnk = e.target
    }
    if (!lnk) return;
    if (lnk.nodeName.toUpperCase() != 'A') {
        // lnk is not actually the link -- ascend parents until we hit a link
        lnk = getParent(lnk,"A");
    }
    if (!lnk) return;
    nicetitle = lnk.getAttribute("nicetitle");document.createTextNode
    
    var d = document.createElementNS(XHTMLNS,"div");
    d.className = "nicetitle";
    tnt = document.createTextNode(nicetitle);
    pat = document.createElementNS(XHTMLNS,"p");
    pat.className = "titletext";
    pat.appendChild(tnt);
    d.appendChild(pat);
	
    if (lnk.href && showNiceTitleLinks) {
        tnd = document.createTextNode(lnk.href);
        pad = document.createElementNS(XHTMLNS,"p");
        pad.className = "destination";
        pad.appendChild(tnd);
        d.appendChild(pad);
    }
    
    STD_WIDTH = 300;
    if (lnk.href) {
        h = lnk.href.length;
    } else { h = nicetitle.length; }
    if (nicetitle.length) {
      t = nicetitle.length;
    }
    h_pixels = h*6; t_pixels = t*10;
    
    if (h_pixels > STD_WIDTH) {
        w = h_pixels;
    } else if ((STD_WIDTH>t_pixels) && (t_pixels>h_pixels)) {
        w = t_pixels;
    } else if ((STD_WIDTH>t_pixels) && (h_pixels>t_pixels)) {
        w = h_pixels;
    } else {
        w = STD_WIDTH;
    }
        
    d.style.width = w + 'px';    

    /*
    mx = lnk.offsetLeft;
    my = lnk.offsetTop;
    */
    mpos = findPosition(lnk);
    mx = mpos[0];
    my = mpos[1];
    //xy = getMousePosition(e);
    //mx = xy[0]; my = xy[1];
    
//	d.style.left = (mx+15) + 'px';
		d.style.left = (mx+15) + 'px';
    d.style.top = (my+35) + 'px';
    if (window.innerWidth && ((mx+w) > window.innerWidth)) {
        d.style.left = (window.innerWidth - w - 25) + "px";
    }
    if (document.body.scrollWidth && ((mx+w) > document.body.scrollWidth)) {
        d.style.left = (document.body.scrollWidth - w - 25) + "px";
    }
    
    document.getElementsByTagName("body")[0].appendChild(d);
    
    CURRENT_NICE_TITLE = d;
}

function hideNiceTitle(e) {
    if (!document.getElementsByTagName) return;
    if (CURRENT_NICE_TITLE) {
        document.getElementsByTagName("body")[0].removeChild(CURRENT_NICE_TITLE);
        CURRENT_NICE_TITLE = null;
    }
}

// Add an eventListener to browsers that can do it somehow.
// Originally by the amazing Scott Andrew.
function addEvent(obj, evType, fn){
  if (obj.addEventListener){
    obj.addEventListener(evType, fn, false);
    return true;
  } else if (obj.attachEvent){
	var r = obj.attachEvent("on"+evType, fn);
    return r;
  } else {
	return false;
  }
}

function getParent(el, pTagName) {
	if (el == null) return null;
	else if (el.nodeType == 1 && el.tagName.toLowerCase() == pTagName.toLowerCase())	// Gecko bug, supposed to be uppercase
		return el;
	else
		return getParent(el.parentNode, pTagName);
}

function getMousePosition(event) {
  if (browser.isIE) {
    x = window.event.clientX + document.documentElement.scrollLeft
      + document.body.scrollLeft;
    y = window.event.clientY + document.documentElement.scrollTop
      + document.body.scrollTop;
  }
  if (browser.isNS) {
    x = event.clientX + window.scrollX;
    y = event.clientY + window.scrollY;
  }
  return [x,y];
}

// Determine browser and version.

function Browser() {
// blah, browser detect, but mouse-position stuff doesn't work any other way
  var ua, s, i;

  this.isIE    = false;
  this.isNS    = false;
  this.version = null;

  ua = navigator.userAgent;

  s = "MSIE";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isIE = true;
    this.version = parseFloat(ua.substr(i + s.length));
    return;
  }

  s = "Netscape6/";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isNS = true;
    this.version = parseFloat(ua.substr(i + s.length));
    return;
  }

  // Treat any other "Gecko" browser as NS 6.1.

  s = "Gecko";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isNS = true;
    this.version = 6.1;
    return;
  }
}


