//==================== Functions for zooming thumbnail images ====================
var fzooming=false;
var iThmbW;
var iThmbH;
var iStartzoomfrom;
var storedAltTxt;
var storedTitleTxt;
var zIndexNumber=999 //workaround for IE (bug causes poor z-indexing)
$(document).ready(function() {

	//Set body to kill zoomed popup when clicking away
	$("body").click(function() {
		if(!fzooming && $('#zoomthmbimg').length>0)blurzoomthmbimg('');
	});

	//Apply new onClick event to all items with zoom thumb class
	$(".ZoomThumb").click(function() {
		if(!$('#zoomthmbimg').length>0) focuszoomthmbimg($(this))
	});
		
	//Add zoom icon to each image
	$(".ZoomThumb").each(function() {
		//Check is image and load
		if($(this).attr("src")){ //Check is an image
			var imgsrc = $(this).attr("src").toLowerCase();
			if(imgsrc.length>0 && imgsrc.indexOf('/_systhumbs')>0) {
				var sFullSizeImgSrc = imgsrc.replace("/_systhumbs","")
				sFullSizeImgSrc = sFullSizeImgSrc.substring(0,sFullSizeImgSrc.length-4)
				sFullSizeImgSrc = sFullSizeImgSrc.substring(0,sFullSizeImgSrc.length-3)+"."+sFullSizeImgSrc.substring(sFullSizeImgSrc.length-3)
				var oImg = new Image();
				oImg.src = sFullSizeImgSrc;
				oImg.load;
				//Add Icon
				$(this).css('zIndex', zIndexNumber); //workaround for IE (bug causes poor z-indexing)
				zIndexNumber -= 10; //workaround for IE (bug causes poor z-indexing)
				$(this).wrap("<span style='width:"+$(this).width()+"px; position:relative;"+$(this).attr("style")+"' class='thmbwrapper'></span>")
				$(this).removeAttr("style");
				$(".thmbwrapper").append("<img style='filter:alpha(opacity=20); opacity:0.20; position:absolute; bottom:1px; right:0px;' src='/control_GLOBAL/shared/images/enlarge_icon.gif'>");
			}
		}
	});
	
	//Add mouseover event
	$(".ZoomThumb").mouseover(function() {
	//	if($(this).attr("alt")!="Click to Enlarge"){
			storedAltTxt=$(this).attr("alt"); $(this).attr("alt","Click to Enlarge");
			storedTitleTxt=$(this).attr("title"); $(this).attr("title","Click to Enlarge");
	//	}
		$(this).next("img").animate({opacity: 0.75},300);
	});
	//Add mouseout event
	$(".ZoomThumb").mouseout(function() {
	//	if($(this).attr("alt")=="Click to Enlarge"){
			$(this).attr("alt",storedAltTxt); storedAltTxt="";
			$(this).attr("title",storedTitleTxt); storedTitleTxt="";
	//	}
		$(this).next("img").animate({opacity: 0.20},300);
	});
	
});

function focuszoomthmbimg(obj) {
	//obj.find(".zoomtoggle").css({opacity:0});
	var pos = obj.position();
	if(obj.attr("src")){ //Check is an image
		iThmbW = obj.width(); iThmbH = obj.height();
		var imgsrc = obj.attr("src").toLowerCase();
		var fIsThumb = imgsrc.indexOf('/_systhumbs')>0;
		if(imgsrc.length>0 && fIsThumb) {
			sFullSizeImgSrc = imgsrc.replace("/_systhumbs","")
			sFullSizeImgSrc = sFullSizeImgSrc.substring(0,sFullSizeImgSrc.length-4)
			sFullSizeImgSrc = sFullSizeImgSrc.substring(0,sFullSizeImgSrc.length-3)+"."+sFullSizeImgSrc.substring(sFullSizeImgSrc.length-3)
			shtml=	"<div id='zoomthmbimg' class='wsExpandThumbContainer' style='filter:alpha(opacity=10); opacity:0.1; z-index:100; position:absolute; top:"+pos.top+"; left:"+pos.left+";'>";
			shtml+=	"	<table class='wsExpandThumbImage' cellpadding='0' cellspacing='0' onClick='blurzoomthmbimg(null);'>";
			shtml+=	"		<tr valign='top'><td><img style='filter:alpha(opacity=10);opacity:0.1;' src='"+sFullSizeImgSrc+"'></td>";
			if(storedAltTxt.length>0 || storedTitleTxt.length>0) { //Add alt text as txt for image on right
				if(storedTitleTxt.length<31){
					//Auto-stretch title bar
					shtml+=	"	<td style='padding:4px; visibility:hidden;' id='zoomthmbalt'><strong>"+storedTitleTxt.replace(/[\s]/gi,"&nbsp;")+"</strong><BR><BR>"+storedAltTxt+"</td>";
				} else {
					shtml+=	"	<td style='padding:4px; visibility:hidden;' id='zoomthmbalt'><strong>"+storedTitleTxt+"</strong><BR><BR>"+storedAltTxt+"</td>";
				}
			} else {
				shtml+=	"		<td style='padding:0px;' id='zoomthmbalt'></td>";
			}
			shtml+=	"	</tr></table>";
			shtml+=	"</div>";
			obj.parent().append(shtml);
			var newWidth = $("#zoomthmbimg").find('img').width();
			var newHeight = $("#zoomthmbimg").find('img').height();
			$("#zoomthmbimg").find('img').attr('width',iThmbW);
			$("#zoomthmbimg").find('img').attr('height',iThmbH);
		//	$("#"+obj.position().top+obj.position().left).css({visibility: 'hidden'}); //Hide zoom icon
			fzooming=true;
			iStartzoomfrom=0; //go right
			if(parseInt($(window).width())-parseInt(obj.parent().position().left)<newWidth) iStartzoomfrom=-newWidth //go left
			$("#zoomthmbimg").animate({opacity:1,left:iStartzoomfrom},300);
			$("#zoomthmbimg").find('img').animate({
				width: newWidth,
				height: newHeight,
				opacity: '100'
			}, 300, function(){fzooming=false; $("#zoomthmbalt").css({visibility: 'visible'});});
			
		}
	}
}
	
function blurzoomthmbimg(obj) {
	if($('#zoomthmbimg').length>0) {
		fzooming=true;
		$("#zoomthmbalt").css({visibility: 'hidden'});
		$("#zoomthmbimg").find('img').animate({
			width: iThmbW,
			height: iThmbH,
			opacity: 0.1
		}, 300, function(){$('#zoomthmbimg').remove();fzooming=false;if(obj)focuszoomthmbimg(obj);});
		if(iStartzoomfrom.toString().length>0 && iStartzoomfrom!=0){
			$("#zoomthmbimg").animate({opacity:0,left:-80},300);
		} else {
			$("#zoomthmbimg").animate({opacity:0,left:0},300);
		}
		iStartzoomfrom=0;
	}
}
//================================================================================