/*
Standard JavaScript Features
-----------------------------
Copyright 2007 Kira Nebilak
kira@kiranebilak.com Ver 1.1
*/

var curzone = 0;

function stdSwitch(imagesrc, o, n) {
	imagesrc.src = String(imagesrc.src).replace(o, n);
}

function stdOver(imagesrc) {
	stdSwitch(imagesrc, '_off', '_on');
}

function stdOut(imagesrc) {
	stdSwitch(imagesrc, '_on', '_off');
}

function stdPreload(imagesrc) {
	var imagePreload;
	imagePreload = new Image ();
	imagePreload.src = imagesrc;
}

function stdReset(fieldsrc,newtext) {
	if (fieldsrc.value == '') {
	fieldsrc.value = newtext;
	}
}

function xmlhttpPost(strURL) {
    var xmlHttpReq = false;
    var self = this;
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    self.xmlHttpReq.open('POST', strURL, true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.onreadystatechange = function() {
        if (self.xmlHttpReq.readyState == 4) {
            updatepage(self.xmlHttpReq.responseText);
        }
    }
    self.xmlHttpReq.send(getquerystring());
	return false;
}

function errorreport() {
    var xmlHttpReq = false;
    var self = this;
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
	
    self.xmlHttpReq.open('POST', 'http://quests.aionblog.net/resources/ext_errorReport.php', true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	
    self.xmlHttpReq.onreadystatechange = function() {
        if (self.xmlHttpReq.readyState == 4) {
            updatepageerror(self.xmlHttpReq.responseText);
        } else {
			document.getElementById('errorReportContainer').innerHTML = "<div class='ele full highlighted border-bottom' style='height: 18px; padding-top: 11px;' id='errorreport'><strong>Your report is being submitted...</strong></div>";	
		}
    }
    self.xmlHttpReq.send(getquerystringerror());
	return false;
}

function getquerystring() {
    var word = document.getElementById('searchstr').value;
    qstr = 's=' + escape(word);  // NOTE: no '?' before querystring
    return qstr;
}

function updatepage(str){
    document.getElementById('results').innerHTML = str;
}

function updatepageerror(str){
    document.getElementById('errorReportContainer').innerHTML = str;
}

function getquerystringerror() {
    var type = document.getElementById('ERtype').value;
	var id = document.getElementById('ERid').value;
	var msg = document.getElementById('ERerror').value;
    qstr = 'ertype=' + escape(type);  // NOTE: no '?' before querystring
	qstr = qstr +'&erid=' + escape(id);
	qstr = qstr +'&ererror=' + escape(msg);
    return qstr;
}


function loadNPCLocs(strURL,qs) {
	updateLocs('Loading Data...');
    var xmlHttpReq = false;
    var self = this;
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    self.xmlHttpReq.open('POST', strURL, true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.onreadystatechange = function() {
        if (self.xmlHttpReq.readyState == 4) {
            updateLocs(self.xmlHttpReq.responseText);
        }
    }
    self.xmlHttpReq.send(qs);
	return false;
}
function updateLocs(str){
    document.getElementById('npcLocs').innerHTML = str;
}


var locList = new Array();

function addLoc(idnpc) {
	locList.push(idnpc);
	cleanseList(0);
	loadNPCLocs('http://quests.aionblog.net/resources/loadLocations.php','n=' + locList.join('.') + '&z='+curzone);
}

function remLoc(idnpc) {
	removeItems(locList, idnpc);
	loadNPCLocs('http://quests.aionblog.net/resources/loadLocations.php','n=' + locList.join('.') + '&z='+curzone);
}

function debug_addLoc(idnpc) {
	locList.push(idnpc);
	cleanseList(0);
	alert('n=' & locList.join('.') & '&z='&curzone);
	loadNPCLocs('http://quests.aionblog.net/resources/loadLocations.php','n=' & locList.join('.') & '&z='&curzone);
}

function toggleNPC(idnpc) {
	
		c = document.getElementById('npclink'+idnpc).firstChild.src;
		if (c=="http://quests.aionblog.net/images/plus.jpg") {
			if (locList.length<5) {
				document.getElementById('npclink'+idnpc).firstChild.src = "../../../images/minus.jpg";
				document.getElementById('npclink'+idnpc).className = 'npclink npcselected';
				document.getElementById('npclinkout'+idnpc).className = 'npcselected';
				
				addLoc(idnpc);
			}
		} else {
			document.getElementById('npclink'+idnpc).firstChild.src = "../../../images/plus.jpg";
			document.getElementById('npclink'+idnpc).className = 'npclink';
			document.getElementById('npclinkout'+idnpc).className = '';
			
			remLoc(idnpc);
			
		}
	
	
}

function cleanseList(idnpc) {
	if (idnpc == 0) {
		locList = uniqueArr(locList);
	} else {
		cleanseList(0);
		locList = removeItems(locList, idnpc);
		loadNPCLocs('http://quests.aionblog.net/resources/loadLocations.php','n=' & locList.join('.') & '&z='&curzone);
	}
}

function uniqueArr(a) {
 temp = new Array();
 for(i=0;i<a.length;i++){
  if(!contains(temp, a[i])){
   temp.length+=1;
   temp[temp.length-1]=a[i];
  }
 }
 return temp;
}
 
//Will check for the Uniqueness
function contains(a, e) {
 for(j=0;j<a.length;j++)if(a[j]==e)return true;
 return false;
}

function removeItems(array, items) {
var i = 0;
while (i < array.length) {
if (array[i] == items) {
array.splice(i, 1);
} else {
i++;
}
}
return array;
}

// Source: http://www.alistapart.com/articles/horizdropdowns/

startList = function() {
if (document.all&&document.getElementById) {
navRoot = document.getElementById("navbar");
for (i=0; i<navRoot.childNodes.length; i++) {
node = navRoot.childNodes[i];
if (node.nodeName=="LI") {
node.onmouseover=function() {
this.className+=" over";
  }
  node.onmouseout=function() {
  this.className=this.className.replace(" over", "");
   }
   }
  }
 }
}
window.onload=startList;


// Set Netscape up to run the "captureMousePosition" function whenever
// the mouse is moved. For Internet Explorer and Netscape 6, you can capture
// the movement a little easier.
if (document.layers) { // Netscape
    document.captureEvents(Event.MOUSEMOVE);
    document.onmousemove = captureMousePosition;
} else if (document.all) { // Internet Explorer
    document.onmousemove = captureMousePosition;
} else if (document.getElementById) { // Netcsape 6
    document.onmousemove = captureMousePosition;
}
// Global variables
xMousePos = 0; // Horizontal position of the mouse on the screen
yMousePos = 0; // Vertical position of the mouse on the screen
xMousePosMax = 0; // Width of the page
yMousePosMax = 0; // Height of the page

function captureMousePosition(e) {
    if (document.layers) {
        // When the page scrolls in Netscape, the event's mouse position
        // reflects the absolute position on the screen. innerHight/Width
        // is the position from the top/left of the screen that the user is
        // looking at. pageX/YOffset is the amount that the user has
        // scrolled into the page. So the values will be in relation to
        // each other as the total offsets into the page, no matter if
        // the user has scrolled or not.
        xMousePos = e.pageX;
        yMousePos = e.pageY;
        xMousePosMax = window.innerWidth+window.pageXOffset;
        yMousePosMax = window.innerHeight+window.pageYOffset;
    } else if (document.all) {
        // When the page scrolls in IE, the event's mouse position
        // reflects the position from the top/left of the screen the
        // user is looking at. scrollLeft/Top is the amount the user
        // has scrolled into the page. clientWidth/Height is the height/
        // width of the current page the user is looking at. So, to be
        // consistent with Netscape (above), add the scroll offsets to
        // both so we end up with an absolute value on the page, no
        // matter if the user has scrolled or not.
        xMousePos = window.event.x+document.body.scrollLeft;
        yMousePos = window.event.y+document.body.scrollTop;
        xMousePosMax = document.body.clientWidth+document.body.scrollLeft;
        yMousePosMax = document.body.clientHeight+document.body.scrollTop;
    } else if (document.getElementById) {
        // Netscape 6 behaves the same as Netscape 4 in this regard
        xMousePos = e.pageX;
        yMousePos = e.pageY;
        xMousePosMax = window.innerWidth+window.pageXOffset;
        yMousePosMax = window.innerHeight+window.pageYOffset;
    }
}

    function init() {
		var e = document.getElementById('itemDbwin');
        var divCollection = document.getElementsByTagName("a");
        for (var i=0; i<divCollection.length; i++) {
          /*  if(divCollection[i].getAttribute("className") == "itemdb") {
				alert('founone!'); */
				if (String(divCollection[i].id).substr(0,1)=="i") {
					divCollection[i].onmouseover = function () {
						e.style.display = 'block';
						e.style.left=xMousePos+"px";
						e.style.top=yMousePos+"px";
						e.innerHTML='<div style="padding: 8px;">Please Wait: Loading...</div>';
						xmlhttpPostItem('http://quests.aionblog.net/resources/','iditem='+escape(this.id));
					};
					divCollection[i].onmouseout = function () {
						e.style.display = 'none';
					};
				}
           // } 
        }
    }

function xmlhttpPostItem(strURL,querystringaa) {
    var xmlHttpReq = false;
    var self = this;
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    self.xmlHttpReq.open('POST', strURL, true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.onreadystatechange = function() {
        if (self.xmlHttpReq.readyState == 4) {
            updatepageItem(self.xmlHttpReq.responseText);
        }
    }
	querystringaa = String(querystringaa);
    self.xmlHttpReq.send(querystringaa);
	return false;
}

function updatepageItem(str){
    document.getElementById('itemDbwin').innerHTML = '<div style="padding: 8px;">' +  str + '</div>';
}
