﻿function videoPlayerUtil(objName)
{
    this.objName = objName;
    this.showVideoInModal = showVideoInModal;
    this.drawEmbed = drawEmbed;
    this.getCenter = getCenter;
    this.closeModal = closeModal;
    this.modalCnt = 0;
    this.pWidth = 0;
    this.pHeight = 0;
    this.offSetTop = -10;
    this.offSetLeft = 5;
    this.modalID = new String();
    
    function showVideoInModal()
    {
        this.modalCnt++;
        this.pWidth = getClientWidthHeight()[0] - 50;
        this.pHeight = getClientWidthHeight()[1] - 50;

        this.modalID = "modal" + this.modalCnt;
        this.modalMaskID = "modalMask" + this.modalCnt;
        var newModal = document.createElement('div');
        var newModalMask = document.createElement('div');
        
        newModalMask.id = this.modalMaskID;
        newModalMask.style.width = getClientWidthHeight()[0] + "px";
        newModalMask.style.height = getClientWidthHeight()[1] + "px";
        newModalMask.style.position = "absolute";
        newModalMask.style.top = 0;
        newModalMask.style.left = 0;
        newModalMask.style.backgroundColor = "transparent";

        newModal.id = this.modalID;
        newModal.style.height = this.pHeight+"px";
        newModal.style.width = this.pWidth+"px";
        newModal.style.position = "absolute";
        newModal.style.backgroundColor = "#cccccc";
        if (document.all) {
            newModal.style.top = this.getCenter(this.pWidth, this.pHeight)[1] + this.offSetTop;
        } else {
            newModal.style.top = 0;
        }
        newModal.style.left = this.getCenter(this.pWidth, this.pHeight)[0] + this.offSetLeft;
        
        //document.body.appendChild(newModalMask);
        document.body.appendChild(newModal);

        newModal.innerHTML = this.drawEmbed(this.modalID,this.modalMaskID);  //+ "width:" + this.pWidth + "<br>height:" + this.pHeight + "<br>" + this.videoID;

        document.getElementById("playerContainer").style.display = "none";
    }

    function drawEmbed(mID,mMaskID)
    {
        var embdStr = new String("<div align='right' style='padding:5px;'><a href=javascript:" + this.objName + ".closeModal('" + mID + "','" + mMaskID + "') style='font-family:arial;margin-right:5px;text-decoration:none;'>Back To All Videos&nbsp;&nbsp;&gt;</a></div>");
        embdStr += "<object width='" + this.pWidth + "' height='" + this.pHeight + "'>";
        embdStr += "<param name='movie' value='"+this.videoID+"&autoplay=0&rel=0&showinfo=0'/>";
        embdStr += "<param name='wmode' value='transparent'/>";
	    embdStr += "<param name='allowFullScreen' value='true'/>";
        embdStr += "<embed src='"+this.videoID+"&autoplay=0&rel=0&showinfo=0' ";
        embdStr += "type='application/x-shockwave-flash' width='"+this.pWidth+"' height='"+this.pHeight+"'";
	    embdStr += " allowFullScreen='true' wmode='transparent'/>";
        embdStr += "</object>";

        return embdStr;
    }

    function closeModal(m,mMask) {
        activeModalID = new String(m);
        activeModalMaskID = new String(mMask);
        document.getElementById(activeModalID).style.display = "none";
        //document.getElementById(activeModalMaskID).style.display = "none";
        document.body.removeChild(document.getElementById(activeModalID));
        //document.body.removeChild(document.getElementById(activeModalMaskID));
        document.getElementById("playerContainer").style.display = "";
    }
    
    function getCenter(w,h)
    {
        coords = new Array();

        cw = getClientWidthHeight()[0];
        ch = getClientWidthHeight()[1];
        coords[0] = (cw/2) - (w / 2);
        coords[1] = (ch/2) - (h / 2);
        return coords;
    }

    function getClientWidthHeight() {

        whArr = new Array();
        
        var cliWidth =
		document.documentElement && document.documentElement.clientWidth ||
		document.body && document.body.clientWidth ||
		document.body && document.body.parentNode && document.body.parentNode.clientWidth ||
		0;
		
        var cliHeight =
		document.documentElement && document.documentElement.clientHeight + document.documentElement.scrollTop ||
		document.body && document.body.clientHeight + document.body.scrollTop ||
  		document.body && document.body.parentNode && document.body.parentNode.clientHeight + document.body.parentNode.scrollTop ||
  		0;
  		
        whArr[0] = cliWidth;
        whArr[1] = cliHeight;
        
        return whArr;
    }
}