var OverlaySlideShow_glbIndex = 0;
var OverlaySlideShow_SlideShow = true;
function OverlaySlideShow(pImageSRCs) {
	var ObjName = 'dvCMSOverlay';
	
	var tArrImgs = [];
	var tImgsLoaded = 0;
	var tArrImgsDimension = [];
	
	var tFunc = function () {
		var tLoading = false; 
		
		
		
		jQuery('#'+ObjName+'_IMAGE div[jrow|=0]:eq('+OverlaySlideShow_glbIndex+')').unbind('load');
		jQuery('#'+ObjName+'_IMAGE div[jrow|=0]').each(
			function (pIndex, pValue) {
				if (OverlaySlideShow_glbIndex != pIndex) {
					jQuery(pValue).fadeOut(400);
					if (jQuery(tArrImgs[OverlaySlideShow_glbIndex])[0].complete || OverlaySlideShow_glbIndex == 0)
						jQuery('#'+ObjName+'_IMAGE div[jrow|=0]:eq('+OverlaySlideShow_glbIndex+')').fadeIn(600);
					else {
						jQuery("#"+ObjName+'_LOADER').css('z-index', parseInt(jQuery("#"+ObjName+'_IMAGE').css("z-index"))+1);
						
						jQuery("#"+ObjName+'_LOADER').css({
						'top': jQuery('#'+ObjName+'_IMAGE').top+(jQuery('#'+ObjName+'_IMAGE').height/2),
						'left': jQuery('#'+ObjName+'_IMAGE').left+(jQuery('#'+ObjName+'_IMAGE').left/2),
						});
						
						jQuery("#"+ObjName+'_LOADER').fadeIn('fast');
						
						jQuery('#'+ObjName+'_IMAGE div[jrow|=0]:eq('+OverlaySlideShow_glbIndex+')').load(
							function () {
								jQuery("#"+ObjName+'_LOADER').fadeOut('fast');
								jQuery('#'+ObjName+'_IMAGE div[jrow|=0]').hide();
								
								jQuery('#'+ObjName+'_IMAGE div[jrow|=0]:eq('+OverlaySlideShow_glbIndex+')').fadeIn(600);
							}
						);
						tLoading = true;
					}
				}
			}
		);
		
		if (jQuery(tArrImgs[OverlaySlideShow_glbIndex])[0].complete) {
			jQuery('#'+ObjName+'_IMAGE').css({
			width: jQuery('#'+ObjName+'_IMAGE div[jrow|=0]:eq('+OverlaySlideShow_glbIndex+')').width(), 
			height: jQuery('#'+ObjName+'_IMAGE div[jrow|=0]:eq('+OverlaySlideShow_glbIndex+')').height()
			});
			jQuery("#"+ObjName+'_LOADER').hide();
		} else {
			jQuery('#'+ObjName+'_IMAGE').css({
			width: tArrImgsDimension[0], 
			height: tArrImgsDimension[1]
			});
		}
		
		var tTop = (jQuery(window).scrollTop()+((jQuery(window).height()/2)-(jQuery("#"+ObjName+'_IMAGE').height()/2)))+25;
		var tLeft = jQuery(window).scrollLeft()+((jQuery(window).width()/2)-(jQuery("#"+ObjName+'_IMAGE').width()/2));
		
		if (tTop <= jQuery(window).scrollTop()) tTop = jQuery(window).scrollTop()+25;
		if (tLeft <= 0) tLeft = 0;
		

		jQuery("#"+ObjName+'_IMAGE').css("top", tTop); 
		jQuery("#"+ObjName+'_IMAGE').css("left", tLeft);
		if (!tLoading) jQuery("#"+ObjName+'_IMAGE').css("z-index", GetDivHighestZIndex());	
		
		jQuery("#"+ObjName+'_HEADER').css({
			'top': jQuery("#"+ObjName+'_IMAGE').offset().top-(jQuery("#"+ObjName+'_HEADER').height()),
			'left': (jQuery("#"+ObjName+'_IMAGE').offset().left),
			'width': jQuery("#"+ObjName+'_IMAGE').outerWidth(),
			'z-index': GetDivHighestZIndex()
		}).fadeIn('fast');
		
		if (pImageSRCs[OverlaySlideShow_glbIndex][1]!='') {
			jQuery("."+ObjName+'_CAPTION').html(pImageSRCs[OverlaySlideShow_glbIndex][1]);
			jQuery("#"+ObjName+'_CAPTION').css({
				'top': jQuery("#"+ObjName+'_IMAGE').offset().top+(jQuery("#"+ObjName+'_IMAGE').height()),
				'left': (jQuery("#"+ObjName+'_IMAGE').offset().left),
				'width': jQuery("#"+ObjName+'_IMAGE').width(),
				'z-index': GetDivHighestZIndex()
			}).fadeIn('fast');
		} else {
			jQuery("#"+ObjName+'_CAPTION').fadeOut('fast');
		}
		
	}
	
	jQuery("body").append('<div id="'+ObjName+'_BG" style="width: 100%; height: 100%; background: #fff; position: fixed; top: 0; left: 0; z-index: '+GetDivHighestZIndex()+'; filter:alpha(opacity=0); -moz-opacity:0.0; opacity: 0.0"></div>');
	jQuery('#'+ObjName+'_BG').fadeTo(600, 0.80, 
		function () {
			var tHTML = '';
			tHTML = '<div id="'+ObjName+'_LOADER" style="position: absolute; z-index: '+GetDivHighestZIndex()+10+'; width: auto; height: auto; border-color: #2975B1; border-style: solid; border-width: 1px;  background: #ffffff; display: none"><img src="scripts/overlay.image.viewer-1.0/images/loading.gif" width="220" height="19" /></div>';
			tHTML += '<div id="'+ObjName+'_HEADER" style="position: absolute; font-weight: bold; font-size: 14px; background-color: #2975B1; color: #ffffff; display: none;">\
				<div style="width: 50%; float: left">\
					<div id="'+ObjName+'_NAV" style="float: left; margin-left: 10px; margin-top: 2px; font-family: Georgia, \'Times New Roman\', Times, serif; font-size: 11px; font-style: italic; font-weight: normal; color: #ffffff;"><a id="'+ObjName+'_NAVPREV" href="javascript: void(0)" style="color: #ffffff; text-decoration: none;">previous</a>&nbsp;|&nbsp;<a id="'+ObjName+'_NAVNEXT" href="javascript: void(0)" style="color: #ffffff; text-decoration: none;">next</a></div>\
				</div>\
				<div style="width: 50%; float: left" align="right">\
				<img id="'+ObjName+'_CLOSE" src="images/close.gif" width="130" height="24" style="cursor: pointer" />\
				</div>\
			</div>';
			tHTML += '<div id="'+ObjName+'_CLOSE" style="position: absolute; display: none;"><img src="images/close.gif" width="130" height="24" /></div>';
			
			tHTML += '<div id="'+ObjName+'_IMAGE" style="position: absolute; width: auto; height: auto; border-color: #2975B1; border-style: solid; border-width: 1px; background-color: #2975B1; visibility: hidden"><div style="position: relative">';
			jQuery.each(pImageSRCs,
				function (pIndex, pValue) {	
					if (pValue[2]!='')
						tHTML += '<div style="position: absolute; top: 0px; left: 0px; display: '+((pIndex==0) ? '' : 'none')+'" jrow="0"><a href="'+pValue[2]+'"><img src="'+pValue[0]+'" /></a></div>';
					else
						tHTML += '<div style="position: absolute; top: 0px; left: 0px; display: '+((pIndex==0) ? '' : 'none')+'" jrow="0"><img src="'+pValue[0]+'" /></div>';
				}
			);
			tHTML += '</div></div>'; 
			
			tHTML += '<div id="'+ObjName+'_CAPTION" style="position: absolute; top: 0px; left: 0px; display: none; font-family: Verdana; font-size: 12px; color: #d4e3ef; height: auto; width: auto; height: auto; border-color: #2975B1; border-style: solid; border-width: 1px; background-color: #2975B1"><div class="'+ObjName+'_CAPTION" style="padding-left: 25px; padding-right: 25px; padding-top: 15px; padding-bottom: 15px;"></div></div>';
			
			jQuery("body").append(tHTML);
			
			jQuery("#"+ObjName+'_NAVPREV').hover(function () { jQuery(this).css('font-weight', 'bold'); }, function () { jQuery(this).css('font-weight', 'normal'); });
			jQuery("#"+ObjName+'_NAVNEXT').hover(function () { jQuery(this).css('font-weight', 'bold'); }, function () { jQuery(this).css('font-weight', 'normal'); });
			
			jQuery("#"+ObjName+'_LOADER').css("top", jQuery(window).scrollTop()+((jQuery(window).height()/2)-(jQuery("#"+ObjName+'_LOADER').height()/2))); 
			jQuery("#"+ObjName+'_LOADER').css("left", jQuery(window).scrollLeft()+((jQuery(window).width()/2)-(jQuery("#"+ObjName+'_LOADER').width()/2)));

			var tINI = true;
			jQuery.each(pImageSRCs,
				function (pIndex, pValue) {
					tArrImgs[pIndex] = new Image();
					tArrImgs[pIndex].src = pValue[0];
					
					if (tINI) {
						if (jQuery(tArrImgs[pIndex])[0].complete) {
							
							jQuery("#"+ObjName+'_IMAGE').css('display', 'none');
							jQuery("#"+ObjName+'_IMAGE').css('visibility', 'visible');
							jQuery("#"+ObjName+'_IMAGE').fadeIn(400);
							
							tArrImgsDimension[0] = jQuery('#'+ObjName+'_IMAGE img:eq(0)').width();
							tArrImgsDimension[1] = jQuery('#'+ObjName+'_IMAGE img:eq(0)').height();
							
							tFunc();
							
							if (pImageSRCs.length > 1) {
								jQuery(document).everyTime('5s',function(i) {
									tNext();
								});
							} else {
								jQuery("#"+ObjName+'_HEADER div:eq(0)').remove();
								jQuery("#"+ObjName+'_HEADER div:eq(0)').css('width', '100%');
								jQuery("#"+ObjName+'_HEADER').css('background-color', '');
							}
							
						} else {
							
							jQuery("#"+ObjName+'_LOADER').show();
							
							tArrImgsDimension[0] = 600;
							tArrImgsDimension[1] = 400;
							
							jQuery(tArrImgs[pIndex]).load(
								function () { 
									
									jQuery("#"+ObjName+'_LOADER').fadeOut('fast'); 
									
									jQuery("#"+ObjName+'_IMAGE').css('display', 'none');
									jQuery("#"+ObjName+'_IMAGE').css('visibility', 'visible');
									jQuery("#"+ObjName+'_IMAGE').fadeIn(400);
									
									tArrImgsDimension[0] = jQuery('#'+ObjName+'_IMAGE img:eq(0)').width();
									tArrImgsDimension[1] = jQuery('#'+ObjName+'_IMAGE img:eq(0)').height();
									
									tFunc();
									
									if (pImageSRCs.length > 1) {
										jQuery(document).everyTime('5s',function(i) {
											tNext();
										});
									} else {
										jQuery("#"+ObjName+'_HEADER div:eq(0)').remove();
										jQuery("#"+ObjName+'_HEADER div:eq(0)').css('width', '100%');
										jQuery("#"+ObjName+'_HEADER').css('background-color', '');
									}
									
								}
							);
						}
						tINI = false;
					} else {
					
						jQuery(tArrImgs[pIndex]).load(
							function () {
								this.complete = true;
							}
						)
					
					}
				}
			);
			
			var tNext = function () {
				
				if (OverlaySlideShow_glbIndex == (tArrImgs.length-1))
					OverlaySlideShow_glbIndex = -1;
				else if (OverlaySlideShow_glbIndex > (tArrImgs.length-1))
					OverlaySlideShow_glbIndex = 0;

				OverlaySlideShow_glbIndex++;
				
				tFunc();
				
				if (pImageSRCs.length > 0) jQuery("#"+ObjName+'_NAVPREV').fadeIn('fast');
				
				jQuery("."+ObjName+'_CAPTION').html(pImageSRCs[OverlaySlideShow_glbIndex][1]);
				
				if (OverlaySlideShow_glbIndex >= (pImageSRCs.length-1)) OverlaySlideShow_glbIndex = -1;
			};
			
			var tPrev = function () {
				
				if (OverlaySlideShow_glbIndex == 0)
					OverlaySlideShow_glbIndex = tArrImgs.length;
				else if (OverlaySlideShow_glbIndex < 0)
					OverlaySlideShow_glbIndex = tArrImgs.length-1;
				
				OverlaySlideShow_glbIndex--;
				
				tFunc();
				
				jQuery("."+ObjName+'_CAPTION').html(pImageSRCs[OverlaySlideShow_glbIndex][1]);
				if (OverlaySlideShow_glbIndex == 0) {
					OverlaySlideShow_glbIndex = tArrImgs.length;
				}
			};
			
			jQuery('#'+ObjName+'_IMAGE, #'+ObjName+'_HEADER, #'+ObjName+'_CAPTION').hover(
				function () {
					OverlaySlideShow_SlideShow = false;
					jQuery(document).stopTime();
				},
				function () {
					OverlaySlideShow_SlideShow = true;
					jQuery(document).everyTime('5s',function(i) {
						tNext();
					});
				}
			);
			
			jQuery("#"+ObjName+'_NAVNEXT').click(
				function () {
					tNext();
				}
			);
			
			jQuery("#"+ObjName+'_NAVPREV').click(
				function () {
					tPrev();
				}
			); 

			
			jQuery('#'+ObjName+'_CLOSE, #'+ObjName+'_BG').click(
				function () {
					jQuery('#'+ObjName+'_BG').remove();
					jQuery('#'+ObjName+'_LOADER').remove();
					jQuery('#'+ObjName+'_HEADER').remove();
					jQuery('#'+ObjName+'_IMAGE').remove();
					jQuery('#'+ObjName+'_CAPTION').remove();
					OverlaySlideShow_glbIndex = 0;
					jQuery(document).stopTime();
					OverlaySlideShow_SlideShow = true;
				}
			);
			
		}
	);
}

function GetDivHighestZIndex() {
	var ret = 0;
	var ArrCMS_Func_GetHighestzIndex = new Array();
	jQuery('body').find('div').each(
		function () {
			if (parseFloat(jQuery(this).css('z-index')) > 0) {
				ArrCMS_Func_GetHighestzIndex[parseFloat(jQuery(this).css('z-index'))] = jQuery(this).css('z-index');
			}
		}
	);
	
	if (ArrCMS_Func_GetHighestzIndex.length > 0) {
		ret = parseFloat(ArrCMS_Func_GetHighestzIndex[ArrCMS_Func_GetHighestzIndex.length-1])+1;
	} else {
		ret = 1;
	}
	
	return ret;
}
