﻿var TectureBox = {

	_caption: undefined,
	_src: undefined,
	_windowHeight: undefined,
	_windowWidth: undefined,
	_flashPath: undefined,

	getType: function() {        
		switch(TectureBox._src.substring(TectureBox._src.lastIndexOf('.'))) {
			case ".jpg":
				return "image";
				break;
			case ".bmp":
				return "image";
				break;
			case ".gif":
				return "image";
				break;   
			case ".png":
				return "image";
				break;  
			case ".swf":
				return "flash";
				break;
			default:
				return "html";
				break;                                           
		}
	},

	remove: function() {
		$("#tWindow .header img").fadeOut(500);
		$("#tWindow .caption").css("visibility", "hidden");
		$("#tWindow .content").empty();
		$("#tWindow").slideUp(500, function() {
			$(this).remove();
			$("#overlay").fadeOut(500, function() {
				$(this).remove();
			});
		});
	},

	render: function(src, windowWidth, windowHeight, caption, flashPath) {
		var ratio = windowWidth / windowHeight;
		var wH = $(window).height() - 120;

		if (windowHeight > wH) {
			windowHeight = wH;
			windowWidth = parseInt(wH * ratio);
		}      

		this._src = src;
		this._caption = (caption != undefined) ? caption : undefined;
		this._windowHeight = isNaN(parseInt(windowHeight)) ? 0 : parseInt(windowHeight);
		this._windowWidth = isNaN(parseInt(windowWidth)) ? 0 : parseInt(windowWidth);   
		this._flashPath = (flashPath != undefined) ? flashPath : undefined;

		$("body").append('<div id="overlay" style="filter:alpha(opacity=50);">&#160;</div>');                           

		$("#overlay").fadeIn(500, function() {            
			$(this).click(function() {
				TectureBox.remove();                
			});

			var bw = '<div id="tWindow"><div class="header"><img src="images/closelabel.gif" /></div><div class="content"></div><div class="caption"></div></div>';
			$("body").append(bw);

			$("#tWindow").show();
			$("#tWindow").animate({
				height: (TectureBox._windowHeight + 22) + "px",
				marginTop: "-" + (parseInt(TectureBox._windowHeight/2) + 21) + "px"
			}, 600, function() {
				$("#tWindow").animate({
					marginLeft: "-" + parseInt(TectureBox._windowWidth/2) + "px",
					width: TectureBox._windowWidth + "px"
				}, 600, function() {     

					$("#tWindow .content").css({
						height: TectureBox._windowHeight+"px",
						width: TectureBox._windowWidth+"px"
					});          

					if (!(!TectureBox._caption)) {
						$("#tWindow .caption").html(TectureBox._caption).show();
						TectureBox._windowHeight += $("#tWindow .caption").height() + 10;
						$("#tWindow").animate({
							height: (TectureBox._windowHeight + 22) + "px",
							marginTop: "-" + (parseInt(TectureBox._windowHeight/2) + 21) + "px"
						}, 600, function() {
							$("#tWindow .caption").css("visibility", "visible"); 
							TectureBox._windowHeight -= $("#tWindow .caption").height() + 10;
							TectureBox.replaceContent();
						});
					} else {
						TectureBox.replaceContent();
					}

				}); 
			});
		});
	},

	replaceContent: function() {
		switch(TectureBox.getType()) {
			case "flash":
				$("#tWindow .content").html('<object type="application/x-shockwave-flash" ' + (TectureBox._flashPath != undefined ? 'flashvars="importedPath='+TectureBox._flashPath+'"' : '') + ' data="' + TectureBox._src +'" width="' + TectureBox._windowWidth +'" height="' + TectureBox._windowHeight + '"><param name="movie" value="' + TectureBox._src +'" /></object>');
				break;
			case "image":
				$("#tWindow .content").html('<img src="' + TectureBox._src + '" height="' + TectureBox._windowHeight + 'px" width="' + TectureBox._windowWidth + 'px" />');
				break;
			case "html":
				$.get("/ajaxLoad/?url="+TectureBox._src, function(res) {
					$("#tWindow .content").html(res);
				});
				break;
		}
		$("#tWindow .header img").click(function() {
			TectureBox.remove();
		}).fadeIn(500);     
	}

}
