function showRyMacBox(id) {
	document.getElementById(id).className = "rymacbox-main";
	calculatePosition(id);
	
	//hide any flash elements so they dont overlay the macbox
	hideFlash(true);
}

function hideFlash(hide)
{
	var displayMode = "";
	if (hide) displayMode = "none";
	else displayMode = "block";
	var eles = document.getElementsByTagName("embed");
	if (eles.length > 0)
	{
		for (var idx = 0, len = eles.length; idx < len; idx++)
		{
			eles[idx].style.display = displayMode;
		}
	}
}

function isdefined(variable)
{
    return (typeof(window[variable]) == "undefined")?  false: true;
}

function calculatePosition(id) {

    var macboxEle = document.getElementById(id);

    //left position
    macboxEle.style.left = "50%";
    var width = parseInt(macboxEle.style.width.split('p')[0]);
	macboxEle.style.marginLeft = -(width/2) + "px";
	
	//top position
	macboxEle.style.top = document.documentElement.scrollTop + "px";
}

function hideRyMacBox(id) {
	document.getElementById(id).className = "rymacbox-main rymacbox-hidden";
	hideFlash(false);
}

function RyMacBox(name, image, width, height, outputToEle, attributesObj) {

	// Required //
	//////////////

	this.image = image;
	this.image.isHtml = false;
	//this.output = outputToEle;
	this.output = $("body").get(0);
	this.name = name;

	this.width = width;	// Width in pixels
	this.height = height;	// Height in pixels

	if (typeof(this.image) != "object") {
		var bits = this.image.split("|");

		if ((bits[0] == "swf")||(bits[0] == "SWF")) {
			var src = bits[1];
	
			this.image = {};
			this.image.src = src;
			this.image.isFlash = true;
		}
		else if ((bits[0] == "html")||(bits[0] == "HTML")) {
		
			var DATA = document.getElementById(bits[1]).innerHTML;
	
			this.image = {};
			this.image.data = DATA;
			this.image.origEle = document.getElementById(bits[1]);
			this.image.isFlash = false;		
			this.image.isHtml = true;
		}
	}

	// Optional //
	//////////////

	this.oncloseFunc = "";
	this.onopenFunc = function() { };	
	
	this.caption = "";	// Caption

	// If extra attributes have been passed in
	if (typeof(attributesObj) == "object") {
		if (attributesObj.caption != undefined) {	this.caption = attributesObj.caption; }
		if (attributesObj.onclose != undefined) {	this.oncloseFunc = attributesObj.onclose; }
		if (attributesObj.onopen != undefined) {	this.onopenFunc = attributesObj.onopen; }		
	}

	var closedRef = this;
	setTimeout(function() { closedRef.init(); }, 500);
}

RyMacBox.prototype.show = function()
{
    document.getElementById(this.name).className = "rymacbox-main";
	calculatePosition(this.name);
	
	//hide any flash elements so they dont overlay the macbox
	hideFlash(true);
}

RyMacBox.prototype.hide = function()
{
    document.getElementById(this.name).className = "rymacbox-main rymacbox-hidden";
	hideFlash(false);
}

