
/**
 * Layered Window
 */
function LayeredWindow(title, body) {
	this.title = title;
	this.body = body;
}

LayeredWindow.prototype.show = function() {
	if(LayeredWindow.current) {
		LayeredWindow.current.close();
	}	
	if(!this.node) {			
		var html = '';
		html += '<div style="position:fixed;top:0;left:0;width:100%;height:100%;background-color:#686868;opacity:0.80;filter:alpha(opacity=80);z-index:998"></div>';
		html += '<table border="0" cellspacing="0" cellpadding="0" style="position:fixed;width:100%;height:100%;top:0;left:0;z-index:999">';
		html += '<tr><td align="center" valign="middle">';
		html += '<table border="0" cellspacing="0" cellpadding="0" style="border:5px solid silver; background-color:#fff"><tr>';
		html += '<td style="padding:6px;font-size:16px;font-weight:bold;">'+ this.title +'</td>';
		html += '<td align="right" style="padding:6px;"><a href="javascript:LayeredWindow.current.close()"><b>X</b></a></td>';
		html += '</tr><tr><td colspan="2" style="border-top:1px solid gray">';		
		html += this.body;
		html += '</td></tr></table>';
		html +='</td></tr></table>';
		
		this.node = jQuery(html);
	}
	LayeredWindow.current = this;
	jQuery(document.body).append(this.node);
}

LayeredWindow.prototype.close = function() {
	if(this.node) {
		this.node.remove();
		LayeredWindow.current = false;
	}
}


function ImgPreview(title, images) {
	this.images = images;
	this.title = title;
	this.currImage = 0;
	this.isInit = false;
	this.id = 'imgPrev_' + parseInt( Math.random() * 1000000 );
}

function getWinHeight() { // based on a script by projectseven.com (PVII)
	if (window.innerWidth) {
		h = window.innerHeight; // ns4
	} else if(document.body) {
		h = document.body.clientHeight;
		if (document.body.offsetHeight == h && document.documentElement && document.documentElement.clientHeight) {
			h = document.documentElement.clientHeight;
		}
	}
	return h;
}

ImgPreview.prototype.doInit = function() {	
	var height = getWinHeight();
	this.imgHeight = height - 100;
	var bh = height - 75;
	var bw = 1000;
	var b =	'<div id="'+ this.id +'"> ' +
				'<div style="background-color:#E6E6E6; padding-left:4px">' +
					'<a href="javascript:currImgPreview.prev();" style="font-family:Comic Sans MS; font-size:16px">&lt;&lt;&lt;</a>' +
					' <input name="currImage" type="text" value="1" style="width:35px" />/<span name="imgCount">'+ this.images.length +'</span>' +
					' <a href="javascript:currImgPreview.next();" style="font-family:Comic Sans MS; font-size:16px">&gt;&gt;&gt;</a>' +
					'&nbsp;&nbsp;&nbsp;' +
					'<a href="javascript:currImgPreview.sizeDown();">[-]</a><a href="javascript:currImgPreview.sizeUp();">[+]</a>' +
				'</div>' +
				'<div style="margin:0px; background-color:#656565;">' +
					'<center name="imgPreviewBox" style="padding:5px;overflow:auto;width:'+bw+';height:'+bh+';"></center>' +
				'</div>' +
			'</div>';	
	this.body = b;
}

ImgPreview.prototype.show = function() {
	if(!this.isInit) {
		this.doInit();
	}
	if(typeof this.title == 'function') {
		this.title = this.title();
	}
	this.lwin = new LayeredWindow(this.title, this.body);
	this.lwin.show();
	this.showImage(0);
}

ImgPreview.prototype.showImage = function(nImg) {
	if(nImg < 0 || nImg >= this.images.length) {
		return;
	}
	var self = this;
	var img = new Image();
	img.onload = function() {
		self.currImage = nImg;
		var box = jQuery('#' + self.id);
		var imgBox = box.find('[name="imgPreviewBox"]');
		var body = jQuery(document.body);
		var html = '<img class="previewImage" id="'+ self.id +'_'+ nImg +'" src="'+ self.images[nImg] +'" height="'+ (self.imgHeight) +'" style="cursor:move;position:relative;top:0px;left:0px" />';
		imgBox.html(html);
		box.find('[name="currImage"]').val(nImg + 1);
	};
	img.src = this.images[nImg];
}

ImgPreview.prototype.next = function() {
	this.showImage(this.currImage + 1);
}

ImgPreview.prototype.prev = function() {
	this.showImage(this.currImage - 1);
}

ImgPreview.prototype.doSize = function(val) {
	var img = jQuery('#' + this.id + '_' + this.currImage);
	var h = img.height();
	var newH = h + h * val / 100;
	img.height(newH);
	imgHeight = newH;
}

ImgPreview.prototype.sizeUp = function() {
	this.doSize(15);
}

ImgPreview.prototype.sizeDown = function() {
	this.doSize(-15);
}

var currImgPreview = false;

function imgPreview_show(title, images) {
	currImgPreview = new ImgPreview(title, images);
	currImgPreview.show();
}

function imgPreview_initPreview(parent, title, images) {
	var id = "fi_" + parseInt( Math.random() * 1000000 );
	var html = '<img class="smallPreviewImage" id="'+ id +'" src="'+ images[0] +'" style="width:100%; cursor:pointer" />';
	parent.html(html);
	jQuery(parent).find('#' + id).click(function() {
		imgPreview_show(title, images);
	});
}

function imgPreview_outputPreview(title, images) {
	var id = "fi_" + parseInt( Math.random() * 1000000 );
	var html = '<img class="smallPreviewImage" id="'+ id +'" src="'+ images[0] +'" style="width:100%; cursor:pointer" />';
	document.write(html);
	jQuery('#' + id).click(function() {
		imgPreview_show(title, images);
	});
}
