var posx = 0;
var posy = 0;

function showContent(id) {
    document.getElementById(id).style.display="block";
}

function hideContent(id) {
    document.getElementById(id).style.display="none";
}

function showVideo(el, fmsPath, e) {
    posx = -e.clientX - document.body.width;
			//+ document.documentElement.scrollLeft;
	posy = e.clientY + document.body.scrollTop
			+ document.documentElement.scrollTop;
	document.getElementById(el).style.top = posy+"px";
	document.getElementById(el).style.right = posx+"px";
	document.getElementById(el).style.display = "block";
}

function setFocus(id) {
    var fld = document.getElementById(id);    
    fld.focus();
} 

function selectAll(id) {
    var fld = document.getElementById(id);    
    fld.select();
} 

function toggleContent(el, e) {
    obj = document.getElementById(el);
    if (obj.style.display == "none") {
        obj.style.display = "block";
    }
    else
        obj.style.display = "none";
}
function hide(el) {
    document.getElementById(el).style.display = "none";
}
function show(el) {
    document.getElementById(el).style.display = "block";
}
function flashRow(obj) {
    obj.bgColor = "#FFFF99";
}
function unFlashRow(obj) {    
    obj.bgColor = "#dddddd";
}
function cancelProgress()
{
    document.getElementById('progress').innerHTML = '';
}
function handleResponse() {
  if(xmlhttp.readyState == 4){
    if (xmlhttp.status == 200){
      var response = xmlhttp.responseText;
      var update = new Array();

      if(response.indexOf('|') != -1) {
      }
    }
  }
}

String.prototype.trim=function(){
    return this.replace(/^\s*|\s*$/g,'');
}

String.prototype.ltrim=function(){
    return this.replace(/^\s*/g,'');
}

String.prototype.rtrim=function(){
    return this.replace(/\s*$/g,'');
}

function setTitle(id) {
    // switch UL with a loading div
	desc = document.getElementById(id+"_title_input_box").value;
	escdesc = escape(desc);
	document.getElementById(id+'_active_title').innerHTML = '<img src="/images/smallwait.gif">';
	var xmlhttp = new XMLHttpRequest();
    xmlhttp.open('get', "dbfunc.php?id="+id+"&var=title&val="+escdesc);
    xmlhttp.onreadystatechange = function() {
        if(xmlhttp.readyState == 4){
            if (xmlhttp.status == 200){
                var response = xmlhttp.responseText;
                document.getElementById(id+'_active_title').innerHTML = response;
            }
        }
    }
    xmlhttp.send(null);	
}

function getDescription(id) {
    // switch UL with a loading div
	desc = document.getElementById(id+"_description").value;
	escdesc = escape(desc);
	var xmlhttp = new XMLHttpRequest();
    xmlhttp.open('get', "getdescription.php?id="+id);
    xmlhttp.onreadystatechange = function() {
        if(xmlhttp.readyState == 4){
            if (xmlhttp.status == 200){
                var response = xmlhttp.responseText;
                document.getElementById(id+'_description').innerHTML = response;
            }
        }
    }
    xmlhttp.send(null);	
}

function getTitle(id) {
    // switch UL with a loading div
	desc = document.getElementById(id+"_title").value;
	escdesc = escape(desc);
	var xmlhttp = new XMLHttpRequest();
    xmlhttp.open('get', "gettitle.php?id="+id);
    xmlhttp.onreadystatechange = function() {
        if(xmlhttp.readyState == 4){
            if (xmlhttp.status == 200){
                var response = xmlhttp.responseText;
                document.getElementById(id+'_title').innerHTML = 
                    "<p style=\"font: 14px Verdana, Arial, Helvetica, sans-serif; text-decoration: none; font-weight:bold; color:#66f\">"+response+"</p>";
            }
        }
    }
    xmlhttp.send(null);	
}

