﻿var isIE = (navigator.appName.indexOf("Internet Explorer")>=0);
var isIE7 = (navigator.appVersion.indexOf("MSIE 7.0")>=0);
var isFF = (navigator.appName.indexOf("Netscape")>=0);
var isSafari = (navigator.appVersion.indexOf("Safari")>=0);

function getE() {
  var elements = new Array();

  for (var i = 0; i < arguments.length; i++) {
    var element = arguments[i];
    if (typeof element == 'string')
      element = document.getElementById(element);

    if (arguments.length == 1) 
      return element;

    elements.push(element);
  }

  return elements;
}

function getObject(name) {
	return document.getElementById(name);
}

var interval = null;


var divPopup = null;
var myIndex = null;
function toggleIFrame(name, url,w,h){
	var opacity = document.getElementById('divOpacity');
	var v = viewPoint;

	v.calc();
	docheightcomplete = (v.standardbody.offsetHeight > v.standardbody.scrollHeight) ? v.standardbody.offsetHeight : v.standardbody.scrollHeight;

	opacity.style.width = v.docwidth + "px";
	opacity.style.height = this.docheightcomplete + "px";
	
	opacity.style.display = "block";
	divPopup = getObject(name);
	var frm = getObject(name+"_frm");
	if(divPopup.style.display != "block"){
		frm.src = url;
		frm.style.width = w + "px";
		frm.style.height = (h) + "px";
		divPopup.style.width = w + "px";
		divPopup.style.height = h + "px";
		divPopup.style.left =  (v.docwidth/2 - w/2)+ "px";
		divPopup.style.top = (getYOffset() + 30)+ "px";
		divPopup.style.display = "block";
	} else {
		frm.src = url;
		divPopup.style.display = "none";
		opacity.style.display = "none";
	}
}

function popupInit(index){
	setTimeout('togglePopup("galDiv",650,650,'+index+')',700);
}

function togglePopup(name,w,h,index){
	myIndex = index;
	var opacity = document.getElementById('divOpacity');
	var v = viewPoint;

	v.calc();
	docheightcomplete = (v.standardbody.offsetHeight > v.standardbody.scrollHeight) ? v.standardbody.offsetHeight : v.standardbody.scrollHeight;

	opacity.style.width = v.docwidth + "px";
	opacity.style.height = this.docheightcomplete + "px";
	
	opacity.style.display = "block";
	divPopup = getObject(name);
	if(divPopup.style.display != "block"){
		divPopup.style.width = w + "px";
		divPopup.style.height = h + "px";
		divPopup.style.left =  (v.docwidth/2 - w/2)+ "px";
		divPopup.style.top = (getYOffset() + 10)+ "px";
		divPopup.style.display = "block";
		initImg(myIndex);
		setTimeout('initImg('+myIndex+',myIndex)',1000);
	} else {
		divPopup.style.display = "none";
		opacity.style.display = "none";
	}
}
function getYOffset(){    
	var pageY;    
	if(typeof(window.pageYOffset)=='number'){
		pageY=window.pageYOffset;
	}else{
		pageY=document.documentElement.scrollTop;
	}
	return pageY;
}

//window.onscroll = scrollEvent;

//function scrollEvent(){
//	if(divPopup != null && divPopup.style.display == "block"){
//		divPopup.style.top = (getYOffset() + 20)+ "px";
//	}
//}

var prevW = 0;
var prevH = 0;
var currW = 0;
var currH = 0;
var txtH = 0;
var currLeft = null;
var currImg = null;
var currTxt = null;
var prevImg = null;
var nxtImg = null;
var offsetLeft = 0;
var toProd = null;

function initImg(myIndex,isShow){
	prevImg	= getE("prevImg");
	nxtImg	= getE("nxtImg");
	currImg = getE("currImg1");
	currTxt = getE("txtArea");
	toProd  = getE("toProduct");
	
	prevImg.style.display = "none";
	nxtImg.style.display = "none";
	
	if(isShow != null){
		showImage(myIndex,myIndex);
	}else{
		showImage(myIndex);
	}
}

function showImage(myIndex,isShow){
	currImg.src = imgArr[myIndex].url;
	currImg.alt = imgArr[myIndex].alt;
	if(imgArr[myIndex].text != null){
		currTxt.innerHTML = imgArr[myIndex].text;
	}
	
	if(imgArr[myIndex].pID != null){
		toProd.style.display = "block";
		toProd.href = domain + 'product.aspx?id='+imgArr[myIndex].pID;
	}else{
		toProd.style.display = "none";
	}

	txtH = parseInt(getE("ctrlArea").offsetHeight);
	currW = (parseInt(getE("currImg1").width))+"px";
	currH = (parseInt(getE("currImg1").height) + txtH)+"px";
	
	if(parseInt(currW) >= 400){
		divPopup.style.width = currW;
	}else{
		divPopup.style.width ="400px";
	}
	divPopup.style.height = currH;
	
	divPopup.style.display = "none";
		
	var v = viewPoint;
	v.calc();
	divPopup.style.left =  (v.docwidth/2 - ((parseInt(divPopup.style.width)/2)+40))+ "px";
	offsetLeft = ((parseInt(prevW))-(parseInt(currW)));
	if(isShow != null){
		divPopup.style.opacity = '1.0';
		divPopup.style.filter='alpha(opacity=100)';
		$(divPopup).fadeIn('fast',function(){
			if(myIndex != 0){
				prevImg.style.display = "block";
			}
			if(myIndex != (imgArr.length-1)){
				nxtImg.style.display = "block";
			}
		});
	}else{
		divPopup.style.opacity = '0.0';
		divPopup.style.filter='alpha(opacity=00)';
		divPopup.style.display = "block";
		if(myIndex != 0){
			prevImg.style.display = "block";
		}
		if(myIndex != (imgArr.length-1)){
			nxtImg.style.display = "block";
		}
	}
	if(parseInt(currW) >= 400){
		prevW = currW;
	}else{
		prevW ="400px";
	}
	prevH = currH;
}


function prevThis(){
	var prevIndex = --myIndex;
	currImg.src = imgArr[prevIndex].url;
	currImg.onLoad= togglePopup('galDiv',650,800,prevIndex);
}
function nxtThis(){
	var nextIndex = ++myIndex;
	currImg.src = imgArr[nextIndex].url;
	currImg.onLoad = togglePopup('galDiv',650,800,nextIndex);
}
function closeThis(){
	divPopup.style.display = "block";
	togglePopup('galDiv');
}

