// global variables
//
var geck;
var objDiv = null;
var originalDivHTML = "";
var DivID = "";
var over = false;
var timerhandle = null;
var chatting = false;
var popmode = false;
var xMOUSE;
var yMOUSE;

//ajax chat
function stateChangedChat() 
{ 
  if (xmlHttp.readyState==4)
  { 
	document.getElementById("message").disabled=false;
	document.getElementById("message").value = "";
	document.getElementById("message").focus();
  }
}

//ajax chat
function stateChangedChatView() 
{ 
  if (xmlHttp.readyState==4)
  { 
	document.getElementById("chatview").innerHTML = xmlHttp.responseText;
  }
}

//show tools
function showtools()
{
	if (document.getElementById("tools").style.visibility != "visible") {
		document.getElementById("tools").style.visibility = "visible";
		document.getElementById("tools").style.height     = "auto";
	} else {
		document.getElementById("tools").style.visibility = "hidden";
		document.getElementById("tools").style.height     = "0px";		
	}
}

///////////////////////// find x and y  ///////////////////////////////////////
function findPosX(obj)
{
    var curleft = 0;
    if(obj.offsetParent)
        while(1) 
        {
          curleft += obj.offsetLeft;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.x)
        curleft += obj.x;
    return curleft;
}

function findPosY(obj)
{
    var curtop = 0;
    if(obj.offsetParent)
        while(1)
        {
          curtop += obj.offsetTop;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.y)
        curtop += obj.y;
    return curtop;
}

function creatediv(id, html, width, height, left, top) {
   var newdiv = document.createElement('div');
   newdiv.setAttribute('id', id);

   newdiv.style.width = width;
   newdiv.style.height = height;
   
   newdiv.style.position = "absolute";
   newdiv.style.left = left;
   newdiv.style.top = top;
   
   newdiv.style.background = "#00C";
   //newdiv.style.border = "4px solid #000";
   newdiv.style.border = '4px solid #061282';
   
   if (html) { newdiv.innerHTML = html; }
   
   document.body.appendChild(newdiv);
} 

//
function makeObject(ref, type){
	if (type==0){
		return('<object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/' +ref+ '"</param><param name="allowFullScreen" value="true"></param><embed src="http://www.youtube.com/v/' +ref+ '&fs=1&autoplay=1"  type="application/x-shockwave-flash"  allowfullscreen="true"  width="425" height="344"></embed></object>');
	} else {
		return('<embed style="width:425px; height:344px;" src="http://video.google.com/googleplayer.swf?docid='+ref+'&hl=en&fs=true&autoPlay=true" allowFullScreen="true" allowScriptAccess="always" type="application/x-shockwave-flash"> </embed>');
	}
}