function setDescription(id) {
    // switch UL with a loading div
	desc = document.getElementById(id+"_description_input_box").value;
	escdesc = escape(desc);
    document.getElementById(id+'_active_description').innerHTML = '<img src="/images/smallwait.gif">';
	var xmlhttp = new XMLHttpRequest();
    xmlhttp.open('get', "dbfunc.php?id="+id+"&var=description&val="+escdesc);
    xmlhttp.onreadystatechange = function() {
        if(xmlhttp.readyState == 4){
            if (xmlhttp.status == 200){
                var response = xmlhttp.responseText;
                if (desc == "")
	               document.getElementById(id+'_active_description').innerHTML = "(No Description)";
                else
                   document.getElementById(id+'_active_description').innerHTML = response;
            }
        }
    }
    xmlhttp.send(null);	
}

function setPublic(id, status) {
	// switch UL with a loading div
	e = document.getElementById(id+"_public");
	e.innerHTML = '<img src="/images/smallwait.gif">';
	tooltip.hide();
	tooltip.init();
	var xmlhttp = new XMLHttpRequest();
    xmlhttp.open('get', "dbfunc.php?id="+id+"&var=isPublic&val="+status);
    xmlhttp.onreadystatechange = function() {
        if(xmlhttp.readyState == 4){
            if (xmlhttp.status == 200){
                var response = xmlhttp.responseText;
                if (response == 1)
                    e.innerHTML = '<div title="Video is viewable by the world.&lt;br /&gt;Click to make private."><img src="icons/page_white_world.png" border="0" onclick="setPublic(\''+id+'\', 0)"></div>';
                else if (response == 0)
                    e.innerHTML = '<div title="Video is only viewable to you.&lt;br /&gt;Click to make public."><img src="icons/page_white_delete.png" border="0" onclick="setPublic(\''+id+'\', 1)"></div>';
                //window.location.reload(true);
            }
        }
        tooltip.init ();
    }
    xmlhttp.send(null);
}

function userNameValid(name) {
	var xmlhttp = new XMLHttpRequest();
    xmlhttp.open('get', "dbfunc.php?id="+id+"&var=public&val="+status);
    xmlhttp.onreadystatechange = function() {
        if(xmlhttp.readyState == 4){
            if (xmlhttp.status == 200){
                var response = xmlhttp.responseText;
                if (response == 1)
                    e.innerHTML = '<div title="Video is viewable by the world.&lt;br /&gt;Click to make private."><img src="icons/page_white_world.png" border="0" onclick="setPublic(\''+id+'\', 0)"></div>';
                else if (response == 0)
                    e.innerHTML = '<div title="Video is only viewable to you.&lt;br /&gt;Click to make public."><img src="icons/page_white_delete.png" border="0" onclick="setPublic(\''+id+'\', 1)"></div>';
                //window.location.reload(true);
            }
        }
        tooltip.init ();
    }
    xmlhttp.send(null);
    
}

function getFlvPath(id) {
	// switch UL with a loading div
    xmlhttp.open('get', "getvideo.php?id="+id, false);  
    xmlhttp.send(null);
    return xmlhttp.responseText;
}

function confirmDelete(title, delUrl) {
  if (confirm("Are you sure you want to delete " + title + "?")) {
    document.location = delUrl;
  }
}

function confirmCancelAccount(canUrl) {
  if (confirm("Are you sure you want to cancel your account?  Note that you will be refunded a prorated amount based on your current monthly usage.")) {
    document.location = canUrl;
  }
}


function load()
{
    document.getElementById('loadStatus').innerHTML = '';
}

function setNowPlaying1(id, width, height) {
    d = document.getElementById('nowplaying');
    d.style.width = width;
    d.style.height = height;
    d.innerHTML = '<img src="/images/loader.gif">';
    xmlhttp.open('get', "/video/"+id);
    xmlhttp.onreadystatechange = function() {
        if(xmlhttp.readyState == 4){
            if (xmlhttp.status == 200){
                var response = xmlhttp.responseText;
                d.innerHTML = response;
            }
        }
    }
    xmlhttp.send(null);	
}

function setNowPlaying(id, title, description, width, height) {
   var so = new SWFObject("http://m3diaworks.com/show.swf", title, width, height, "8", "#336699");
   so.addVariable("id", id);
   so.write("flashcontent");
}

