var aPopupImages = [];
var iPopupImageIndex = -1;

function iniPopup(name, oJqueryObject, aImages)
{
    aPopupImages[name] = aImages;
    
    if (!jQuery('#popup').length) jQuery('<div id="popup"></div>').appendTo('body');
    if (!jQuery('#backgroundPopup').length) jQuery('<div id="backgroundPopup"></div>').prependTo('body');

    oJqueryObject.click(function() { clickToPopup(this, name) });
      
    jQuery("#popup").click(disablePopup);  

    jQuery(document).keypress(function(e) {  
        if(e.keyCode==27 && jQuery('#popup').length && jQuery('#popup').css('display') != 'none'){  
            disablePopup();
        }
    });
}

var sPopupName;

var iWinWidth = $(window).width();
var iWinHeight = $(window).height();
var iDivWidth = iWinWidth-80;
var iDivHeight = iWinHeight-80;


function clickToPopup(ob, name)
{
    sPopupName = name;
    iPopupImageIndex = -1;
	
    if (jQuery('#popup').length && jQuery('#popup').css('display') == 'none')
    {
		id = jQuery(ob).attr('id');
        id_parts = jQuery(ob).attr('id').split('_');
        iPopupImageIndex = !id_parts.length || parseInt(id_parts[1]) != id_parts[1] ? 0 : id_parts[1];
        jQuery('#popup img').remove();
		var iDivWidth = iWinWidth - 40;
        jQuery('#popup').append('<img src="' + aPopupImages[name][iPopupImageIndex][0] + '" width=10" height="10" border="0" alt="" />');
		scale(aPopupImages[sPopupName][iPopupImageIndex][1],aPopupImages[sPopupName][iPopupImageIndex][2]);
    }
    loadPopup();
}

function scale(width,height)
{
	var iOrigWidth  = width;
	var iOrigHeight = height;
	
	fRatioWidth  = 1;
	fRatioHeight = 1;
	
	iMaxWidth = iDivWidth - 20;
	iMaxHeight = iDivHeight- 20;
	if( iOrigWidth>iMaxWidth) fRatioWidth = ( iMaxWidth / iOrigWidth );
	if( iOrigHeight>iMaxHeight) fRatioHeight = ( iMaxHeight / iOrigHeight );
	
	if( fRatioWidth > fRatioHeight ) 
	{
		fRatio = fRatioHeight; 
	} else {
		fRatio = fRatioWidth;
	}
	
	iWidth  = iOrigWidth * fRatio;
	iHeight = iOrigHeight * fRatio;
	
	jQuery("#popup div").css({"width":iDivWidth,"height":iDivHeight});
	jQuery("#popup img").css({"width":iWidth,"height":iHeight});
}

function loadPopup()
{   
    if (jQuery('#popup').length && jQuery('#popup').css('display') != 'none') return;
    
    centerPopup();
            
    jQuery("#backgroundPopup").css({  
        "opacity": "0.7"  
    });  
    jQuery("#backgroundPopup").fadeIn("fast");
    jQuery("#popup").fadeIn("fast");
    if (iPopupImageIndex!=-1)
    {
        jQuery('body').append('<img src="/site/std/img/iconClose.png" id="icon_close" width="60" height="60" border="0" alt="" style="behavior:url(/js/iepngfix.htc); z-index: 200; cursor: pointer" onclick="disablePopup()" />').fadeIn("slow", centerClose);
        //if (iPopupImageIndex > 0)
        //    jQuery('body').append('<img src="/site/std/img/iconLeft.png" id="icon_prev" width="60" height="60" border="0" alt="" style="behavior:url(/js/iepngfix.htc); z-index: 100; cursor: pointer" onclick="prevImage()" />').fadeIn("slow", centerPrev);
       //if (iPopupImageIndex < aPopupImages[sPopupName].length - 1)
        //    jQuery('body').append('<img src="/site/std/img/iconRight.png" id="icon_next" width="60" height="60" border="0" alt="" style="behavior:url(/js/iepngfix.htc); z-index: 100; cursor: pointer" onclick="nextImage()" />').fadeIn("slow", centerNext);
    }            

}  

