var todb = 'sro';
var tourl = 'http://'+todb+'.db.mmosite.com';
var tocat = 'armor|weapon|quest|monster|npc|alchemy|tablet';
var tohead = document.getElementsByTagName('head')[0];
var tipName = 'mmositeTip';
var tipPosition = new Array();
var tocss = 'http://sro.db.mmosite.com/Style/tip.css';
var timer;

/*css 娣诲姞*/
var cssEl = document.createElement('link');
cssEl.href = tocss;
cssEl.type = 'text/css';
cssEl.rel = 'stylesheet';
tohead.appendChild(cssEl);

Browser={ie:navigator.appName.indexOf("Microsoft")!=-1,ie7:this.ie&&navigator.appVersion.indexOf("MSIE 7")!=-1,ie6:this.ie&&navigator.appVersion.indexOf("MSIE 6")!=-1,opera:!!window.opera,safari:navigator.userAgent.indexOf("Safari")!=-1,gecko:navigator.userAgent.indexOf("Gecko")!=-1&&navigator.userAgent.indexOf("KHTML")==-1}

function addVar(content,divName){

	var div = document.getElementById(divName);
	div.innerHTML = content;
	div.style.cssText = 'position:absolute;font:normal 12px Arial; color:#fff; background-color:#000;z-index:1000;';
    div.style.display = '';
}

function pDatabase(){
	
	if (typeof document.compatMode != 'undefined' && document.compatMode != 'BackCompat'){

		var scrollT = document.documentElement.scrollTop;
		var scrollL = document.documentElement.scrollLeft;		
	}else{
		
		var scrollT = document.body.scrollTop;
		var scrollL = document.body.scrollLeft;
	}
	var divT = parseInt(tipPosition[1])-scrollT;
	var divL = parseInt(tipPosition[0])-scrollL;
	var getPageSizew = getPageSize();
	var winW = getPageSizew[2];
	var winH = getPageSizew[3];	
	
	var div = document.getElementById(tipName);
	if(divT>winH/2){
		
		var top = parseInt(tipPosition[1])-parseInt(div.offsetHeight)+'px';
	}else{
		
		var top = parseInt(tipPosition[1])+20+'px';
	}
	if(divL>winW/2){
		
		var left = parseInt(tipPosition[0])-parseInt(div.offsetWidth)+'px';
	}else{
		
		var left = tipPosition[2]+'px';
	}
	div.style.top = top;
	div.style.left = left;
	
	//alert(getPageSizew[0] +':'+ getPageSizew[1] +':'+ getPageSizew[2] +':'+ getPageSizew[3]);
}