function setNowPlaying2(id, title, description, width, height) {
    d = document.getElementById('basicModalContent');
    d.style.width = width;
    d.style.height = height+37;
    var ret = '<object align="middle" '+
        'classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" '+
        'codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" '+
        'height="'+(height+37)+'" '+
        'width="'+width+'"> '+
        '<param name="allowScriptAccess" value="sameDomain"> '+
        '<param name="movie" value="http://www.m3diaworks.com/video/show.swf?id='+id+'"> '+
        '<param name="loop" value="false"> '+
        '<param name="quality" value="high"> '+
        '<param name="bgcolor" value="#CCCCCC"> '+
        '<param name="wmode" value="transparent"> '+
        '<embed align="middle" allowscriptaccess="sameDomain" bgcolor="#CCCCCC" '+ 
            'loop="false" '+ 
            'pluginspage="http://www.macromedia.com/go/getflashplayer" '+
            'quality="high" '+
            'src="http://www.m3diaworks.com/video/show.swf?id='+id+'" '+
            'type="application/x-shockwave-flash" '+ 
            'width="'+width+'" '+
            'height="'+(height+37)+'"> '+
        '</embed>'+
    '</object>';
    d.innerHTML = ret;
}

//////////////////////////////////////////////////////////////////
// qTip - CSS Tool Tips - by Craig Erskine
// http://qrayg.com
//
// Multi-tag support by James Crooke
// http://www.cj-design.com
//
// Inspired by code from Travis Beckham
// http://www.squidfingers.com | http://www.podlob.com
//////////////////////////////////////////////////////////////////

var qTipTag = "a,label,div,img"; //Which tag do you want to qTip-ize? Keep it lowercase!//
var qTipX = 0; //This is qTip's X offset//
var qTipY = 15; //This is qTip's Y offset//

//There's No need to edit anything below this line//
tooltip = {
  name : "qTip",
  offsetX : qTipX,
  offsetY : qTipY,
  tip : null
}

tooltip.init = function () {
	var tipNameSpaceURI = "http://www.w3.org/1999/xhtml";
	if(!tipContainerID){ var tipContainerID = "qTip";}
	var tipContainer = document.getElementById(tipContainerID);

	if(!tipContainer) {
	  tipContainer = document.createElementNS ? document.createElementNS(tipNameSpaceURI, "div") : document.createElement("div");
		tipContainer.setAttribute("id", tipContainerID);
	  document.getElementsByTagName("body").item(0).appendChild(tipContainer);
	}

	if (!document.getElementById) return;
	this.tip = document.getElementById (this.name);
	if (this.tip) document.onmousemove = function (evt) {tooltip.move (evt)};

	var a, sTitle, elements;
	
	var elementList = qTipTag.split(",");
	for(var j = 0; j < elementList.length; j++)
	{	
		elements = document.getElementsByTagName(elementList[j]);
		if(elements)
		{
			for (var i = 0; i < elements.length; i ++)
			{
				a = elements[i];
				sTitle = a.getAttribute("title");
                sClass = a.getAttribute("class");				
				if(sTitle && sClass != "noTip")
				{
					a.setAttribute("tiptitle", sTitle);
					a.removeAttribute("title");
					a.removeAttribute("alt");
					a.onmouseover = function() {tooltip.show(this.getAttribute('tiptitle'))};
					a.onmouseout = function() {tooltip.hide()};
				}
			}
		}
	}
}

tooltip.move = function (evt) {
	var x=0, y=0;
	if (document.all) {//IE
		x = (document.documentElement && document.documentElement.scrollLeft) ? document.documentElement.scrollLeft : document.body.scrollLeft;
		y = (document.documentElement && document.documentElement.scrollTop) ? document.documentElement.scrollTop : document.body.scrollTop;
		x += window.event.clientX;
		y += window.event.clientY;
		
	} else {//Good Browsers
		x = evt.pageX;
		y = evt.pageY;
	}
	if (this.tip) {    // musner - added to check for tip first in order to aviod JS null errors
	   this.tip.style.left = (x + this.offsetX) + "px";
	   this.tip.style.top = (y + this.offsetY) + "px";
	}
}

tooltip.show = function (text) {
	if (!this.tip) return;
	this.tip.innerHTML = text;
	this.tip.style.display = "block";
}

tooltip.hide = function () {
	if (!this.tip) return;
	this.tip.innerHTML = "";
	this.tip.style.display = "none";
}

window.onload = function () {
	tooltip.init ();
}

