// Contains Copyrighted Materials by (c) ISPRODUCTIONS 2007

/**************************************  FRONT PAGE *****************************************/
/* Frontpage variables */
var imgWidth, cWidth;
var ib, ibW, ocont, oPanel, iAllImgs ;
if (typeof arGalleryInfo != 'undefined') var imgcount = arGalleryInfo.length-1;
var imgPerPage = 5;
var iMargin = 4;
var bCutPanel = false;
var currentPos ;//= 1;
var LastCurrent ;//= imgcount - imgPerPage + 1;
var curLeft = 0;
var oPrevArrow, oNextArrow;
var arPLImgs, arHs, arWs, arILoad, bgrheight;
//var minThumbs=205;
//var bgrwidth = 140;
var padTopBottom=20;
var iTxtH = 20;
//var iIMG_THUMB_HEIGHT = 195;
/* end of Frontpage variables */


// frontpage init function
function init_FP(){

   ImgPreload();
}
   
function init_FP_next(){

   var arGName, arGImg, i;
   currentPos = 1;
   LastCurrent = imgcount - imgPerPage + 1;
    
   if ( bgrheight > iIMG_THUMB_HEIGHT ) bgrheight = iIMG_THUMB_HEIGHT;
    
//** set sizes according to the frontpage image if it is big one
   oHolder = GetElement("holder");
   var pad = iTxtH + padTopBottom;
   
   //** set sizes to the thumbnail 
   if (bgrheight+pad < minThumbs) bgrheight = minThumbs-pad;
   GetElement('grayrow').style.height = bgrheight+pad+'px';
   GetElement('icontainer').style.height = bgrheight+iTxtH+'px';
   GetElement('icontent').style.height = bgrheight+iTxtH+'px';
   //**set position of the Gallery name field according to the thumbnail strip height
   arGName = getElementsByClassName('gname');
   for (i=0; i<arGName.length; i++){
        GetElement(arGName[i]).style.top = bgrheight-iTxtH+5+'px';
        }
   //** vertical center the image thumbnail according to the box without the Name Label (Gallery name)
   arGImg = getElementsByClassName('img_thumb');
   for (i=0; i<arGImg.length; i++){
        if (arHs[i] != bgrheight){
            var ob = GetElement(arGImg[i])
            ob.style.top = (bgrheight-iTxtH-arHs[i])/2 +'px';
        }
        GetElement(arGImg[i]).style.left = ( bgrwidth-arWs[i])/2 + 'px';
   }
   ib = GetElement('imgbox');
   ibW = parseInt(ib.offsetWidth);
   oPrevArrow = GetElement('prevG');
   oNextArrow = GetElement('nextG');
   oPrevArrow.style.display = 'none';
   
   iAllImgs = imgcount;
   if (imgcount<=imgPerPage) {
       // iAllImgs = imgPerPage;
        bCutPanel = true;
        oNextArrow.style.display = 'none';
   }
   cWidth = iAllImgs * (ibW+ 2*iMargin);
   oCont = GetElement('icontent');
   
   oPanel = GetElement('icontainer');
   if (oCont) {
       oCont.style.width = cWidth+20+'px';
       oCont.style.left = '0px';
       if (bCutPanel) {
            if (oPanel) oPanel.style.width = cWidth+'px';
       }
   }
   oHolder.style.visibility = 'visible';

   if (oHolder) {
        oD = GE('fpimage');
        arImD = oD.getElementsByTagName('img');
        if (arImD.length > 0 ){
            oIm = arImD[0];
           if (oIm.onload) oIm.onload();
       } 
   }
}
var iFIDEnd = 0;
var imgs;
function ImgPreload(){
    bgrheight=0;
    arHs = new Array();
    arWs = new Array();
    arILoad = new Array();
    
    if (!document.arPLImgs) document.arPLImgs = new Array();
    imgs = arGalleryInfo.length;
    for (a=1; a<imgs;  a++){
        th = arGalleryInfo[a].thumb;
        if (th.length>0) th = th.substring(0,th.indexOf('"'));
        document.arPLImgs[a] = new Image();
        document.arPLImgs[a].src = th;
        arILoad[a-1] = setTimeout('FillImgDims('+a+', 5)',5);
    }
    if (imgs==0) {
         oHolder = GetElement("holder");
         if(oHolder) oHolder.style.visibility = 'visible';
         oPrevArrow = GetElement('prevG');
         if(oPrevArrow) oPrevArrow.style.display = 'none';
         oNextArrow = GetElement('nextG');
         if(oNextArrow) oNextArrow.style.display = 'none';
    }
   
}

 function FillImgDims(ind,iCTime) {
        if ( iCTime > 1000 ) {
            clearTimeout(arILoad[ind-1]);
            arHs[ind-1] = 0; 
            arWs[ind-1] = 0;
            iFIDEnd++;
    //** if FillImgDims are over and then call init_FP_next()
            if (iFIDEnd == (imgs-1)) init_FP_next();
            return;
        }
        else
            if ( document.arPLImgs[ind].height > 0 ) {
                if (bgrheight < document.arPLImgs[ind].height) bgrheight = document.arPLImgs[ind].height;
                arHs[ind-1] = document.arPLImgs[ind].height; 
                arWs[ind-1] = document.arPLImgs[ind].width;
                iFIDEnd++;
        //** if FillImgDims are over and then call init_FP_next()
                if (iFIDEnd == (imgs-1)) init_FP_next();
            }
            else 
                arILoad[ind-1] = setTimeout('FillImgDims('+ind+', '+(iCTime+5)+')',5);
    }