function getPageSize(){

	var xScroll, yScroll;

	if (window.innerHeight && window.scrollMaxY) {
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}

	var windowWidth, windowHeight;
	if (self.innerHeight) {    // all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}

	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else {
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight)
	return arrayPageSize;
}

function exQuality(quality){
	
	for(i in quality){
		
		if(Browser.ie){
			
			var els = document.body.all[i];

			for (j=0;j<els.length;j++){
				
				els[j].setAttribute('className','qua_'+quality[i]);
			}
		}else{
						
			var els = document.getElementsByName(i);
			for (j=0;j<els.length;j++){
				
				els[j].setAttribute('class','qua_'+quality[i]);
			}
		}
	}
}

function parseLinks(){
	
	var body = document.getElementsByTagName('body')[0];
	/*涓籺ip娣诲姞*/
	var divEl = document.createElement('div');
	divEl.id = tipName;
	divEl.style.display = 'none';
	
	divEl.onmouseover = function(){ 
		if( timer != null )clearTimeout( timer );
		this.style.display = 'block'; 
	}
	divEl.onmouseout = function(){ 
		this.style.display = 'none';
	}
	
	body.appendChild(divEl);
	
	var linksCol=document.getElementsByTagName("a");
	var lpattern=new RegExp(todb+"\\.db\\.mmosite\\.com\\/html\\/gdb_"+todb+"\\/("+tocat+")\\/("+tocat+")_(\\d+)\\.shtml");
	var quaStr = '';
	for(var i=0;i<linksCol.length;i++){
		var match=lpattern.exec(linksCol[i].href);
		if(match){

			/*insertBefore icon*/
			var className = Browser.ie?linksCol[i].getAttribute('className'):linksCol[i].getAttribute('class');
			if(className == 'show_icon'){
				
				var imgSpan = document.createElement('span');imgSpan.style.cursor = 'pointer';
				var icon = document.createElement('img');
				icon.src = tourl+"/?controller=dbindex&action=icon&t="+match[1]+"&id="+match[3];
				if(imgSpan.addEventListener){
	
					imgSpan.addEventListener('mouseover',funcMouseover(linksCol[i],match[1],match[3]),false);
					imgSpan.addEventListener('mouseout',funcMouseOut(match[1],match[3]),false);
					imgSpan.addEventListener('click',funcClick(match[0]),false);
				}else{
					
					imgSpan.attachEvent('onmouseover',funcMouseover(linksCol[i],match[1],match[3]));
					imgSpan.attachEvent('onmouseout',funcMouseOut(match[1],match[3]));
					imgSpan.attachEvent('onclick',funcClick(match[0]));
				}

				imgSpan.appendChild(icon);
				linksCol[i].parentNode.insertBefore(imgSpan,linksCol[i]);
			}
			
			/*fuck ie*/
			h = linksCol[i].innerHTML;
			linksCol[i].innerHTML = '';
			fuckie = document.createElement('span');
			if(Browser.ie){
				
				fuckie.id = 'qua_'+match[1]+'_'+match[3];
			}else{
			
				fuckie.setAttribute('name','qua_'+match[1]+'_'+match[3]);
			}
			fuckie.innerHTML = h;
			linksCol[i].appendChild(fuckie);
			
			
			if(linksCol[i].addEventListener){

				linksCol[i].addEventListener('mouseover',funcMouseover(linksCol[i],match[1],match[3]),false);
				linksCol[i].addEventListener('mouseout',funcMouseOut(match[1],match[3]),false);
			}else{
				
				linksCol[i].attachEvent('onmouseover',funcMouseover(linksCol[i],match[1],match[3]));
				linksCol[i].attachEvent('onmouseout',funcMouseOut(match[1],match[3]));
			}
			quaStr += match[1]+':'+match[3]+'|';
		}
	}
			/*鐗╁搧鍝佽川*/
			var scriptEl = document.createElement('script');
			scriptEl.src = tourl+'/?controller=dbindex&action=quality&qua='+quaStr;
			tohead.appendChild(scriptEl);
}

function funcMouseover(el,name,id){
	
	return function (){
		if( timer != null )clearTimeout( timer );
		
		tipPosition = findPosition(el);
		
		try{
			
			
			tipVal = eval(name+id);
			addVar(tipVal,tipName);
			pDatabase();
		}catch(e){
			
			
			var scriptEl = document.createElement('script');
			scriptEl.src = tourl+'/?controller=dbindex&action=tooltipo&t='+name+'&id='+id;
			tohead.appendChild(scriptEl);
			addVar("Loading...",tipName);
			pDatabase();
		}

		//alert(1);
	}
}
function funcMouseOut(name,id){
	
	return function (){
		if( timer != null )clearTimeout( timer );
		timer = setTimeout( function(){document.getElementById(tipName).style.display = 'none';},1000 )
	}
}

function funcClick(url){
	
	return function (){
		
		window.location.href = 'http://'+url;
	}
}

function findPosition( oElement ){
	var x2 = 0;
	var y2 = 0;
	var width = oElement.offsetWidth;
	var height = oElement.offsetHeight;
	if( typeof( oElement.offsetParent ) != 'undefined' ){
		for( var posX = 0, posY = 0; oElement; oElement = oElement.offsetParent ){
			posX += oElement.offsetLeft;
			posY += oElement.offsetTop;
		}
		x2 = posX + width;
		y2 = posY + height;
		return [ posX, posY ,x2, y2];

	} else{
		x2 = oElement.x + width;
		y2 = oElement.y + height;
		return [ oElement.x, oElement.y, x2, y2];
	}
}

if(document.addEventListener){

	document.addEventListener('DOMContentLoaded',parseLinks,false);
}else{
	
	document.onreadystatechange = function (){
		
		if(document.readyState == 'complete'){
			
			parseLinks();
		}
	}
}