/*
 Name: General JavaScript Functions
 Created: 19.04.2010
 Modified: 01.05.2010
 Author: Lakutin Ivan
*/

var __browsers = {IE: "Microsoft", NETSCAPE: "Netscape", OPERA: "Opera"}

function browserIsIE()
{
	return (navigator.appName.indexOf(__browsers.IE) == 0);
}

function browserIsNetscape()
{
	return (navigator.appName.indexOf(__browsers.NETSCAPE) == 0);
}

function browserIsOpera()
{
	return (navigator.appName.indexOf(__browsers.OPERA) == 0);
}

function getElementStyle(obj)
{
	if (browserIsIE())
	return obj.styleSheet;
	else
	return obj.style;
}

function getCurrentDomain(){
	return location.host;
}

function getElementStyleById(obj)
{
	if (browserIsIE())
	return document.getElementById(obj).styleSheet;
	else
	{
	return document.getElementById(obj).style;
	}
}



/* [ Graphical User Interface Functions ] */

var windows = Array();
var spoilers_names = Array();
var spoilers_onclick_functions = Array();
var windows_count = 0;
var spoilers_count = 0;

var win_colors = {
	active: "#69C",
	deactive: "#B8D5DC",
	border: "gray"
}

var button_colors = {
	onMousePos: ["#4E8DB8", "1px double #339"],
	offMousePos: ["#7EACCB", "1px double black"]
}

var spoiler_colors = {
	closed: "#4E8DB8",
	opened: "#7EACCB",
	panel: "white"
}

var drag_object = null;

