﻿
    //---------------------------------------- ENLARGEMEDIA ------------------------------------------------- 

    function enlargemedia(url, w, h, popshowtype) {
        if (popshowtype == "POPUP") {
            opensimplepopup(url, w, h)
        } else {
            popinmanager.showpopin(url, -1, -1, w, h)
        }
    }


    //---------------------------------------- LEGACY ENLARGERS ------------------------------------------------- 
    // params prepared in surrounding inc file:
    // var jsimagesbaseurl
    var enlargeimmaxwidth = 800
    var enlargeimheight = 750
    
    //enlargeimageinnewwindowr is rewrite-only : url form:  img-XXX-wYY-hZZ.html
    function enlargeimageinnewwindow(url) {
        window.open(url, 'image_detail', 'titlebar=no,resizable=no,scrollbars=no,status=no,left=160,top=68,width=400,height=300')
    }
        
    var currentenlargeimage = -1
    
    
    function enlargeimageinpopin(id,ratio,srctype,baseurl,optionalenlargewidth, optionalenlargeheight)
    {
        
	    if (!isNaN(optionalenlargewidth)){enlargeimmaxwidth = optionalenlargewidth}
	    if (!isNaN(optionalenlargeheight)){enlargeimmaxheight = optionalenlargeheight}
	    
	    if (id == currentenlargeimage)
	    {
		    hideenlargedimage()
	    }
	    else
	    {
		    var w, h
		    h = enlargeimheight
		    if (ratio == -1) {ratio = 701;}
    		
		    w = parseInt((1000 * h)/ratio)
		    if (w > enlargeimmaxwidth)
		    {
			    w = enlargeimmaxwidth
			    h = parseInt(w * (ratio/1000))
		    }
		    
		    var x = parseInt((getclientwidth() - w)/2)
		    x = (x < 0) ? 0 : x ;
		    var y = getClientScrollTop() + parseInt((getclientheight() - h) / 2)
		    //var y = document.body.scrollTop + 150
		    x = ( x < 0) ? 0 : x ;
		    y = ( y < 0) ? 0 : y ;

		    var newlnk
		    //check for rewritten
		    if (baseurl.indexOf('?img=') > 0) {newlnk = baseurl + 'img-' + id + '-w' + w + '-h' + h + '-t' + srctype + '.jpg'; }
		    else { newlnk = baseurl + '?id=' + id + '&h=' + h + '&w=' + w + '&type=' + srctype;  }
	        
	         
	        //OBS OBS trans disabled
		    if ( document.all && (navigator.userAgent.indexOf('Opera') == -1)) //if IE and not Opera
		    {
			    dotransenlarge(newlnk, x, y, w, h)
		    }
		    else
		    {
		        document.enlargeimageobj.src = newlnk
		        document.enlargeimageobj.style.left = Math.floor(Math.round(x)) + 'px'; //nar siden er xhtml1-transitional.dtd: formen skal være feks '11px'
		        document.enlargeimageobj.style.top = Math.floor(Math.round(y)) + 'px';
		        document.enlargeimageobj.style.width = Math.floor(Math.round(w)) + 'px'
		        document.enlargeimageobj.style.height = Math.floor(Math.round(h)) + 'px'
		        //document.enlargeimageobj.style.border = '1px solid #aaaaaa';
		        document.enlargeimageobj.style.visibility =  'visible';
		    }
		    currentenlargeimage = id
	    }
	}

	function dotransenlarge(newsource, xpos, ypos, w, h) {
	    document.body.style.filter = 'blendTrans(Duration=0.500)';
	    document.body.style.cursor = 'wait'; //'progress';
	    autopplaytransintervalid = setInterval(dotransafterimageload, 1000) //fallback: if imageonload fails filter is applied after 1 sec
	    document.body.filters.blendTrans.apply();
	    document.enlargeimageobj.style.visibility = 'visible';
	    document.enlargeimageobj.src = newsource;
	    document.enlargeimageobj.style.left = xpos;
	    document.enlargeimageobj.style.top = ypos;
	    document.enlargeimageobj.style.width = w;
	    document.enlargeimageobj.style.height = h;
	    document.enlargeimageobj.onload = dotransafterimageload;
	}

	var autopplaytransintervalid = -1
	
	function dotransafterimageload() {
	    if (autopplaytransintervalid != -1) { clearInterval(autopplaytransintervalid); autopplaytransintervalid = -1; }
	    document.body.filters.blendTrans.play();
	    document.body.style.cursor = '';
	}
    
	function getClientScrollTop() {
	    var OffWindowH = 0;
	    if (typeof self.pageYOffset != 'undefined')
	        OffWindowH = self.pageYOffset;
	    else if (document.compatMode && document.compatMode != 'BackCompat')
	        OffWindowH = document.documentElement.scrollTop;
	    else if (document.body && typeof (document.body.scrollTop) != 'undefined')
	        OffWindowH = document.body.scrollTop;
	    return OffWindowH;
	}
    
    function getclientwidth()
    {
        if (self.innerWidth)
	    {
		    return self.innerWidth;
		    //frameHeight = self.innerHeight;
	    }
	    else if (document.documentElement && document.documentElement.clientWidth)
	    {
		    return document.documentElement.clientWidth;
		    //frameHeight = document.documentElement.clientHeight;
	    }
	    else if (document.body)
	    {
		    return document.body.clientWidth;
		    //frameHeight = document.body.clientHeight;
	    }
	    else 
        {
            return 1024 ;
        }
    }
    
    function getclientheight()
    {
        if (self.innerWidth)
	    {
		    return self.innerHeight;
	    }
	    else if (document.documentElement && document.documentElement.clientWidth)
	    {
		    return document.documentElement.clientHeight;
	    }
	    else if (document.body)
	    {
		    return document.body.clientHeight;
	    }
	    else 
        {
            return 768 ;
        }
    }

    function hideenlargedimage()
    {
	    if (currentenlargeimage != -1)
	    {
		    document.enlargeimageobj.src = 'graphicsinterface/white1-1.png'
		    document.enlargeimageobj.style.visibility = 'hidden'
		    currentenlargeimage = -1
	    }
    }
    
    

    //---------------------------------------- IMAGECHANGE ------------------------------------------------- 
    
    //NOTE  imageChange functions requires initCI at page onload
        
        
    var imageObjArr = new Array()
    var altImagesArr = new Array()
    var altImagesRatioArr = new Array()
    var altImagesTypeArr = new Array()
    var loopIntervalId
    var startTime = new Date();
    var timeBtwAutoCI = 1000 
    var nextAutoCI = ticks() + timeBtwAutoCI
    
    function initCI() 
    {
        nextAutoCI = ticks() + timeBtwAutoCI
        loopIntervalId = setInterval("loopCI()", 50)
        setTimeout('clearInterval(' + loopIntervalId +')', 60000)
    }
    
    function registerForCI(imgObj, idArr, ratioArr, srctype)
    {
        imageObjArr.push(imgObj)
        altImagesArr.push(idArr)
        altImagesRatioArr.push(ratioArr)
        altImagesTypeArr.push(srctype)
        imgObj.onload = '' //avoid re-register at imagechange 
    }
    
    function rndCI() {
        var rn = parseInt(imageObjArr.length * Math.random())
        CI(rn)
    }

    function loopCI()
    {	
            if (ticks() > nextAutoCI)
	        {
			    rndCI()
		        nextAutoCI = ticks() + timeBtwAutoCI;
	        }
    }
    
    function CI(ID) 
    {
        var imobj = imageObjArr[ID]
        var altImArr = altImagesArr[ID]
        var srctype = altImagesTypeArr[ID]
        
        if (!isNaN(parseInt(altImArr)))
        {
	        var numOfAlts = altImArr.length
	        var newIX  = parseInt(numOfAlts * Math.random())
	        var newImID = altImArr[newIX]
	        var usepage = (altImagesTypeArr[ID] == '') ? 'getimagevar' : 'getimagevarintranet'
	        var newlnk
	        if (newImID!=-1)
	        {
	            //newlnk = jsimagesbaseurl + '/' + usepage + '.asp?id=' + newImID + '&w=' + (imobj.width) + '&h=' + (imobj.height) + '&type=' + srctype
	            newlnk = '/' + usepage + '.asp?id=' + newImID + '&w=' + (imobj.width) + '&h=' + (imobj.height) + '&type=' + srctype
		        defaultStatus = newlnk
		        var tmpstr = imobj.src
		        //alert(tmpstr)
		        tmpstr = tmpstr.substr(tmpstr.length - newlnk.length)
		        if (newlnk != tmpstr)
		        {
		            var ie7 = (document.all && !window.opera && window.XMLHttpRequest) ? true : false
			        if (ie7){doTrans(imobj, 1.0, newlnk)}
			        else {imobj.src = newlnk}
		        }
	        }
        }
    }

    function doTrans(idObj,nDur, nsrc, optW, optH) {
        if ( nDur <= 0 )
	        nDur = 1;
    	
        var otherfilters = idObj.style.filter	
        idObj.style.filter = otherfilters + " blendTrans(Duration=" + nDur + ")";
        idObj.filters.blendTrans.apply();
        idObj.src = nsrc;
        if (!isNaN(optW))
        {
	        idObj.style.width = optW
	        idObj.style.height = optH
        }
        idObj.filters.blendTrans.play();
    }
    
    function ticks(){return  new Date() - startTime}
    
    function doPostLoad(imObj, url)
    {
        imObj.src = url;
    }
        