function GalleryMove(dir,o) {
    var Limit;
    if (dir==1) Limit = 1; else Limit = LastCurrent;
    if (currentPos != Limit){
               // curLeft = curLeft + dir *(ibW+ 2*iMargin);
               // oCont.style.left = curLeft + 'px';
        curLeft = dir *(ibW+ 2*iMargin);
        //alert(curLeft)
        var anim = new YAHOO.util.Motion('icontent', { points: { by: [curLeft, 0] } });
        anim.animate();
       
        currentPos -= dir;
        if (currentPos == Limit) eval('o'+o+'Arrow.style.display = "none"');
    }
    else {
        if (dir>0)
            oPrevArrow.style.display = 'none';  //hide previous
        else
            oNextArrow.style.display = 'none';  //hide next
    }
    if (currentPos != 1) oPrevArrow.style.display = '';                // show prev
    if (currentPos != LastCurrent) oNextArrow.style.display = '';     // show next
    

}
function change(id,clas) {
    GetElement(id).className = clas;
}

 // returns an array of id-s
function getElementsByClassName(className, parent){
    var oParent;
    var arr = new Array();
    if (parent) oParent = GetElement(parent); else oParent=document;
    var elems = oParent.getElementsByTagName("div");
    for(var i = 0; i < elems.length; i++)
    {
        var elem = elems[i];
        var id = elem.getAttribute("id");
        var cls = elem.className
        if(cls == className){
            arr[arr.length] = id;
        }
    }
    return arr;
}

//returns an array of objects
function getObjectsByClassName(className, parent){
    var oParent;
    var arr = new Array();
    if (parent) oParent = GetElement(parent); else oParent=document;
    var elems = oParent.getElementsByTagName("div");
    for(var i = 0; i < elems.length; i++)
    {
        var elem = elems[i];
        var cls = elem.className
        if(cls == className){
            arr[arr.length] = elem;
        }
    }
    return arr;
}

/* inside pages variables */
var arGNameWidths = new Array();
var temp;
/* end of inside pages variables */


function GetElement(id){
    return document.getElementById(id);
}
function GE(id) { return GetElement(id); }

function togDisp(e,ind){
    stopB(e);
    var elems = getObjectsByClassName('HMoreButton');
    for(var i=0;i<elems.length;i++){
        var obj=elems[i];
        var dp="none";
       // var dp = 'hidden';

       if (obj.id == ('more'+ind)){
           // if(obj.style.visibility == 'visible') dp='hidden'; else { dp = 'visible'; }
            if(obj.style.display=='block') dp='none'; else dp = 'block';
        }
//          obj.style.visibility = dp;
        obj.style.display=dp;
    }
    return false;
}
function stopB(e){
    if(!e) e=window.event;
    e.cancelBubble=true;
}


//*!!!!!!!!!!!!!!!*****************  razdeli funcciite na takiva za FP, za inside pages i ob6ti ********/

//window.onload = ImgPreload();
var iATo1, iATo2;
function DoTheJob(obj,target){
    var href, ds, par='z';
    
    if (obj){
        ShowLoading(target);

    /*    if (arFadeTO)
            for(i = 0; i <= 100; i++)
                clearTimeout(arFadeTO[i]);
        clearTimeout(iFeTo);
        if (iATo1!=0) clearTimeout(iATo1);
        if (iATo2!=0) clearTimeout(iATo2);
   */   
        clearTimeout(iFeTo);
        if (iATo1!=0) clearTimeout(iATo1);

        var href = obj;
        // get & set the template content
        postGetData(href, par, target, true);
    }
}

