var CardBox = function() {

	this.dimensions = new Array();
	this.size = new Array();
	this.size[0] = "640px";
	this.size[1] = "480px";

	this.uri = "";
		
	this.closeButton = true;

	if (undefined != document.getElementById("cardbox_outer")) {

		var dd = document.getElementById("cardbox_outer");
		document.body.removeChild(dd);
	}

		var bgN = new Element("div");
		bgN.className = "cardbox_bg";
		bgN.className += " cardbox_bg_n";
			
		var bgNE = new Element("div");
		bgNE.className = "cardbox_bg";
		bgNE.className += " cardbox_bg_ne";
			
		var bgE = new Element("div");
		bgE.className = "cardbox_bg";
		bgE.className += " cardbox_bg_e";
			
		var bgSE = new Element("div");
		bgSE.className = "cardbox_bg";
		bgSE.className += " cardbox_bg_se";
			
		var bgS = new Element("div");
		bgS.className = "cardbox_bg";
		bgS.className += " cardbox_bg_s";
			
		var bgSW = new Element("div");
		bgSW.className = "cardbox_bg";
		bgSW.className += " cardbox_bg_sw";
		
		var bgW = new Element("div");
		bgW.className = "cardbox_bg";
		bgW.className += " cardbox_bg_w";
		
		var bgNW = new Element("div");
		bgNW.className = "cardbox_bg";
		bgNW.className += " cardbox_bg_nw";

		var cardboxBg = new Element("div");
		cardboxBg.id = "cardbox_bg";

			cardboxBg.appendChild(bgN);
			cardboxBg.appendChild(bgNE);
			cardboxBg.appendChild(bgE);
			cardboxBg.appendChild(bgSE);
			cardboxBg.appendChild(bgS);
			cardboxBg.appendChild(bgSW);
			cardboxBg.appendChild(bgW);
			cardboxBg.appendChild(bgNW);
		
		this.Box_DOM_Frame = new Element("iframe");
		this.Box_DOM_Frame.id = "cardbox_frame";
		this.Box_DOM_Frame.style.display = "inline";
		this.Box_DOM_Frame.src = "";
		this.Box_DOM_Frame.setAttribute("hspace","0");
		this.Box_DOM_Frame.setAttribute("frameborder","0");
		this.Box_DOM_Frame.setAttribute("border","no");

		this.Box_DOM_CloseButton = new Element("div");
		this.Box_DOM_CloseButton.id = "cardbox_close";
		this.Box_DOM_CloseButton.style.display = "block";
		
		this.Box_DOM_Content = new Element("div");
		this.Box_DOM_Content.id = "cardbox_content";
		
			this.Box_DOM_Content.appendChild(this.Box_DOM_Frame);
		
		this.Box_DOM_Element = new Element("div");
		this.Box_DOM_Element.id = "cardbox_outer";
		this.Box_DOM_Element.style.top = ( window.pageYOffset ? window.pageYOffset : ( document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop ) ) + "px";
		this.Box_DOM_Element.style.display = "none";

			document.body.appendChild(this.Box_DOM_Element);

		var cardboxInner = new Element("div");
		cardboxInner.id = "cardbox_inner";

			this.Box_DOM_Element.appendChild(cardboxInner);
			cardboxInner.appendChild(this.Box_DOM_CloseButton);
			cardboxInner.appendChild(cardboxBg);
		
		var cardboxLeftIcon = new Element("span");
		cardboxLeftIcon.id = "cardbox_left_ico";
		cardboxLeftIcon.className = "cardbox_ico";
		
		var cardboxRightIcon = new Element("span");
		cardboxRightIcon.id = "cardbox_right_ico";
		cardboxRightIcon.className = "cardbox_ico";
		
		var cardboxLeft = new Element("a");
		cardboxLeft.id = "cardbox_left";
		cardboxLeft.style.display = "none";
		
		var cardboxRight = new Element("a");
		cardboxRight.id = "cardbox_right";
		cardboxRight.style.display = "none";
		
			cardboxLeft.appendChild(cardboxLeftIcon);
			cardboxRight.appendChild(cardboxRightIcon);
			
			cardboxInner.appendChild(cardboxLeft);
			cardboxInner.appendChild(cardboxRight);
			cardboxInner.appendChild(this.Box_DOM_Content);
		
		this.Box_DOM_TitleContainer = new Element("div");
		this.Box_DOM_TitleContainer.id = "cardbox_title";
		this.Box_DOM_TitleContainer.style.display = "none";
		
			cardboxInner.appendChild(this.Box_DOM_TitleContainer);
		
		var cardboxTitleTable = new Element("table");
		cardboxTitleTable.border = "0";
		cardboxTitleTable.setAttribute("cellpadding","0");
		cardboxTitleTable.setAttribute("cellspacing","0");
		
			this.Box_DOM_TitleContainer.appendChild(cardboxTitleTable);
		
		var cardboxTitleTbody = new Element("tbody");
		
			cardboxTitleTable.appendChild(cardboxTitleTbody);
			
		var cardboxTitleTr = new Element("tr");
		
			cardboxTitleTbody.appendChild(cardboxTitleTr);
		
		var cardboxTitleTd1 = new Element("td");
		cardboxTitleTd1.id = "cardbox_title_left";
		cardboxTitleTd1.className = "cardbox_title";
		
		var cardboxTitleTd2 = new Element("td");
		cardboxTitleTd2.id = "cardbox_title_main";
		cardboxTitleTd2.className = "cardbox_title";
		
		var cardboxTitleTd3 = new Element("td");
		cardboxTitleTd3.id = "cardbox_title_right";
		cardboxTitleTd3.className = "cardbox_title";
		
			cardboxTitleTr.appendChild(cardboxTitleTd1);
			cardboxTitleTr.appendChild(cardboxTitleTd2);
			cardboxTitleTr.appendChild(cardboxTitleTd3);
		
		this.Box_DOM_Title = new Element("div");
		
			cardboxTitleTd2.appendChild(this.Box_DOM_Title);
	/*}
	else {

		this.Box_DOM_Frame = document.getElementById("cardbox_frame");
		this.Box_DOM_CloseButton = document.getElementById("cardbox_close");
		this.Box_DOM_Content = document.getElementById("cardbox_content");
		this.Box_DOM_Element = document.getElementById("cardbox_outer");
		this.Box_DOM_TitleContainer = document.getElementById("cardbox_title");
		this.Box_DOM_Title = document.getElementById("cardbox_title").childNodes[0].childNodes[0].childNodes[0].childNodes[1].childNodes[0];
	}*/
};
	
	CardBox.prototype.getValue = function(value) {

		if ("number" == typeof(value)) {

			return (value + "px");
		}
		else {
		
			return value;
		}
	};
	
	CardBox.prototype.setDimensions = function(top,right,bottom,left) {

		this.dimensions[0] = this.getValue(top);
		this.dimensions[1] = this.getValue(right);
		this.dimensions[2] = this.getValue(bottom);
		this.dimensions[3] = this.getValue(left);
	};
	
	CardBox.prototype.setSize = function(width,height) {
	
		this.size[0] = this.getValue(width);
		this.size[1] = this.getValue(height);
	};
	
	CardBox.prototype.setTarget = function(uri) {

		this.uri = uri;
	};
	
	CardBox.prototype.setTitle = function(title) {

		this.Box_DOM_TitleContainer.style.display = "block";
		this.Box_DOM_Title.innerHTML = title;
	};
	
	CardBox.prototype.setCloseButton = function(value) {
	
		this.closeButton = ( true == value ? true : false );
	};

	CardBox.prototype.setScrolling = function(value) {
	
		this.Box_DOM_Frame.scrolling = ( value ? "auto" : "no"  );
		this.Box_DOM_Frame.style.overflow = ( value ? "auto" : "hidden"  );
	};
	
	CardBox.prototype.open = function() {

		var isIE6 = CardBox.isIE6();

		this.Box_DOM_Content.style.top = ( isIE6 ? "0" : "10" ) + "px";
		this.Box_DOM_Content.style.right = ( isIE6 ? "0" : "10" ) + "px";
		this.Box_DOM_Content.style.bottom = ( isIE6 ? "0" : "10" ) + "px";
		this.Box_DOM_Content.style.left = ( isIE6 ? "0" : "10" ) + "px";

		this.Box_DOM_Element.style.width = this.size[0];
		this.Box_DOM_Element.style.height = this.size[1];

		this.Box_DOM_Element.style.left = ((document.body.clientWidth - document.getElementById(this.Box_DOM_Element.id).getWidth()) / 2) + "px";

		this.Box_DOM_Frame.src = this.uri;
		
		this.Box_DOM_CloseButton.style.display = ( this.closeButton ? "block" : "none"  );

		document.getElementById("cardbox_close").onclick = function() {

			CardBox.close();
		};

		var dark = new Element("div");
		dark.id = "cardbox_background";
		document.body.appendChild(dark);

		if (CardBox.isIE()) {
		
			this.Box_DOM_Element.style.display = "block";

			if (isIE6) {

				this.Box_DOM_Content.style.margin = "10px";

				this.Box_DOM_Content.style.width = this.Box_DOM_Content.offsetWidth-20;
				this.Box_DOM_Content.style.height = this.Box_DOM_Content.offsetHeight-20;
			}
		}
		else {
		
			CardBox.effect("open", this.Box_DOM_Element);
		}
	};

	CardBox.isIE6 = function() {
	
		return (navigator.userAgent.indexOf("MSIE 6") > -1 ? true : false);
	};
	
	CardBox.isIE = function() {
	
		return (navigator.userAgent.indexOf("MSIE") > -1 ? true : false);
	};
	
	CardBox.close = function() {

		if (CardBox.isIE()) {
		
			document.getElementById("cardbox_outer").style.display = "none";
		}
		else {
		
			CardBox.effect("close", document.getElementById("cardbox_outer"));
		}

		document.body.removeChild(document.getElementById("cardbox_background"));
	};
	
	CardBox.effect = function(action,element) {

		if ("open" == action) {
		
			new Effect.Appear(element);
		}
		else if ("close" == action) {
		
			new Effect.Fade(element);
		}
	};

	document.observe("dom:loaded",onReady);

	// {{{ Scan for appropriate elements

	function onReady() {

		var elements = document.getElementsByTagName("a");

		for (var i in elements) {

			if (undefined != elements[i].tagName && "a" == elements[i].tagName.toLowerCase() && "cardbox" == elements[i].rel.toLowerCase()) {

				elements[i].onclick = function(t) {

					var cardbox = new CardBox();

					if (undefined != typeof(this.rev) && "" != this.rev) {

						var indexSeparator = this.rev.indexOf(",");
	
						cardbox.setSize(parseInt(this.rev.substring(0,indexSeparator)),parseInt(this.rev.substring(indexSeparator+1)));
	
						delete(indexSeparator);
					}
					
					if (undefined != typeof(this.title) && "" != this.title) {
				
						cardbox.setTitle(this.title);
					}
	
					cardbox.setTarget(this.href);
					cardbox.setCloseButton(false);
					cardbox.setScrolling(false);
					
					cardbox.open();
					
					return false;
				};
			}
		}
	}
	
	// }}}
