var bLogoDrag=false;
var oLogoDragElem;
var oLogoOffset={};
var oParentPos;
var iLogoZ = 0;

function PrepareLogo(){
	var oLogo=document.getElementById('LogoSplitted');
	oParentPos=getAbsolutePos(oLogo);
	if(oLogo){
		var oImg=oLogo.getElementsByTagName('img');
		for(var i=0; i<oImg.length; i++){
			if(oImg[i].className && oImg[i].className == 'koko')
			{
				if (iLogoZ < oImg[i].style.zIndex)
					iLogoZ = oImg[i].style.zIndex;
				addEvent(oImg[i], 'mousedown', LogoStartDrag);
				if (isMSIE && uaVersion >= 5.5)
				{
					if (oImg[i].src.indexOf('/images/_e.gif') == -1)
					{
					oImg[i].runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + oImg[i].src + "',sizingMethod='scale')";
					oImg[i].src = '/images/_e.gif';
					}
				}
				else if (isMSIE && uaVersion < 5.5)
				{
					oImg[i].src = oImg[i].src.replace(/png/,'gif');
				}
			}
		}
	}
}

function LogoStartDrag(evt){
	if((evt=checkEvent(evt))){
		bLogoDrag=true;
		var oLogoDelta={x: evt.clientX, y: evt.clientY};
		var oPos=getAbsolutePos(evt.target);
		oLogoOffset={x: oPos.x - oLogoDelta.x, y: oPos.y - oLogoDelta.y};
		oLogoDragElem=evt.target;
		oLogoDragElem.style.zIndex = ++iLogoZ;
		evt.cancelBubble=true;
		if(evt.cancelable){
			evt.stopPropagation();
			evt.preventDefault();
		}
	}
	return false;
}

function LogoDoDrag(evt){
	if(bLogoDrag && (evt=checkEvent(evt))){
		oLogoDragElem.style.left=(evt.clientX - oParentPos.x + oLogoOffset.x)+'px';
		oLogoDragElem.style.top=(evt.clientY - oParentPos.y + oLogoOffset.y)+'px';
	}
}

function LogoStopDrag(){
	bLogoDrag=false;
}

function LogoRefreshPos()
{
	var oLogo=document.getElementById('LogoSplitted');
	oParentPos=getAbsolutePos(oLogo);
}

addEvent(document, 'mousemove', LogoDoDrag);
addEvent(document, 'mouseup', LogoStopDrag);
addEvent(window, 'resize', LogoRefreshPos);

function getAbsolutePos(oElem){
	var _x=0;
	var _y=0;
	if(oElem){
		do{
			_x+=oElem.offsetLeft;
			_y+=oElem.offsetTop;
		}while( (oElem=oElem.offsetParent) )
	}
	
	return new Point(_x, _y);
}

function Point(x, y){
	this.x=x;
	this.y=y;
}

Point.prototype.toString=function(){
	return 'x: '+this.x+', y: '+this.y;
}

/*****************************
**   Kokopelli Drag
******************************/

function checkEvent(oEvt){
	oEvt=(oEvt) ? oEvt : ( (window.event) ? window.event : null );
	if(oEvt && oEvt.srcElement && !window.opera)
		oEvt.target=oEvt.srcElement;
	return oEvt;
}

function addEvent(objElement, strEventType, ptrEventFunc) {
	if (objElement.addEventListener)
		objElement.addEventListener(strEventType, ptrEventFunc, false);
	else if (objElement.attachEvent)
		objElement.attachEvent('on' + strEventType, ptrEventFunc);
}

/**********
**   browser detection
**********/
if (!window.userApp)
{

	var userApp=navigator.userAgent.toLowerCase();
	var isMSIE = true;
	var isOpera = false;
	var isGecko = false;
	var isSafari = false;


	if (userApp.search(/opera/i)!=-1)
	{
		isOpera=true;
		
	}
	if (userApp.search(/gecko/i)!=-1)
	{
		isGecko=true;
	}
	if (userApp.search(/apple/i)!=-1)
	{
		isSafari = true;
	}
	if (isGecko || isOpera || isSafari)
	{
		isMSIE=false;
		var tempVer = userApp.search(/\/([0-9\.]+)/);
		if (tempVer != -1)
		{
			tempVer += 1;
		}
	}
	else
	{
		var tempVer = userApp.search(/msie ([0-9\.]+)/);
		if (tempVer != -1)
		{
			tempVer += 'msie '.length;
		}
	}
	if (tempVer != -1)
	{
		var uaVersion = parseFloat(userApp.substr(tempVer));
	}
	
}

function $(id){return document.getElementById(id);}