function MkUrlVars() {
	var URLVars = [];
	var QMark = location.href.indexOf('?');
	var LastSlash = location.href.lastIndexOf('/');
	if (QMark != -1) {
		var getVars = location.href.substr(QMark+1, location.href.length);
		var getVars = getVars.split('&');
		for (var i=0;i<getVars.length;i++) {
			var SepVars = getVars[i].split('=');
			URLVars[SepVars[0]] = SepVars[1];
		}
		URLVars[0] = 1;
		URLVars['FileName'] = location.href.substr(LastSlash+1, QMark-LastSlash-1);
	} else {
		URLVars[0] = -1;
		URLVars['FileName'] = location.href.substr(LastSlash+1, location.href.length);
	}
	
	return URLVars;
}

function GetRefer() {
	var ReferSet = location.href;
	if (ReferSet.indexOf('http://') == 0) ReferSet = ReferSet.substr(7, ReferSet.length);
	return ReferSet.split('/')[0];
}

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 getPageScroll(){
	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}

	arrayPageScroll = new Array('',yScroll) 
	return arrayPageScroll;
}

function GoTop(e) {
	if (e.pageYOffset) {
		//self.pageYOffset = 0;
		alert('1');
		e.pageYOffset = 0;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		alert('2');
		document.documentElement.scrollTop = 0;
	} else if (document.body) {// all other Explorers
		alert('3');
		document.body.scrollTop = 0;
	}
}

function getPosition(theElement) {
	var positionX = 0;
	var positionY = 0;
	while (theElement != null) {
		positionX += theElement.offsetLeft;
		positionY += theElement.offsetTop;
		theElement = theElement.offsetParent;
	}
	return [positionX, positionY];
}

function identifyBrowser() {
  var agent = navigator.userAgent.toLowerCase();

  if (typeof navigator.vendor != "undefined" && navigator.vendor == "KDE" &&
      typeof window.sidebar != "undefined")
  {
    return "kde";
  }
  else if (typeof window.opera != "undefined")
  {
    var version = parseFloat(agent.replace(/.*opera[\/ ]([^ $]+).*/, "$1"));

    if (version >= 7)
    {
      return "opera7";
    }
    else if (version >= 5)
    {
      return "opera5";
    }

    return false;
  }
  else if (typeof document.all != "undefined")
  {
    if (typeof document.getElementById != "undefined")
    {
      var browser = agent.replace(/.*ms(ie[\/ ][^ $]+).*/, "$1").replace(/ /, "");

      if (typeof document.uniqueID != "undefined")
      {
        if (browser.indexOf("5.5") != -1)
        {
          return browser.replace(/(.*5\.5).*/, "$1");
        }
        else
        {
          return browser.replace(/(.*)\..*/, "$1");
        }
      }
      else
      {
        return "ie5mac";
      }
    }

    return false;
  }
  else if (typeof document.getElementById != "undefined")
  {
    if (navigator.vendor.indexOf("Apple Computer, Inc.") != -1)
    {
      if (typeof window.XMLHttpRequest != "undefined")
      {
        return "safari1.2";
      }

      return "safari1";
    }
    else if (agent.indexOf("gecko") != -1)
    {
      return "mozilla";
    }
  }
  return false;
}

function getEventTarget(e) {
	var target;
	if (window.event && window.event.srcElement)
		target = window.event.srcElement;
	else if (e && e.target)
		target = e.target;
	while (target.id.length == 0) target = target.parentNode;
	return target;
}

function getEventTargetParent(e) {
	var target;
	if (window.event && window.event.srcElement)
		target = window.event.srcElement;
	else if (e && e.target)
		target = e.target;
	while (target.id.indexOf('TagArea') == -1 && target.parentNode != null) target = target.parentNode;
	return target;
}

function stopEvent(e) {
	if (window.event && window.event.srcElement) {
		event.cancelBubble = true; 
	} else 	e.stopPropagation();
}

function CalculateTxt(EventTitle) {
	var lengthTmp = 0;
	for (var i=0;i<EventTitle.length;i++) {
		if ((EventTitle.charCodeAt(i) >= 33 && EventTitle.charCodeAt(i) <= 255) || (EventTitle.charAt(i) == ' ')) {
			lengthTmp += 1;
		} else {
			lengthTmp += 2;
		}
	}
	return lengthTmp;
}