var $$ = function()
{
	var object = null;
	var _page = function()
	{
		_page.prototype.Height = function(){
		var frameHeight=640;
		if (self.innerHeight) frameHeight = self.innerHeight;
		else
		if (document.documentElement && document.documentElement.clientHeight) frameHeight = document.documentElement.clientHeight;
		else
		if (document.body) frameHeight = document.body.clientHeight;
		return frameHeight;
		}
		
		_page.prototype.Width = function(){
		var frameWidth=800;
		if (self.innerWidth) frameWidth = self.innerWidth;
		else
		if (document.documentElement && document.documentElement.clientWidth) frameWidth = document.documentElement.clientWidth;
		else
		if (document.body) frameWidth = document.body.clientWidth;
		return frameWidth;
		}	
	}
	
	var _gui = function()
	{

		
		_gui.prototype.CreateDivLine = function(obj_id,x,y,w, h){
			
			document.getElementById(obj_id).innerHTML = document.getElementById(obj_id).innerHTML + 
			'<hr style="position:absolute;background:white;border-top-width: 1px;border-right-width: 0px;border-bottom-width:0px;border-left-width: 0px;border-top-style: solid;border-right-style: none;border-bottom-style: none;border-left-style: none;border-top-color: #999;left:'+x+'px;top:'+y+'px;width:'+w+'px;height:'+h+'px;"/>';
		}
		
		_gui.prototype.CreateButton = function(obj_id, id, x, y, w, h, caption, EOnClick)
		{
			var obj = document.getElementById(obj_id);
			var button = "<div id='"+id+"' onmouseover='ButtonOnMove(this)' onmouseout='ButtonOnOut(this)' OnMouseDown='void(0);' onclick='"+EOnClick+"' style='cursor:pointer; background:"+button_colors.offMousePos[0]+";position:absolute;left:"+x+"px;top:"+y+"px;width:"+w+"px;height:"+h+"px;'><div align='center'>"+caption+"</div></div>";
			obj.innerHTML = obj.innerHTML + button;
			ButtonOnOut(document.getElementById(id));
		}
		

		
		_gui.prototype.CreateSpoilerObject = function(obj_id, id, x, y, w, h, pheight, mclosed, caption, EOnClick, HTML)
		{
			var obj = document.getElementById(obj_id);
			var pShow = null;
			var splrObj = "<div id='"+id+"' onclick='changeSpoilerMode(this)' style='cursor:pointer; background:"+spoiler_colors.closed+";position:absolute;left:"+x+"px;top:"+y+"px;width:"+w+"px;height:"+h+"px;'><div align='center'>"+caption+"</div></div>";
			
			if (mclosed == true)
			pShow = "none";
			else
			pShow = "";
			
			var splrpnlObj = "<div id='"+id+"_panel' style='background:"+spoiler_colors.panel+";display:"+pShow+";position:absolute;left:"+x+"px;top:"+(y+h)+"px;width:"+w+"px;height:"+(h+pheight)+"px;'><div align='center'>"+HTML+"</div></div>";
			obj.innerHTML = obj.innerHTML + splrObj+splrpnlObj;			
			spoilers_onclick_functions[spoilers_count] = EOnClick;
			spoilers_names[spoilers_count] = id;
			spoilers_count++;
		}
	
	
	}
	
	var _window = function()
	{

		_window.prototype.SetHTML = function(win_class, center, arg)
		{
			var obj = document.getElementById(win_class+"_HTML");
			obj.innerHTML = arg;
			if (center == true)
			{
			getElementStyle(obj).textAlign = "center";
			}
		}
		
		_window.prototype.SetCaption = function(win_class, arg)
		{
			document.getElementById(win_class+"_caption").innerHTML = arg;
		}		
		
		_window.prototype.CreateWindow = function(win_class)
		{
            document.write('<div id="'+win_class+'"></div>');
			document.write('<div id="'+win_class+'_window" onmousedown=WindowToFront(document.getElementById("'+win_class+'")); style="position:absolute">');
			document.write('<div id="'+win_class+'_bckgnd" style="opacity:0.5;position:absolute;"></div>');
			document.write('<div id="'+win_class+'_caption" onmousedown=StartDragWindow(document.getElementById("'+win_class+'"),event); onmousemove=MoveObject(document.getElementById("'+win_class+'_window")); onmouseup=StopDrag(); style=" cursor:default;position:absolute"></div>');
			document.write('<div id="'+win_class+'_HTML" onmousedown=WindowToFront(document.getElementById("'+win_class+'")); style="padding:1px;position:absolute">');
			document.write('</div>');
			document.write('</div>');
			document.write('</div>');			
			windows[windows_count] = win_class;
			windows_count++;		
		}		
		
		_window.prototype.HideWindow = function(win_class)
		{
			 getElementStyleById(win_class+"_window").display = "none";			 
		}
			
		
		_window.prototype.ShowWindow = function(win_class, newpos, left, top, width, height)
		{
			var win_object = null;
			win_object = document.getElementById(win_class+"_window");
			if (getElementStyle(win_object).display == "none")
			{
				getElementStyle(win_object).display = "";
			}
			else
			if (newpos == true)
			{
			getElementStyle(win_object).left = left+"px";
			getElementStyle(win_object).top = top+"px";
			getElementStyle(win_object).width = width+"px";
			getElementStyle(win_object).height = height+"px";
			getElementStyle(win_object).background = "white";
			win_object = document.getElementById(win_class+"_caption");
			getElementStyle(win_object).left = 1+"px";
			getElementStyle(win_object).top = 1+"px";
			getElementStyle(win_object).width = (width-4)+"px";
			getElementStyle(win_object).height = 22+"px";
			
			getElementStyle(win_object).borderTopWidth = "1px";
			getElementStyle(win_object).borderLeftWidth = "1px";
			getElementStyle(win_object).borderTopStyle = "solid";
			getElementStyle(win_object).borderLeftStyle = "solid";
			getElementStyle(win_object).borderTopColor = "#999";
			getElementStyle(win_object).borderLeftColor = "#999";
			
			win_object.style.background = win_colors.deactive;
			win_object = document.getElementById(win_class+"_HTML");
			win_object.style.left = 1+"px";
			win_object.style.top = 24+"px";
			win_object.style.width = (width-6)+"px";
			win_object.style.height = (height-29)+"px";
			win_object.style.background = "#F1F0E7";			
			win_object.style.borderTopWidth = "1px";
			win_object.style.borderLeftWidth = "1px";
			win_object.style.borderTopStyle = "solid";
			win_object.style.borderLeftStyle = "solid";
			win_object.style.borderTopColor = "#999";
			win_object.style.borderLeftColor = "#999";

			win_object = document.getElementById(win_class+"_bckgnd");
			win_object.style.left = 2+"px";
			win_object.style.top = 1+"px";
			win_object.style.width = (width-3)+"px";
			win_object.style.height = (height-2)+"px";
			win_object.style.background = win_colors.border;
			}

		}
	}

		$$.prototype.value = function(objectId, arg)
		{
			document.getElementById(objectId).value = arg;
		}
		$$.prototype.getValue = function(objectId)
		{
			return document.getElementById(objectId).value;
		}	
		$$.prototype.checkboxValue = function(objectId, checked)
		{
			document.getElementById(objectId).checked = checked;
		}	
		
		$$.prototype.getCheckValue = function(objectId)
		{
			if (document.getElementById(objectId).checked == true)
			return "1";
			else
			return "0";
		}
		
		$$.prototype.getFullCheckValue = function(objectId)
		{
			return document.getElementById(objectId).checked; 
		}		
		
		$$.prototype.getCheckValue = function(objectId)
		{
			if (document.getElementById(objectId).checked == true)
			return "1";
			else
			return "0";
		}		
		
		$$.prototype.setInputMode = function(objectId, pswd)
		{
			if (pswd==true)
			document.getElementById(objectId).type = "password";
			else
			document.getElementById(objectId).type = "text";			
		}
	
		$$.prototype.getHTML = function(objectId)
		{
			return document.getElementById(objectId).innerHTML;
		}
		
		$$.prototype.HTML = function(objectId, arg)
		{
			document.getElementById(objectId).innerHTML = arg;
		}	
		
		$$.prototype.setId = function(objectId, newId)
		{
			document.getElementById(objectId).id = newId;
		}	
		
		$$.prototype.inc = function(value)
		{
			if (typeof(value)=="string")
			return parseInt(value)+1;
			else
			if (typeof(value)=="number")
			return value+1;
		}			

		$$.prototype.IsExists = function(obj)
		{
			if (obj == undefined)
			return false;
			else
			return true;
		}
		
		$$.prototype.focusObject = function(objId)
		{
			document.getElementById(objId).focus();
		}

	$$.prototype.page = new _page();
	$$.prototype.window = new _window();
	$$.prototype.gui = new _gui();		
}