// make the preview window appear
function displayFloatingDiv(divId, title, divx, divy, vidURL, thumbURL, divType, movieType) 
{
	DivID = divId;
	//alert(divType);

//find the element position, and position this element above:
//    document.getElementById(divId).style.left = findPosX(document.getElementById(divId)) + 'px';  // x
//    document.getElementById(divId).style.top = findPosY(document.getElementById(divId)) + 'px';   // y
//    document.getElementById(divId).style.width = document.getElementById(divId).offsetWidth-20 + 'px';
//    document.getElementById(divId).style.height = document.getElementById(divId).offsetHeight + 'px';

//	document.getElementById(divId).className="popped";
//	document.getElementById(divId).style.position='absolute';
	
	//position player near mouse pointer
//	document.getElementById(divId).style.left = (xMOUSE+150) + 'px';
//	document.getElementById(divId).style.top  = (yMOUSE-100) + 'px';
	document.getElementById(divId).style.visibility='visible';
	
	if (divx == 0) {
		document.getElementById(divId).style.minWidth = '425px';
		document.getElementById(divId).style.minHeight= '344px';
	} else {
		if (window.innerWidth){ //if browser supports window.innerWidth
			var BrowserW = window.innerWidth;
			var BrowserH = window.innerHeight;
		}
		else if (document.all){ //else if browser supports document.all (IE 4+)
			var BrowserW = document.body.clientWidth;
			var BrowserH = document.body.clientHeight;			
		}
		
		if (divx > (0.8*BrowserW) || divy > (0.8*BrowserH)) { 
			divx=0.5*divx;
			divy=0.5*divy;		
		}
		
		document.getElementById(divId).style.minWidth = divx + 'px';
		document.getElementById(divId).style.minHeight= divy + 'px';
	
		floatingMenu.targetX = -(divx+25)
	}
	
	//var innards;
	//innards = document.getElementById(divId).innerHTML;
	var addHeader;
	var VIDobj;
	var strYTref = '';
	
	//divtype 0 is a movie from a result, divtype 1 is a movie from the database
	if(divType==0)
	{
		//check for youtube
		var position = vidURL.indexOf('youtube.com/watch?v=');
		if (position!=-1)
		{
			strYTref = vidURL.substring(position+20,position+31);
			VIDobj = makeObject(strYTref, 0);
			watchMovie(title, thumbURL, strYTref, 0);
		}
		//check again for youtube
		position2 = vidURL.indexOf('youtube.com/v/');
		if (position2!=-1)
		{
			strYTref = vidURL.substring(position2+14,position2+25);
			VIDobj = makeObject(strYTref, 0);
			watchMovie(title, thumbURL, strYTref, 0);
		}
		//check for google
		position3 = vidURL.indexOf('docid=');
		if (position3!=-1)
		{
			//alert("pos1");
			var tempstr = vidURL.substring(position3+6,vidURL.length);
			for (var count = 0; count < tempstr.length; count++) {
			//for (ti=0; ti=<tempstr.length; ti++){
				if (tempstr.charAt(count)=="&"){
					//alert("pos3");
					strGref = tempstr.substring(0,count);
					//alert(strYTref);
					break;
				}		
			}
			VIDobj = makeObject(strGref, 1);
			watchMovie(title, thumbURL, strGref, 1);
		}
	} 
	else if (divType==1) 
	{		
		if(movieType==0){
			strYTref = vidURL;
			VIDobj = makeObject(strYTref, 0);		
		} else {
			strGref = vidURL;
			VIDobj = makeObject(strGref, 1);		
		}	
	}
	else if (divType==2)
	{
		VIDobj = "<img src='" + vidURL + "' width=" + divx + " height=" + divy + " >"
	}
	
	function showsave(ref)
	{
		if (ref.length>0){
			return ('<td style="width:18px" align="right"><a href="http://www.kissyoutube.com/watch?v='+strYTref+'" target="_blank"><img alt="Save..." title="Save..." src="save.gif" border="0"></a></td>');
		} else {return(' '); }
	}
		addHeader = '<table style="width:100%" class="floatingHeader"><tr><td ondblclick="void(0);" onmouseover="over=true;DivID=\''+ divId +'\';" onmouseout="over=false;" style="cursor:move;height:18px">' + title + '</td>'+showsave(strYTref)+'<td style="width:18px" align="right"><a href="javascript:hiddenFloatingDiv(\'' + divId + '\');void(0);"><img alt="Close..." title="Close..." src="close.png" border="0"></a></td></tr></table>';
		
		document.getElementById(divId).innerHTML = addHeader + VIDobj;
	
	//YOUTUBE
//VIDobj='<object height="100%" width="100%" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" id="object_player_3"><param value="'+vidURL+'" name="movie"/><param value="true" name="allowFullScreen"/><param value="always" name="allowScriptAccess"/><param value="fs=true" name="flashvars"/><embed height="100%" width="100%" src="'+vidURL+'" allowfullscreen="true" allowscriptaccess="always" flashvars="fs=true" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" bgcolor="#000000" id="embed_player_3"/></object>'
	
	//google
//	VIDobj = '<embed id="VideoPlayback" src="http://video.google.com/googleplayer.swf?docid=-856606244008931882&hl=en&fs=true" style="width:400px;height:326px" allowFullScreen="true" allowScriptAccess="always" type="application/x-shockwave-flash"> </embed>'
	
	
	
}

//
function hiddenFloatingDiv(divId) 
{
	document.getElementById(divId).innerHTML = "";
	document.getElementById(divId).style.visibility='hidden';
	//clearInterval(timerhandle);
	//chatting = false;
	//cTimer.stop;
	DivID = "";
}

//
function MouseDown(e) 
{
     if (over)
    {
        if (geck) {
            objDiv = document.getElementById(DivID);
            X = e.layerX;
            Y = e.layerY;
            return false;
        }
        else {
            objDiv = document.getElementById(DivID);
            objDiv = objDiv.style;
            X = event.offsetX;
            Y = event.offsetY;
        }
    }
}

//
function MouseMove(e) 
{
	//this is used with the title bar, to mousemove the previewdiv
    if (objDiv) {
        if (geck) {
            objDiv.style.top = (e.pageY-Y) + 'px';
            objDiv.style.left = (e.pageX-X) + 'px';
            return false;
        }
        else 
        {
            objDiv.pixelLeft = event.clientX-X + document.body.scrollLeft;
            objDiv.pixelTop = event.clientY-Y + document.body.scrollTop;		
            return false;
        }
    }
	
	//this can display a button in the corner of an element
	if (popmode) {
		Event=(e||event)
		Eleme=Event.srcElement||Event.target
		if(Eleme.id){
			var idS=document.getElementById("showID")
			idS.style.visibility="visible";
			idS.innerHTML="<button onclick='popmode=false;document.getElementById(\"showID\").style.visibility=\"hidden\";displayFloatingDiv(\""+Eleme.id+"\",\"Pop\",400,247,50,50);' type='button'>Pop Me ["+Eleme.id+"]</button>"

			idS.style.display='';
			idS.style.position='absolute';
			idS.style.top= findPosY(Eleme) + 'px';
			idS.style.left=findPosX(Eleme) + 'px';
			//idS.style.top= Event.clientY+10
			//idS.style.left=Event.clientX+10	
		}
	}
	
}