function centerPopup()
{    
    //request data for centering  
    var windowWidth = $(window).width();
    var windowHeight = $(window).height();  

    var popupWidth = jQuery("#popup").width();
    var popupHeight = jQuery("#popup").height();  
    
    //centering  
    jQuery("#popup").css({  
        "position": "absolute",  
        "top": $(window).scrollTop(),
        "left": windowWidth/2-popupWidth/2  
    });  
    //only need force for IE6  
    jQuery("#backgroundPopup").css({  
        "height": windowHeight  
    });
}

function disablePopup()
{  
    if (!jQuery('#popup').length || jQuery('#popup').css('display') == 'none') return;
 
    jQuery("#backgroundPopup").fadeOut("slow");  
    jQuery("#popup").fadeOut("slow");  
    jQuery("#icon_close, #icon_prev, #icon_next").remove();
}  

function centerClose()
{    
    //request data for centering  
    var windowWidth = $(window).width();  
    var iconWidth = jQuery("#icon_close").width();
	var iImgWidth = jQuery("#popup").width();
    
    //centering  
    jQuery("#icon_close").css({  
        "position": "absolute",  
        "top": $(window).scrollTop(),  
        "left": windowWidth/2-iconWidth + (iImgWidth/2) + 5
    });  
}

function centerPrev()
{    
    //request data for centering  
    var windowWidth = document.documentElement.clientWidth;
    var windowHeight = document.documentElement.clientHeight;
    var iconHeight = jQuery("#icon_prev").height();  
	var iconPrevWidth = jQuery("#icon_prev").width();
	var popupWidth = jQuery("#popup").width();
	var popupHeight = jQuery("#popup").height();
	
    //centering  
    jQuery("#icon_prev").css({  
        "position": "absolute",
        "top": windowHeight/2-iconHeight/2 + $(window).scrollTop() + ( popupHeight /2 ),
        "left": ( windowWidth/2-popupWidth/2 )
    });    
} 

function centerNext()
{    
    //request data for centering  
    var windowHeight = document.documentElement.clientHeight;
    var popupHeight = jQuery("#icon_next").height();  
    
    //centering  
    jQuery("#icon_next").css({  
        "position": "absolute",  
        "top": windowHeight/2-popupHeight/2 + $(window).scrollTop(),  
        "right": 10
    });     
} 

function prevImage()
{
    if (iPopupImageIndex <= 0) return;
    iPopupImageIndex--;
    
    jQuery('#popup img').fadeOut("slow", function() {
        jQuery(this).remove();
        jQuery('#popup').append('<img src="' + aPopupImages[sPopupName][iPopupImageIndex][0] + '" width="10" height="10" border="0" alt="" />');
        scale(aPopupImages[sPopupName][iPopupImageIndex][1],aPopupImages[sPopupName][iPopupImageIndex][2]);
		
        if (iPopupImageIndex == 0)
            jQuery('#icon_prev').remove();
        if (iPopupImageIndex == aPopupImages[sPopupName].length - 2)
            jQuery('body').append('<img src="/site/std/img/iconRight.png" id="icon_next" width="60" height="60" border="0" alt="" style="behavior:url(/js/iepngfix.htc); z-index: 100; cursor: pointer" onclick="nextImage()" />').fadeIn("slow", centerNext);
        centerPopup();
    });
}

function nextImage()
{
    if (iPopupImageIndex >= aPopupImages[sPopupName].length - 1) return;
    iPopupImageIndex++;
    
    jQuery('#popup img').fadeOut("slow", function() {
        jQuery(this).remove();
        jQuery('#popup').append('<img src="' + aPopupImages[sPopupName][iPopupImageIndex][0] + '" width="10" height="10" border="0" alt="" />');
        scale(aPopupImages[sPopupName][iPopupImageIndex][1],aPopupImages[sPopupName][iPopupImageIndex][2]);
		
        if (iPopupImageIndex == 1)
            jQuery('body').append('<img src="/site/std/img/iconLeft.png" id="icon_prev" width="60" height="60" border="0" alt="" style="behavior:url(/js/iepngfix.htc); z-index: 100; cursor: pointer" onclick="prevImage()" />').fadeIn("slow", centerPrev);
        if (iPopupImageIndex == aPopupImages[sPopupName].length - 1)
            jQuery('#icon_next').remove();
        centerPopup();
    });
}