function getCenter(x)
{
	return Number(x / 2).toFixed(0);
}

function randomNumber(m,n)
{
  m = parseInt(m);
  n = parseInt(n);
  return Math.floor( Math.random() * (n - m + 1) ) + m;
}
		
function ButtonOnMove(obj)
{
	obj.style.background =  button_colors.onMousePos[0]; 
	obj.style.border = "";
	obj.style.border = button_colors.onMousePos[1];
}

function ButtonOnOut(obj)
{
	obj.style.background = button_colors.offMousePos[0];
	obj.style.border = "";
	obj.style.border = button_colors.offMousePos[1];
}

/* [ AJAX Functions ] */

function createXMLHTTP()
{
 if(typeof XMLHttpRequest != 'undefined')
 {
   return new XMLHttpRequest();
 }
 else 
 if(window.ActiveXObject)
 {
   var aVersions = ['MSXML2.XMLHttp.5.0','MSXML2.XMLHttp.4.0','MSXML2.XMLHttp.3.0', 'MSXML2.XMLHttp','Microsoft.XMLHttp'];
   for (var i=0; i<aVersions.length; i++) {
   try
   {
     var oXmlHttp = new ActiveXObject(aVersions[i]);
     alert(aVersions[i]);
     return oXmlHttp;
   } 
   catch (oError)
   {
   }
 }
    throw new Error('Error at creation XMLHttp.');
 }
}

var _cb = null;
var _ercb = null;

function CallBackFunct(fname, info)
{
  document.location.href = "javascript: "+fname+"("+info+");";
}