//
//
//
function MouseUp(e) 
{
    objDiv = null;
}

//
function init()
{
    // check geck
    geck = (document.all) ? 0 : 1;


    if (geck) 
    {
        document.captureEvents(Event.MOUSEDOWN | Event.MOUSEMOVE | Event.MOUSEUP);
    }

    document.onmousedown = MouseDown;
    document.onmousemove = MouseMove;
    document.onmouseup = MouseUp;

}

// call init
init();

///////////////////////// accelleration stuff ///////////////////////////////////////
var floatingMenuId = 'preview';
var floatingMenu =
{
targetX: -450,
targetY: 50,

hasInner: typeof(window.innerWidth) == 'number',
hasElement: document.documentElement
&& document.documentElement.clientWidth,

menu:
document.getElementById
? document.getElementById(floatingMenuId)
: document.all
? document.all[floatingMenuId]
: document.layers[floatingMenuId]
};

floatingMenu.move = function ()
{
if (document.layers)
{
floatingMenu.menu.left = floatingMenu.nextX;
floatingMenu.menu.top = floatingMenu.nextY;
}
else
{
floatingMenu.menu.style.left = floatingMenu.nextX + 'px';
floatingMenu.menu.style.top = floatingMenu.nextY + 'px';
}
}

floatingMenu.computeShifts = function ()
{
var de = document.documentElement;

floatingMenu.shiftX =
floatingMenu.hasInner
? pageXOffset
: floatingMenu.hasElement
? de.scrollLeft
: document.body.scrollLeft;
if (floatingMenu.targetX < 0)
{
if (floatingMenu.hasElement && floatingMenu.hasInner)
{
// Handle Opera 8 problems
floatingMenu.shiftX +=
de.clientWidth > window.innerWidth
? window.innerWidth
: de.clientWidth
}
else
{
floatingMenu.shiftX +=
floatingMenu.hasElement
? de.clientWidth
: floatingMenu.hasInner
? window.innerWidth
: document.body.clientWidth;
}
}

floatingMenu.shiftY =
floatingMenu.hasInner
? pageYOffset
: floatingMenu.hasElement
? de.scrollTop
: document.body.scrollTop;
if (floatingMenu.targetY < 0)
{
if (floatingMenu.hasElement && floatingMenu.hasInner)
{
// Handle Opera 8 problems
floatingMenu.shiftY +=
de.clientHeight > window.innerHeight
? window.innerHeight
: de.clientHeight
}
else
{
floatingMenu.shiftY +=
floatingMenu.hasElement
? document.documentElement.clientHeight
: floatingMenu.hasInner
? window.innerHeight
: document.body.clientHeight;
}
}
}

floatingMenu.doFloat = function()
{
var stepX, stepY;

floatingMenu.computeShifts();

stepX = (floatingMenu.shiftX +
floatingMenu.targetX - floatingMenu.nextX) * .07;
if (Math.abs(stepX) < .5)
{
stepX = floatingMenu.shiftX +
floatingMenu.targetX - floatingMenu.nextX;
}

stepY = (floatingMenu.shiftY +
floatingMenu.targetY - floatingMenu.nextY) * .07;
if (Math.abs(stepY) < .5)
{
stepY = floatingMenu.shiftY +
floatingMenu.targetY - floatingMenu.nextY;
}

if (Math.abs(stepX) > 0 ||
Math.abs(stepY) > 0)
{
floatingMenu.nextX += stepX;
floatingMenu.nextY += stepY;
floatingMenu.move();
}

setTimeout('floatingMenu.doFloat()', 20);
};

// addEvent designed by Aaron Moore
floatingMenu.addEvent = function(element, listener, handler)
{
if(typeof element[listener] != 'function' ||
typeof element[listener + '_num'] == 'undefined')
{
element[listener + '_num'] = 0;
if (typeof element[listener] == 'function')
{
element[listener + 0] = element[listener];
element[listener + '_num']++;
}
element[listener] = function(e)
{
var r = true;
e = (e) ? e : window.event;
for(var i = element[listener + '_num'] -1; i >= 0; i--)
{
if(element[listener + i](e) == false)
r = false;
}
return r;
}
}

//if handler is not already stored, assign it
for(var i = 0; i < element[listener + '_num']; i++)
if(element[listener + i] == handler)
return;
element[listener + element[listener + '_num']] = handler;
element[listener + '_num']++;
};

floatingMenu.init = function()
{
floatingMenu.initSecondary();
floatingMenu.doFloat();
};

// Some browsers init scrollbars only after
// full document load.
floatingMenu.initSecondary = function()
{
floatingMenu.computeShifts();
floatingMenu.nextX = floatingMenu.shiftX +
floatingMenu.targetX;
floatingMenu.nextY = floatingMenu.shiftY +
floatingMenu.targetY;
floatingMenu.move();
}

if (document.layers)
floatingMenu.addEvent(window, 'onload', floatingMenu.init);
else
{
floatingMenu.init();
floatingMenu.addEvent(window, 'onload',
floatingMenu.initSecondary);
}