function postGetData(dataSource, data, divID, bIsAdditFuncsExec)
{
    var XMLHttpRequestObject;
    if (window.XMLHttpRequest) {
        XMLHttpRequestObject = new XMLHttpRequest();
    }else if (window.ActiveXObject){
        XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP");
        }

    if(XMLHttpRequestObject) {
        var obj = document.getElementById(divID);
        try{
            XMLHttpRequestObject.open("POST", dataSource, true);
            XMLHttpRequestObject.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
            XMLHttpRequestObject.onreadystatechange = function()
                {   
                    if (XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status == 200) {
                            var cnt = XMLHttpRequestObject.responseText;
                            
                            bPLay =  false;
                            updateScripts(cnt, true);
                            
                            obj.innerHTML = cnt;
                         
                        if (bIsAdditFuncsExec) {

                            AllOnloads();
                            
                            iATo1 = setTimeout('if (typeof initLightbox != "undefined") initLightbox();', 20);
                           
                        }
                   /*   
                     for (a=0 ; a< cnt.length; a=a+2000)
                         alert(cnt.substr(a, 2000))
                   */   
                    }
                }
            XMLHttpRequestObject.send(data);
       }
        catch (e) {
            window.open(dataSource)
       }
    }
}


function ShowLoading(target){
    if (imgLoadingFile) {
        oMI = document.getElementById(target);
        if (oMI){
            var objILoading = document.createElement("div");
		    objILoading.setAttribute('id','imgLoading');
		    oMI.appendChild(objILoading);
		    
		    objILoading.innerHTML = "<img src='CommonFiles/spacer.gif' />"
        }
    }
 }
