var enableWide = false;
var enablePopup = true;


var imgLeft = 0;
var imgTop = 0;
var loaded = false;
var opacity = 0;

var z=0;
var i = 0;
var imgObj;
var tmpImgObj;
imgs = new Array();
var popupIndex;

var bigImageObj;
var scrollY;
var ext; var extIndex;
var myWidth = 0, myHeight = 0; 

var bImgWidth;
var bImgHeight;
var showingPopup = false;

/*Browser initialization*/
var isOpera=self.opera
var ie=document.all&&document.getElementById&&!isOpera
var ns6=document.getElementById&&!document.all


/*Event initialization*/
function initGallery_popup(){

//This part must be uncomment when popup images are small.
//If popup images are huge, page will be load slowly
/*
   if(enablePopup){
     cashPopupGallery();
   }
*/

   if (!parent.document.getElementById('templateframe')){
    	if(ie||isOpera){
    		document.attachEvent("onmouseover", ieEvent);
    		document.attachEvent("onmouseout", ieEvent);    		
           
    	}else{
    		document.addEventListener("mouseover", imgOver, true);
    		document.addEventListener("mouseout", imgOut, true);
    	}
    }
}


/*IE event initialization*/
function ieEvent(){

	if(event.type == 'mouseover' && enablePopup)
	imgOver(event);	
	
	if(event.type == 'mouseout' && enablePopup)
	imgOut(event);		
}

function imgOver(pEv){
    
    if(pEv.srcElement) 
        lEl=pEv.srcElement;
	else 
	    lEl=pEv.target;


	 
	if(lEl && lEl.tagName) {	    
		if ((lEl.tagName == "DIV" && (lEl.id == "popupDiv" || lEl.id == "innerPopupDiv")) || (lEl.tagName == "IMG" && lEl.src.indexOf("popup") > 0)) {
			showingPopup = true;
		}
		
		
    	if(lEl.tagName == "IMG" &&  inGalleryDiv(lEl) && !showingPopup){
    	    if(ie) lEl.style.cursor = 'hand';
    	    if(ns6 || isOpera) lEl.style.cursor = 'pointer';
    	    
    	    imgObj = lEl;
    	    showingPopup= true;
    	}
    	
   	    if(enablePopup && showingPopup) setTimeout("showPopup()", 200);
    }
    
    
    
}

function imgOut(pEv){
 
    if(pEv.srcElement) 
        lEl=pEv.srcElement;
	else 
	    lEl=pEv.target;
	
    if(lEl && lEl.tagName) {	    

		if ((lEl.tagName == "DIV" && (lEl.id == "popupDiv" || lEl.id == "innerPopupDiv")) || (lEl.tagName == "IMG" && lEl.src.indexOf("popup") > 0)) {
			showingPopup = false;
		}
		
		if(lEl.tagName == "IMG" && inGalleryDiv(lEl) ){
		    lEl.style.cursor = '';
			showingPopup = false;		    
		}
	}
	
    setTimeout("checkNextElem()", 100);		

}


function inGalleryDiv(el){

	if(el && el.tagName) {
		while (el.tagName.toLowerCase() != "body") {
			if (el.id == "popupGalleryDiv")	{
				return true
			}else {
				el = el.parentNode;
			}
		}
	} 
	
	return false;
	
}

function checkNextElem() {

	if(!showingPopup){
		hidePopup();
	}	
}


function initScreen(){

    if( typeof( window.innerWidth ) == 'number' ){
         //Non-IE 
         myWidth = parent.window.innerWidth; 
         myHeight = parent.window.innerHeight; 
    } else if( parent.document.documentElement && 
             ( parent.document.documentElement.clientWidth || 
               parent.document.documentElement.clientHeight ) ) {
                //IE 6+ in 'standards compliant mode' 
                
                myWidth = parent.document.documentElement.clientWidth; 
                myHeight = parent.document.documentElement.clientHeight;
    } else if( parent.document.body && 
             ( parent.document.body.clientWidth || 
               parent.document.body.clientHeight ) ) {
               
                //IE 4 compatible 
                myWidth = parent.document.body.clientWidth; 
                myHeight = parent.document.body.clientHeight; 
    } 
} 


//
//
//
//
// Popup functions
//
//
//
//
function setBImgSize(h,w) {

	bImgHeight=h;
	bImgWidth=w;
	
	initScreen();
	detectScroll();
	detectImgParam();
	
}

function detectImgParam(){

    imgTop = myHeight/2 - bImgHeight/2;
    imgLeft = myWidth/2 - bImgWidth/2;

}

function detectScroll(){
    if(ns6){
        scrollY = window.pageYOffset;
    }else{
        scrollY = document.body.scrollTop;    
    }
}

function showPopup() {
	
	oldSrc = imgObj.src;
	
	popupSrc = oldSrc;
	
	extIndex = oldSrc.lastIndexOf(".");
	ext = oldSrc.substr(extIndex);
	start = extIndex-2;
    indx = imgObj.src.substr(start, 2);	
    
    if(indx/indx != 1){
        start = extIndex-1;
        indx = imgObj.src.substr(start, 1);
    }    

	rg = new RegExp(ext);
	popupSrc = popupSrc.replace(rg, "_popup" + ext);
		
    document.getElementById("popupImg").src = popupSrc;
   	document.getElementById("popupImg").style.width = bImgWidth;
   	document.getElementById("popupImg").style.height = bImgHeight;   
   	
	document.getElementById("popupDiv").style.top = imgTop+scrollY;
	document.getElementById("popupDiv").style.left = imgLeft;
	document.getElementById("popupDiv").style.display = 'block';
		
	setTimeout("changeOpacity()", 70);
}


function changeOpacity(){
	
  elem = document.getElementById("popupImg");
	
  if (ie) {
		
	var oAlpha = elem.filters['DXImageTransform.Microsoft.alpha'] || elem.filters.alpha;
    if(oAlpha.opacity < 100){
		oAlpha.opacity = opacity*100;		
		opacity += 0.1; 
		setTimeout("changeOpacity()", 70);		
	}
     
  }else {
	
	if(document.getElementById("popupImg") && document.getElementById("popupImg").style.opacity < 1){
		document.getElementById("popupImg").style.opacity = opacity;
		opacity += 0.1; 
		setTimeout("changeOpacity()", 70);		
	}
  }
}

function hidePopup(){
    
    elem = document.getElementById("popupImg");
	opacity = 0;
	if(ie){
		var oAlpha = elem.filters['DXImageTransform.Microsoft.alpha'] || elem.filters.alpha;
		oAlpha.opacity = opacity*100;
	}else{
		document.getElementById("popupImg").style.opacity = opacity;
	}
	
	document.getElementById("popupDiv").style.display = 'none';
	document.getElementById("popupImg").src = 'img_0/1px.gif';
	document.getElementById("popupImg").style.width = '1px';
	document.getElementById("popupImg").style.height = '1px';

}


if(ie||isOpera){
	window.attachEvent("onload", initGallery_popup);
   
}else{
	window.addEventListener("load", initGallery_popup, true);
}