RyMacBox.prototype.init = function() {

	var rymacboxMainContainer = document.createElement("div");
	rymacboxMainContainer.id = this.name;
	rymacboxMainContainer.className = "rymacbox-main rymacbox-hidden";
	rymacboxMainContainer.style.width = (this.width + 52) + "px";
	rymacboxMainContainer.style.zIndex = 70000;

    if (this.image.isHtml)
    {
        rymacboxMainContainer.style.left = this.output.offsetLeft + (Math.floor(this.output.offsetWidth / 2) - Math.floor((this.width + 52) / 2)) + "px";
        rymacboxMainContainer.style.top = this.output.offsetTop;
    }
    else
    {
    	rymacboxMainContainer.style.left = this.output.offsetLeft + (Math.floor(this.output.offsetWidth / 2) - Math.floor((this.width + 52) / 2)) + "px";
	    rymacboxMainContainer.style.top = this.output.offsetTop +(Math.floor(this.output.offsetHeight / 2) - Math.floor((this.height + 85) / 2)) + "px";
	}

		var rymacboxContainer = document.createElement("div");
		rymacboxContainer.id = this.name + "DivContainer";
		rymacboxContainer.className = "rymacbox-container";
		rymacboxContainer.style.width = (this.width + 52) + "px";

			var rymacboxCloseLink = document.createElement("a");
			rymacboxCloseLink.className = "rymacbox-close";
			if (rymacboxCloseLink != null)
			{
			    rymacboxCloseLink.innerHTML = "close";
			}
			rymacboxCloseLink.href = "javascript: hideRyMacBox('" + this.name + "'); " + this.oncloseFunc;
			if (rymacboxContainer != null)
			{
			    rymacboxContainer.appendChild(rymacboxCloseLink);
			}

			var rymacboxRight = document.createElement("div");
			rymacboxRight.id = this.name + "DivRight";
			rymacboxRight.className = "rymacbox-right";
			if (rymacboxContainer != null)
			{
			    rymacboxContainer.appendChild(rymacboxRight);
			}

				var rymacboxContent = document.createElement("div");
				rymacboxContent.id = this.name + "DivContent";
				rymacboxContent.className = "rymacbox-content";

                    if (this.image.isHtml)
                    {
                        // Remove the original node
                        this.image.origEle.parentNode.removeChild(this.image.origEle);
                        
                        if (rymacboxContent != null)
                        {
                            rymacboxContent.innerHTML = this.image.data;
                        }
                    }
                    else
                    {
					    var rymacboxImage = document.createElement("img");
					    rymacboxImage.id = this.name + "_image";

					    if (!this.image.isFlash) {
						    rymacboxImage.src = this.image.src;
						    rymacboxImage.alt = "";
					    }
                        
                        if (rymacboxContent != null)
                        {
					        rymacboxContent.appendChild(rymacboxImage);
					    }

					    var rymacboxCaption = document.createElement("p");
					    if (rymacboxCaption != null)
					    {
					        rymacboxCaption.innerHTML = this.caption;
					    }
					    if (rymacboxContent != null)
					    {
					        rymacboxContent.appendChild(rymacboxCaption);
					    }
					}
	
	            if (rymacboxRight != null)
	            {
				    rymacboxRight.appendChild(rymacboxContent);
				}
	        if (rymacboxContainer != null)
	        {
			    rymacboxContainer.appendChild(rymacboxRight);
			}
        if (rymacboxMainContainer != null)
        {
		    rymacboxMainContainer.appendChild(rymacboxContainer);
		}

		var rymacboxFooter = document.createElement("div");
		rymacboxFooter.className = "rymacbox-footer";
		rymacboxFooter.style.width = (this.width + 74) + "px";

			var rymacboxFooterRight = document.createElement("div");
			rymacboxFooterRight.className = "rymacbox-footerright";
			if (rymacboxFooter != null)
			{
			    rymacboxFooter.appendChild(rymacboxFooterRight);
			}
        if (rymacboxMainContainer != null)
        {
		    rymacboxMainContainer.appendChild(rymacboxFooter);
		}
    if (this.output != null)
    {	
	    this.output.appendChild(rymacboxMainContainer);
	}
	
	if (this.image.isFlash) {
		swfobject.embedSWF("images/test.swf", this.name + "_image", this.width, this.height, "9.0.0");  
	}
	
	//check here to see if the browser requested for the add review macbox to show
	var qs = window.location.search.substring(1);
	for (var idx = 0, len = qs.length; idx < len; idx++)
	{
	    if (idx + 3 > len) break;
	    var curBit = qs.substr(idx, 3)
	    if (curBit == "r=1") 
	    {
	        if (this.name == "AddReview")
	        {
	            this.show();
	        }
	    }
	}
}