function updateScripts( html, loadScripts){

        if(!loadScripts) return;
        
        var _parseOLDScripts = function(){
            var oCS = document.getElementById("custScript");
            if (oCS)
                if (oCS.removeNode) 
                    oCS.removeNode(true);
                else
                    oCS.innerHTML = '';
             
            var oCS = document.getElementById("custScript0");
            if (oCS)
                if (oCS.removeNode) 
                    oCS.removeNode(true);
                else
                    oCS.innerHTML = '';
         }
          
        var _parseScripts = function(){
            var docHead = document.getElementsByTagName("head")[0];
         
            var re = /(?:<style.*(?:src=[\"\'](.*)[\"\']).*>.*<\/style>)|(?:<style.*>([\S\s]*?)<\/style>)/ig; // assumes HTML well formed and then loop through it.
            var match;
            while(match = re.exec(html)){
                 var s0 = document.createElement("style");
                 if (match[1])
                    s0.src = match[1];
                 else if (match[2]){
                    s0.setAttribute("type", "text/css");
                    if(s0.styleSheet){// IE
                        s0.styleSheet.cssText = match[2];
                    } else {// w3c
                        var cssText = document.createTextNode(match[2]);
                        s0.appendChild(cssText);
                    }
                    }
                 else
                      continue;
                 docHead.appendChild(s0);
            }   
             
             
            var re = /(?:<script.*?>)((\n|\r|.)*?)(?:<\/script>)/img; 
            var sRe = /(?:<script.*?>)/i;
            var srcRe = /\ssrc=([\'\"])(.*?)\1/i;   
            
            var match, sTagMatch, srcMatch;
          
            while(match = re.exec(html)){
                sTagMatch = match[0].match(sRe);
                if (sTagMatch) {
                    srcMatch = sTagMatch[0].match(srcRe);
                    if(srcMatch && srcMatch[2]){
                       var s = document.createElement("script");
                       s.src = srcMatch[2];
                       docHead.appendChild(s);
                    }else if(match[1] && match[1].length > 0){
                               var s = document.createElement("script");
                               s.type = 'text/javascript';
                               if(navigator.userAgent.indexOf("Safari")!=-1){//code being in a variable is important - this is Safari hack ;)
                                    window.my_code = match[1]; 
                                    s.innerHTML = 'eval(window.my_code)';
                               }
                               s.text = match[1];
                               
                               docHead.appendChild(s);
                    }                     
                }
            }
            
        }
        
        // set timeout to give DOM opportunity to catch up
        setTimeout(_parseOLDScripts, 15);
        setTimeout(_parseScripts, 20);
       

}

var sCloseBut = "<div id='info_boxClose'><A onclick='setInfo_box(); return false;' href='#'>close (x)</A></div><br><br>";
sCloseBut='';
var content_areaObjName = "mainimage";
var bSBLoad = false;
function setInfo_box(sNewContent, iAct)
{  
    var bIsSameF = false;
	if (GetElement) {
		var content_areaHeight = GetElement(content_areaObjName).offsetHeight;
		if (content_areaHeight > 0) {
		    var info_boxElement = GetElement('root1');   //GetElement('info_box');

			var infosource = GetElement(sNewContent);
			if (info_boxElement){
				v = info_boxElement.style.visibility;
			    if (sActive == iAct ){
			        v=(v=='visible')?'hidden':'visible';
			        bIsSameF = true;
		        } else {
			        sActive = iAct;
			        v = 'visible';
			        bSBLoad = false;
			    }
			}
          
            var info_boxElement2 = GetElement('scroll1Content');
            if (info_boxElement2 && infosource && (v=='visible') && !bIsSameF){
       			info_boxElement2.innerHTML ='<p>' + infosource.innerHTML + '</p>' + sCloseBut ;
            }

            if (!bSBLoad) {
                theScroll[1] = null;
                if (theThumbV[1]) {
                    temp = theThumbV[1];
                    myRemoveNode(temp);
                }
                temp = GetElement('track1');
                if (temp) myRemoveNode(temp);
                
                callInitScroller();            
                LoadIt(1);
                bSBLoad = true;
                
           }
            
            if (info_boxElement){
				info_boxElement.style.visibility = v;	
			}
	    }
	}
	//GetElement('mainimage').blur();

}
function myRemoveNode(obj){
  if (obj)   
      if (obj.removeNode)
                obj.removeNode(true);
       else { 
            var parent = obj.parentNode;
            parent.removeChild(obj);
        } 
}

function Scream(){
    alert('aaaaaa')
}
var bPLay = false;
var oD, arImD, oIm

function init_InsidePage(){     // init function for inside image and gallery pages
 //   if (bPLay) setFadeEffect();
}  

// init function for inside image and gallery pages
function init_ThumbPage(){
   // if (bPLay) setFadeEffect();

    var iColC, iRowC;
    var newIHWidth;
    //iThumbW
    if (iColC > 2){
        newIHWidth = iColC * iThumbW;    
    }
}  
function AllOnloads() {

// remove old image listener
    YAHOO.util.Event.removeListener('mainimage','mouseover');
    var a=0;
    if (GE('prices')) a += GetElement('prices').innerHTML.length;
    if (GE('infotext')) a+= GetElement('infotext').innerHTML.length;
    if (GE('iname')) a+= GetElement('iname').innerHTML.length;
    if (parseInt(a)>0) setEvents();
    sActive='';
}
function setEvents(){
    YAHOO.util.Event.on('mainimage', 'mouseover', ShowStrip, true, true);
    YAHOO.util.Event.on('mainimage', 'mouseout', HideStrip, true, true);
}
function ShowStrip(code) {
   var oEl = GetElement('info_labels');
   if (oEl) oEl.style.visibility = 'visible'; 
}
function HideStrip(){
   var oEl = GetElement('info_labels');
   if (oEl) oEl.style.visibility = 'hidden';
}
function setFadeEffect(){
    var object;
    if (!oD) oD = GE('ibox');
    if (oD){
        if (!arImD) arImD = oD.getElementsByTagName('img');
        if (arImD.length > 0 ){
            if (!oIm) oIm = arImD[0];
            sIClass = oIm.className;
            if (sIClass.indexOf('effects_') < 0) {
                if (!oIm.id) oIm.id='iFE';
                PlayFadeEff(oIm.id);
           }
        }
    }
}
var iFeTo;//Fade Effect TimeOut
function setFadeOpacity(){
    var object;
    bPLay = false;
    oD = GE('ibox');
    if (oD){
        arImD = oD.getElementsByTagName('img');
        if (arImD.length > 0 ){
      
            oIm = arImD[0];
            sIClass = oIm.className;
            // check if there is class name for effect in the img tag then set the opacity  0 which is necessary for the default fade effect
            object = oIm.style;
            if (sIClass.indexOf('effects_') < 0 ){  
                object.opacity = 0;  
                object.MozOpacity = 0;
                object.KhtmlOpacity = 0;
                object.filter = "alpha(opacity=0)";
                bPLay = true;
                iFeTo = setTimeout('PlaySetFadeEffect(oIm, 5)',5);
           }
           else {
                object.visibility = "visible";
           }

        }
    }
    
}
function PlaySetFadeEffect(oIm, iCTime){
    if (iCTime > 1000)
       { clearTimeout(iFeTo); oIm.style.visibility = 'visible'; }
    else
        if (oIm.height>0) { oIm.style.visibility = 'visible'; setFadeEffect();}
        else iFeTo = setTimeout('PlaySetFadeEffect(oIm, '+(iCTime+5)+')',5);
}
/***************** Asset Page ******************/
function init_InsideAssetPage(){

 // init_InsidePage();
}

/******* end Asset Page functions *************/

function GoG(link){
    if (link.length > 0 ) document.location.href = link;
}

function GoLink(link, obj){
    
    if (obj.getAttribute('target') && obj.getAttribute('target')=='_blank'){
        window.open(link);
   }else
        GoG(link)
}