function TextLengthLimit(Content, LengthLimit) {
	var NewContent = '';
	var LengthTmp = 0;
	var LimitSet =  (LengthLimit) ? LengthLimit : 20;
	for (var i=0;i<Content.length;i++) {
		LengthTmp = ((Content.charCodeAt(i) >= 33 && Content.charCodeAt(i) <= 255) || Content.charAt(i) == ' ') ? LengthTmp + 1 : LengthTmp + 2;
		if (LengthTmp <= LimitSet) NewContent += Content.charAt(i);
		else { NewContent += '...'; break; }
	}//end for
	return NewContent;
}

function disableSelection(target) {
	if (typeof target.onselectstart != "undefined") //IE route
		target.onselectstart = function() { return false; }
	else if (typeof target.style.MozUserSelect != "undefined") //Firefox route
		target.style.MozUserSelect = "none";
	else //All other route (ie: Opera)
		target.onmousedown = function(){return false}
	
	target.style.cursor = "default";
}

function InputFix(str) {
	str = str.replace(/'/g, '');
	str = str.stripScripts();
	str = str.stripTags();
	return str;
}

function trim(str){
  return str.replace(/^\s*|\s*$/g,"");
}

Object.extend(Event, {
	wheel:function (event){
		var delta = 0;
		if (!event) event = window.event;
		if (event.wheelDelta) {
			delta = event.wheelDelta/120; 
			if (window.opera) delta = -delta;
		} else if (event.detail) { delta = -event.detail/3;	}
		return Math.round(delta); //Safari Round
	}
});

function showFlashObject(objID, objSource, objWidth, objHeight, objQuality, objWmode, objBgcolor) {
	//page url
	var pageUrl = self.window.location.href;
	if(pageUrl.substring(0,5) == 'https') {
		swfUrl = "https";
	} else {
		swfUrl = "http";
	}
	/* Default Value Setting */
	if (objID == "") objID = 'ShockwaveFlash1';
	if (objWidth == "") objWidth = '0';
	if (objHeight == "") objHeight = '0';
	if (objQuality == "") objQuality = 'best';
	if (objWmode == "") {
		objWmode = 'transparent';
	} else {
		objWmode = '';
	}
	/* Flash 8.0 version */
	document.write('<OBJECT id="' + objID + '" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"');
	document.write('type="application/x-shockwave-flash" ');
	document.write('data="' + objSource + '" ');
	document.write('codebase="' + swfUrl +'://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab" ');
	document.write('WIDTH="' + objWidth + '" HEIGHT="' + objHeight + '">');
	document.write('<PARAM NAME=menu value=false>');
	document.write('<PARAM NAME=allowScriptAccess value=always>');
	document.write('<PARAM NAME=wmode value="'+ objWmode +'">');
	document.write('<PARAM NAME=movie VALUE="'+ objSource +'">');
	document.write('<PARAM NAME=quality VALUE="'+ objQuality +'">');
	document.write('<PARAM NAME=bgcolor VALUE="'+ objBgcolor +'">');
	document.write('<embed allowScriptAccess="always" swLiveConnect="true" src="' + objSource + '" menu="false" quality="' + objQuality + '" wmode="' + objWmode + '" bgcolor="' + objBgcolor + '" width="' + objWidth + '" height="' + objHeight + '" type="application/x-shockwave-flash" pluginspace="http://www.macromedia.com/go/getflashplayer" name="'+ objID +'">');
	document.write('</OBJECT>');
}

function addLoadListener(fn) {
  if (typeof window.addEventListener != 'undefined') {
    window.addEventListener('load', fn, false);
  }  else if (typeof document.addEventListener != 'undefined') {
    document.addEventListener('load', fn, false);
  } else if (typeof window.attachEvent != 'undefined') {
    window.attachEvent('onload', fn);
  } else {
    var oldfn = window.onload;
    if (typeof window.onload != 'function') {
      window.onload = fn;
    } else {
      window.onload = function() {
        oldfn();
        fn();
      };
    }
  }
}

function WinOpen(Gid) {
	var PageSize = getPageSize();
	var WinOptions = 'width='+PageSize[0]+',height='+PageSize[1]+',status=yes,toolbar=no,menubar=no,resizable=yes,scrollbars=yes,left=0,top=0';
	var WinEns = window.open('/game/'+Gid+'/play', 'WebGame', WinOptions);
	WinEns.focus();
}