function getRequestWithStrCallBack(req,callback, errorcb) {
   var http = createXMLHTTP();
   _cb = callback;
   _ercb = errorcb;
   http.open('GET',req, true);
   http.onreadystatechange = function() {
    if(http.readyState == 4) {
     if(http.status == 200) {
       CallBackFunct(_cb, http.responseText);
     } else {
        CallBackFunct(_ercb, http.statusText);
     }
    }
   };
   http.send(null);
}


/* [ Drop & Drop Functions ] */

var dx = 0;
var dy = 0;
var flag = false; 

function StartDrag(moveObj,evnt){
	if(!evnt)
		evnt = window.event;
	flag = true;
	dx = evnt.clientX-moveObj.offsetLeft;
	dy = evnt.clientY-moveObj.offsetTop;

	if(evnt.stopPropagation)
		evnt.stopPropagation();
	else
		evnt.cancelBubble = true;
	
	if(evnt.preventDefault)
		evnt.preventDefault();
	else
		evnt.returnValue = false;
}
 
function StopDrag()
{
	flag=false;
}

function _MoveObject(moveObj,evnt){
	if(!flag) return;
	if(!evnt)
		evnt = window.event;

	moveObj.style.left = (evnt.clientX-dx) + "px";
	moveObj.style.top = (evnt.clientY-dy) + "px";
	
	if(evnt.stopPropagation)
		evnt.stopPropagation();
	else
		evnt.cancelBubble = true;
	
	if(evnt.preventDefault)
		evnt.preventDefault();
	else
		evnt.returnValue = false;
		
}

function MoveObject(moveObj)
{
	drag_object = moveObj;
}

function WindowToFront(win_class)
{
	var obj = null;
	for (i=0;i<=windows_count-1;i++)
	{
		obj = document.getElementById(windows[i]+"_window");
		obj.style.zIndex = 0;
		obj = document.getElementById(windows[i]+"_caption");		
		obj.style.background = win_colors.deactive;
	}
	document.getElementById(win_class.id+"_window").style.zIndex = 10;
	document.getElementById(win_class.id+"_caption").style.background = win_colors.active;
}

function execJS(jscode){
	document.location.href = "javascript: "+jscode;
}

function getSpoilerOnClickFunction(objId)
{
	var obj = null;
	for (i=0;i<=spoilers_count-1;i++)
	{
		if (spoilers_names[i] == objId)
		{
			return spoilers_onclick_functions[i];
		}
	}	
}

function changeSpoilerMode(obj)
{
	var object = document.getElementById(obj.id+"_panel");
	if (object.style.display == "none")
	{
	object.style.display = "";
	execJS(getSpoilerOnClickFunction(obj.id));
	}
	else
	{
	object.style.display = "none";		
	}
}

function StartDragWindow(moveObj,evnt)
{
	WindowToFront(moveObj);
	StartDrag(document.getElementById(moveObj.id+"_window"), evnt);
}

window.onmousemove = function(e)
{
	_MoveObject(drag_object,e);
}

window.onmouseup = function(e)
{
	StopDrag();
}

document.onmousemove = function(e)
{
	_MoveObject(drag_object,e);
}

document.onmouseup = function(e)
{
	StopDrag();
}

/* [ Flash Movie Functions ] */

function getFlashObj(objName) {
var M$ =  navigator.appName.indexOf("Microsoft")!=-1
return (M$ ? window : document)[objName]
}

function newFlashObject(id, name, src, w, h)
{
	var ___swfObj = '<object id="'+id+'" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=5,0,0,0" width="'+w+'" height="'+h+'">'
 +'<param name=movie value="'+src+'"><param name=quality value=high><param name="allowScriptAccess" value="sameDomain" />'
 +'<embed name="'+name+'" play="true" loop="false" quality="high" allowScriptAccess="sameDomain" src="'+src+'" swLiveConnect="true" quality=high pluginspage="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash" type="application/x-shockwave-flash" width="'+w+'" height="'+h+'"></embed></object>';
	return ___swfObj;
}



