$(window).bind('resize', function() {
	var src = $('#popupimg').attr('src');
	if (src != undefined){
		closeall();
		popupSingle(inputarray, currentPos);
	}
});
var currentPos = 0;
var inputarray;
function popupSingle(ia, pos){
	var isIE = (navigator.appVersion.indexOf("MSIE") != -1) ? true : false;
	inputarray = ia;
	currentPos = pos;
	var appendcontent = "<div id='popupmain'><div id='popuplayout'></div>";
	appendcontent += "<div id='popupimage'>";
	appendcontent += "<div id='popuploadingdiv'><img src='/bin/jscript/imagebox/images/lightbox-ico-loading.gif' /></div>";
	appendcontent += "<div id='popupimgdiv'><img id='popupimg' src='' /></div>";
	appendcontent += "</div>";
	appendcontent += "<div id='popupbar'>";
	appendcontent += "<div id='popupzoomin'><img src='/bin/jscript/image/zoomin.gif' /></div>";
	appendcontent += "<div id='popupzoomout'><img src='/bin/jscript/image/zoomout.gif' /></div>";
	appendcontent += "<div id='popupprev'><img src='/bin/jscript/image/prev.gif' /></div>";
	appendcontent += "<div id='popupnext'><img src='/bin/jscript/image/next.gif' /></div>";
	appendcontent += "</div>";
	appendcontent += "<div id='popupclose'><img src='/bin/jscript/image/close.gif' /></div>";
	appendcontent += "</div>";
	
	$('body').append(appendcontent);
	$("body").css("overflow", "hidden");

	var arrPageSizes = ___getPageSize();

	if (isIE){
		$('#popupmain').css({
			position:			'absolute',
			zIndex:				5,
			overflow:			'hidden',
			top:				$(document).scrollTop(),
			left:				$(document).scrollLeft(),
			height:				arrPageSizes[3],
			width:				arrPageSizes[2]
		}).fadeIn().bind('mousewheel', function(event, delta) {
			if (delta > 0 ){
				zoomin();
			}else{
				zoomout();
			}
			return false;
		});
		$(window).scroll(function () {
			$('#popupmain').css("top",$(document).scrollTop()).css("left",$(document).scrollLeft());
		});
	}else{
		$('#popupmain').css({
			position:			'fixed',
			zIndex:				5,
			overflow:			'hidden',
			top:				0,
			left:				0,
			height:				arrPageSizes[3],
			width:				arrPageSizes[2]
		}).fadeIn().bind('mousewheel', function(event, delta) {
			if (delta > 0 ){
				zoomin();
			}else{
				zoomout();
			}
			return false;
		});
	}
	$('#popuplayout').css({
		position:			'absolute',
		zIndex:				10,
		top:				0,
		left:				0,
		backgroundColor:	'#000000',
		opacity:			0.8,
		height:				arrPageSizes[3],
		width:				arrPageSizes[2]
	}).fadeIn();
	$('#popupimage').css({
		position:			'absolute',
		zIndex:				20,
		top:				(arrPageSizes[3] - 32 - 80) / 2,
		left:				(arrPageSizes[2] - 32) / 2
	}).fadeIn().draggable({
	});
	$('#popupbar').css({
		position:			'absolute',
		zIndex:				30,
		top:				arrPageSizes[3] - 80,
		left:				0,
		backgroundColor:	'#000000',
		opacity:			0.9,
		height:				80,
		width:				arrPageSizes[2]
	}).fadeIn();
	$('#popupzoomin').css({
		position:			'absolute',
		cursor:				'pointer',
		zIndex:				31,
		top:				20,
		left:				arrPageSizes[2] / 2 - 20 - 32
	}).fadeIn().click(function () {
		zoomin();
	});
	$('#popupzoomout').css({
		position:			'absolute',
		cursor:				'pointer',
		zIndex:				32,
		top:				20,
		left:				arrPageSizes[2] / 2 + 20
	}).fadeIn().click(function () {
		zoomout();
	});
	$('#popupprev').css({
		position:			'absolute',
		cursor:				'pointer',
		zIndex:				33,
		top:				20,
		left:				arrPageSizes[2] / 2 -20 -20 -43 - 43
	}).fadeIn().click(function () {
		setpos(32, 32, (arrPageSizes[2] - 32) / 2, (arrPageSizes[3] - 32 - 80) / 2);
		$('#popupimgdiv').hide();
		$('#popuploadingdiv').show();
		
		currentPos--;
		if (currentPos < 0)
			currentPos = inputarray.length - 1;
		objimg.src = inputarray[currentPos];
	});
	$('#popupnext').css({
		position:			'absolute',
		cursor:				'pointer',
		zIndex:				34,
		top:				20,
		left:				arrPageSizes[2] / 2 + 20 + 43+30
	}).fadeIn().click(function () {
		setpos(32, 32, (arrPageSizes[2] - 32) / 2, (arrPageSizes[3] - 32 - 80) / 2);
		$('#popupimgdiv').hide();
		$('#popuploadingdiv').show();
		
		currentPos++;
		if (currentPos > inputarray.length - 1)
			currentPos = 0;
		objimg.src = inputarray[currentPos];
	});
	$('#popupclose').css({
		position:			'absolute',
		cursor:				'pointer',
		zIndex:				33,
		top:				20,
		left:				arrPageSizes[2] -42 -20
	}).fadeIn().click(function () {
		closeall();
	});
	$('#popuploadingdiv').show();
	$('#popupimgdiv').hide();
	if (inputarray.length == 1){
		$('#popupprev').hide();
		$('#popupnext').hide();
	}
	
	var objimg = new Image();
	objimg.onload = function() {
		$('#popupimgdiv').hide();
		$('#popuploadingdiv').show();
		
		var _v = objimg.width;
		var _h = objimg.height;
		
		var _finH = arrPageSizes[3] - 80;
		var _finW = _v / _h * (arrPageSizes[3] - 80);

		setpos(_finW, _finH, ((arrPageSizes[2] - _finW) / 2), ((arrPageSizes[3] - _finH) / 2));

		$('#popupimg').attr('src',objimg.src);
		$('#popupimgdiv').show();
		$('#popuploadingdiv').hide();
	}
	objimg.src = inputarray[currentPos];
}
function zoomin(){
	var arrPageSizes = ___getPageSize();
	var _v = $('#popupimg').width();
	var _h = $('#popupimg').height();
	_v = _v * 1.5;
	_h = _h * 1.5;
	setpos(_v, _h, ((arrPageSizes[2] - _v) / 2), ((arrPageSizes[3] - _h) / 2));
}
function zoomout(){
	var arrPageSizes = ___getPageSize();
	var _v = $('#popupimg').width();
	var _h = $('#popupimg').height();
	_v = _v / 1.5;
	_h = _h / 1.5;
	if (_v < 10 || _h < 10){
		_v = 10;
		_h = 10;
	}
	setpos(_v, _h, ((arrPageSizes[2] - _v) / 2), ((arrPageSizes[3] - _h) / 2));
}
function closeall(){
	$('#popupmain').remove();
	$("body").css("overflow", "auto");
}
function setpos(_w, _h , _l,_t){
	$('#popupimg').css({
		width:		_w,
		height:		_h
	 });
	$('#popupimage').css({
		width:		_w,
		height:		_h,
		top:		_t,
		left:		_l	
	});
}
function ___getPageSize() {
	var xScroll, yScroll;
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}
	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}
	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
	return arrayPageSize;
};
