// IXF1.11 :: Image cross-fade 
// *****************************************************
// DOM scripting by brothercake -- http://www.brothercake.com/
// BB: 2008-05-20 added support for multiple crossfades/wipes at the same time...
//******************************************************
//global objects
var ixfs = new Array();
var ixws = new Array();

function getRealPosition()
{
	var pos = (arguments[1] == 'x') ? arguments[0].offsetLeft : arguments[0].offsetTop;
	var tmp = arguments[0].offsetParent;
	while(tmp != null)
	{
		pos += (arguments[1] == 'x') ? tmp.offsetLeft : tmp.offsetTop;
		tmp = tmp.offsetParent;
	}
	
	return pos;
}

function beginTransition(arguments)
{
    var img = arguments[0];
    var src = arguments[1];
    if (!img.src) //we haven't gotten the first image yet
    {
      img.src = src;
      return false;
    }
    
    var busy = img.getAttribute('intransition');
    
    if((!busy) || (typeof(busy) == 'undefined') || (busy == 'false'))
    {
        img.setAttribute('intransition', true);
        return true;
    }   
    else
    {
        return false;
    } 
}

function endTransition(img)
{
    img.setAttribute('intransition', false);
}

//crosswipe setup function
function crosswipe()
{
    if(beginTransition(arguments))
    {
        var ixw = { 'clock' : null, 'count' : 1 };

	    //copy the image object 
	    ixw.obj = arguments[0];	
    	
	    //get its dimensions
	    ixw.size = { 'w' : ixw.obj.width, 'h' : ixw.obj.height };
    	
	    //copy the image src argument 
	    ixw.src = arguments[1];
    	
	    //change the image alt text if defined
	    if(typeof arguments[4] != 'undefined' && arguments[4] != '')
	    {
		    ixw.obj.alt = arguments[4];
	    }

	    //if dynamic element creation is supported
	    if(typeof document.createElementNS != 'undefined' || typeof document.createElement != 'undefined')
	    {
		    //create a new image object and append it to body
		    //detecting support for namespaced element creation, in case we're in the XML DOM
		    ixw.newimg = document.getElementsByTagName('body')[0].appendChild((typeof document.createElementNS != 'undefined') ? document.createElementNS('http://www.w3.org/1999/xhtml', 'img') : document.createElement('img'));

		    //set positioning classname
		    ixw.newimg.className = 'idupe';

		    //set src to new image src
		    ixw.newimg.src = ixw.src

		    //move it to superimpose original image
		    ixw.newimg.style.left = getRealPosition(ixw.obj, 'x') + 'px';
		    ixw.newimg.style.top = getRealPosition(ixw.obj, 'y') + 'px';

		    //set it to be completely hidden with clip
		    ixw.newimg.style.clip = 'rect(0, 0, 0, 0)';

		    //show the image 
		    ixw.newimg.style.visibility = 'visible';

		    //copy and convert fade duration argument 
		    ixw.length = parseInt(arguments[2], 10) * 1000;

		    //create fade resolution argument as 20 steps per transition
		    ixw.resolution = parseInt(arguments[2], 10) * 20;

		    //copy slide direction argument
		    ixw.dir = arguments[3];

		    //start the timer
		    var ixwsStart = ixws.length-1;		    
    		for(var dcw=ixwsStart; dcw>=0; dcw--)
    		{
    		    if(ixws[dcw].obj.id == ixw.obj.id)
    		    {
    		        ixws.splice(dcw, 1);
    		    }
    		}  		
    		    	
            ixws.push(ixw);    		    		
		    ixw.clock = setInterval('docrosswipe(\'' + ixw.obj.id + '\')', ixw.length/ixw.resolution);
	    }
    	
	    //otherwise if dynamic element creation is not supported
	    else
	    {
		    //just do the image swap
		    ixw.obj.src = ixw.src;
	    }
	}
}

//crosswipe timer function
function docrosswipe(imgID)
{
    var ixw;
    var initialLength = ixws.length;
    
    for(var iCW=initialLength-1; iCW>=0; iCW--)
    {
        if(ixws[iCW].obj.id == imgID)
        {
            ixw = ixws[iCW];
            break;
        }
    }
    
    if((ixw) && (typeof(ixw) != 'undefined'))
    {    
        //decrease the counter on a linear scale
        ixw.count -= (1 / ixw.resolution);
    	
        //if the counter has reached the bottom
        if(ixw.count < (1 / ixw.resolution))
        {
	        //clear the timer
	        clearInterval(ixw.clock);
	        ixw.clock = null;
    		
	        //reset the counter
	        ixw.count = 1;
    		
	        //set the original image to the src of the new image
	        ixw.obj.src = ixw.src;
		    
	        endTransition(ixw.obj);  		    
        }
    	
        //animate the clip of the new image
        //using the width and height properties we saved earlier
        ixw.newimg.style.clip = 'rect('
	        + ( (/bt|bltr|brtl/.test(ixw.dir)) ? (ixw.size.h * ixw.count) : (/che|cc/.test(ixw.dir)) ? ((ixw.size.h * ixw.count) / 2) : (0) )
	        + 'px, '
	        + ( (/lr|tlbr|bltr/.test(ixw.dir)) ? (ixw.size.w - (ixw.size.w * ixw.count)) : (/cve|cc/.test(ixw.dir)) ? (ixw.size.w - ((ixw.size.w * ixw.count) / 2)) : (ixw.size.w) )
	        + 'px, '
	        + ( (/tb|tlbr|trbl/.test(ixw.dir)) ? (ixw.size.h - (ixw.size.h * ixw.count)) : (/che|cc/.test(ixw.dir)) ? (ixw.size.h - ((ixw.size.h * ixw.count) / 2)) : (ixw.size.h) )
	        + 'px, '
	        + ( (/lr|tlbr|bltr/.test(ixw.dir)) ? (0) : (/tb|bt|che/.test(ixw.dir)) ? (0) : (/cve|cc/.test(ixw.dir)) ? ((ixw.size.w * ixw.count) / 2) : (ixw.size.w * ixw.count) ) 
	        + 'px)';
    			
        //keep new image in position with original image
        //in case text size changes mid transition or something
        ixw.newimg.style.left = getRealPosition(ixw.obj, 'x') + 'px';
        ixw.newimg.style.top = getRealPosition(ixw.obj, 'y') + 'px';
    	
        //if the counter is at the top, which is just after the timer has finished
        if(ixw.count == 1)
        {
	        //remove the duplicate image
	        ixw.newimg.parentNode.removeChild(ixw.newimg);
        }
    }
}

//crossfade setup function
function crossfade()
{
    if(beginTransition(arguments))
    {
        var ixf = { 'clock' : null, 'count' : 1 };
	    //if the timer is not already going
    	
	    //copy the image object 
	    ixf.obj = arguments[0];
    	
	    //copy the image src argument 
	    ixf.src = arguments[1];
    	
	    //store the supported form of opacity
	    if(typeof ixf.obj.style.opacity != 'undefined')
	    {
		    ixf.type = 'w3c';
	    }
	    else if(typeof ixf.obj.style.MozOpacity != 'undefined')
	    {
		    ixf.type = 'moz';
	    }
	    else if(typeof ixf.obj.style.KhtmlOpacity != 'undefined')
	    {
		    ixf.type = 'khtml';
	    }
	    else if(typeof ixf.obj.filters == 'object')
	    {
		    //weed out win/ie5.0 by testing the length of the filters collection (where filters is an object with no data)
		    //then weed out mac/ie5 by testing first the existence of the alpha object (to prevent errors in win/ie5.0)
		    //then the returned value type, which should be a number, but in mac/ie5 is an empty string
		    ixf.type = (ixf.obj.filters.length > 0 && typeof ixf.obj.filters.alpha == 'object' && typeof ixf.obj.filters.alpha.opacity == 'number') ? 'ie' : 'none';
	    }
	    else
	    {
		    ixf.type = 'none';
	    }
    	
	    //change the image alt text if defined
	    if(typeof arguments[3] != 'undefined' && arguments[3] != '')
	    {
		    ixf.obj.alt = arguments[3];
	    }
    	
	    //if any kind of opacity is supported
	    if(ixf.type != 'none')
	    {
		    //create a new image object and append it to body
		    //detecting support for namespaced element creation, in case we're in the XML DOM
		    ixf.newimg = document.getElementsByTagName('body')[0].appendChild((typeof document.createElementNS != 'undefined') ? document.createElementNS('http://www.w3.org/1999/xhtml', 'img') : document.createElement('img'));

		    //set positioning classname
		    ixf.newimg.className = 'idupe';
    		
		    //set src to new image src
		    ixf.newimg.src = ixf.src

		    //move it to superimpose original image
		    ixf.newimg.style.left = getRealPosition(ixf.obj, 'x') + 'px';
		    ixf.newimg.style.top = getRealPosition(ixf.obj, 'y') + 'px';
    		
		    //copy and convert fade duration argument 
		    ixf.length = parseInt(arguments[2], 10) * 1000;
    		
		    //create fade resolution argument as 20 steps per transition
		    ixf.resolution = parseInt(arguments[2], 10) * 20;
    		
		    //start the timer
		    var ixfsStart = ixfs.length-1;		    
    		for(var dcw=ixfsStart; dcw>=0; dcw--)
    		{
    		    if(ixfs[dcw].obj.id == ixf.obj.id)
    		    {
    		        ixfs.splice(dcw, 1);
    		    }
    		}
    		
            ixfs.push(ixf);		  		    		    
		    ixf.clock = setInterval('docrossfade(\'' + ixf.obj.id + '\')', ixf.length/ixf.resolution);
	    }
    	
	    //otherwise if opacity is not supported
	    else
	    {
		    //just do the image swap
		    ixf.obj.src = ixf.src;
	    }
	}
}


//crossfade timer function
function docrossfade(imgID)
{
    var ixf;
    var initialLength = ixfs.length;
    
    for(var iZ=initialLength-1; iZ>=0; iZ--)
    {
        if(ixfs[iZ].obj.id == imgID)
        {
            ixf = ixfs[iZ];
            break;
        }
    }
        
    if((ixf) && (typeof(ixf) != 'undefined'))
    {  
        //decrease the counter on a linear scale
        ixf.count -= (1 / ixf.resolution);
    	
        //if the counter has reached the bottom
        if(ixf.count < (1 / ixf.resolution))
        {
	        //clear the timer
	        clearInterval(ixf.clock);
	        ixf.clock = null;
    		
	        //reset the counter
	        ixf.count = 1;
    		
	        //set the original image to the src of the new image
	        ixf.obj.src = ixf.src;
		    
	        endTransition(ixf.obj);		    
        }
    	
        //set new opacity value on both elements
        //using whatever method is supported
        switch(ixf.type)
        {
	        case 'ie' :
		        ixf.obj.filters.alpha.opacity = ixf.count * 100;
		        ixf.newimg.filters.alpha.opacity = (1 - ixf.count) * 100;
		        break;
    			
	        case 'khtml' :
		        ixf.obj.style.KhtmlOpacity = ixf.count;
		        ixf.newimg.style.KhtmlOpacity = (1 - ixf.count);
		        break;
    			
	        case 'moz' : 
		        //restrict max opacity to prevent a visual popping effect in firefox
		        ixf.obj.style.MozOpacity = (ixf.count == 1 ? 0.9999999 : ixf.count);
		        ixf.newimg.style.MozOpacity = (1 - ixf.count);
		        break;
    			
	        default : 
		        //restrict max opacity to prevent a visual popping effect in firefox
		        ixf.obj.style.opacity = (ixf.count == 1 ? 0.9999999 : ixf.count);
		        ixf.newimg.style.opacity = (1 - ixf.count);
        }
    	
        //now that we've gone through one fade iteration 
        //we can show the image that's fading in
        ixf.newimg.style.visibility = 'visible';
    	
        //keep new image in position with original image
        //in case text size changes mid transition or something
        ixf.newimg.style.left = getRealPosition(ixf.obj, 'x') + 'px';
        ixf.newimg.style.top = getRealPosition(ixf.obj, 'y') + 'px';
    	
        //if the counter is at the top, which is just after the timer has finished
        if(ixf.count == 1)
        {
	        //remove the duplicate image
	        ixf.newimg.parentNode.removeChild(ixf.newimg);
        }
    }
}

if (!mmbo)
	var mmbo = [];

/*
 oImg - 'b_oImg'
 pimgObj - 'b_imgObj'
 pflashID - 'b_themovie'
 bannerID - ID of banner or -1
 imageUrl - url of bannerimage with p=... parameter
 */
 
function MMBanner(pbannerID, pimgDisplayType, poImg, pimgObj, pflashID, ptimeout, 
	pimageLink, pclickUrl, pimageUrl, pfilterNonIE, pimageDataArray)
{
	this.id = mmbo.length;
	mmbo[this.id] = this;
	this.bannerID = pbannerID;
	this.imgDisplayType = pimgDisplayType;
	this.bannerImgID = '0';
	this.oImg = document.getElementById(poImg);
	this.imgObj = document.getElementById(pimgObj);
	this.imageLink = document.getElementById(pimageLink);
	this.imgID = '0';
	this.altImgID = '0';
	this.linkTarget = 'S';
	this.altTekst = '';
	this.hasUrl = '';
	this.timeout = ptimeout;
	this.clickUrl = pclickUrl;
	this.imageUrl = pimageUrl;
	this.flashID = pflashID;
	this.counter = 0;
	this.filterNonIE = pfilterNonIE;
	this.endImage = '';
	this.currentImageIndex = 0;
	
	this.parsingIndex = 0;
	this.totalWeight = 0;
	for (var i=0; i<pimageDataArray.length; i++)
		this.totalWeight += pimageDataArray[i][4];
	
	this.getSimpleIndex = function(nr)
	{
		if (nr < 0 || nr >= this.totalWeight)
			return 0;
		for (var i=0; i<pimageDataArray.length; i++)
		{
			if (nr < pimageDataArray[i][4])
				return i;
			nr -= pimageDataArray[i][4];
		}
		return 0;
	}

	this.getServerImageData = function(objType) {
	    //get next image to show
	    var i;
	    //var prevImgID;
	    if (this.imgDisplayType == "sort") {
	        i = this.currentImageIndex = this.getSimpleIndex((this.parsingIndex++) % this.totalWeight);
	    }
	    else {
	        i = this.currentImageIndex = this.getSimpleIndex(Math.floor(Math.random() * this.totalWeight));
	    }

	    //WAT IS THIS?????
        /*
	    if (i > 0) {
	        prevImgID = pimageDataArray[i - 1][0];
	    } else {
	        prevImgID = pimageDataArray[0][0];
	    }*/

	    this.bannerImgID = pimageDataArray[i][0];
	    this.imgID = pimageDataArray[i][1];
	    this.altImgID = pimageDataArray[i][2];

	    if (i > 0) {
	        this.altTekst = pimageDataArray[i - 1][3];
	    } else {
	        this.altTekst = pimageDataArray[0][3];
	    }

	    this.weight = pimageDataArray[i][4];
	    this.linkTarget = pimageDataArray[i][5];
	    this.hasUrl = pimageDataArray[i][6];

	    if (objType == 'imageBanner') {
	        if (this.imageLink != null) {
	            if (this.hasUrl == 1) {
	                this.imageLink.href = this.clickUrl + "&i=" + this.bannerImgID; //prevImgID; 
	                this.imageLink.target = this.getLinkTarget(this.linkTarget);
	                if (this.oImg != null)
	                    this.oImg.style.cursor = 'pointer';
	            }
	            else {
	                //if the image has no link then it should not react to clicking
	                this.imageLink.href = 'javascript://;';
	                this.imageLink.target = '';
	                if (this.oImg != null)
	                    this.oImg.style.cursor = 'default';
	            }
	        }
	        if (this.oImg != null) {
	            this.oImg.alt = this.altTekst;
	            this.oImg.title = this.altTekst;
	        }
	    }
	}
	
	this.getLinkTarget = function(lTarget)
	{
		switch(lTarget)
		{
			case 'N' : return '_blank';
			case 'T' : return '_top';
			default  : return '_self';
		}
	}

	this.getImage = function() {
	    // counter is different each time to prevent browser caching of url		
	    if (this.imgID == 0)
	        this.getServerImageData('imageBanner');

	    return this.imageUrl + "&im=" + this.imgID + "&c=" + this.counter++;
	}


	this.initBanner = function() {
	    var oldLoad = window.onload;
	    var id = this.id;
	    window.onload = function() {
        MMBannerTimeout(id, 1);
        if (oldLoad) oldLoad();
      }
	}
	
	this.mkTransitionNonIE = function()
	{
		this.endImage = this.getImage();
		this.getServerImageData('imageBanner');
		eval(this.filterNonIE);		
		this.setTimer(1);
	}
	
    
    this.checkBrowser = function(string)
	{
		var detect = navigator.userAgent.toLowerCase();
		place = detect.indexOf(string);
		return place != -1;
	}
	
	this.changeFlash = function()
	{	
		this.getServerImageData('swfBanner');
		var flashSrc = this.getImage();
	
		var txt = "";
		txt += "<object classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000' id='"+this.flashID+"' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=5,0,0,0'>";
		txt += "<param name='quality' value='high'>";
		txt += "<param name='wmode' value='transparent'/>";
		txt += "<param name='movie' value='" + flashSrc + "'> ";
		txt += "<param name='FlashVars' value='clickUrl="+this.clickUrl+"&i="+this.bannerImgID+"' />"		
		txt += "<embed src='" + flashSrc + "' FlashVars='clickUrl="+this.clickUrl+"&i="+this.bannerImgID+"' quality='high' type='application/x-shockwave-flash' name='"+this.flashID+"' id='"+this.flashID+"' pluginspage='http://www.macromedia.com/shockwave/download/index.cgi?p1_prod_version=shockwaveflash' wmode='transparent'></embed>";
		txt += "</object>";	
		
		this.imgObj.innerHTML = txt;
		this.setTimer(2);
	}
	
	this.setTimer = function(tType)
	{		
	    if(this.timeout > 0)
	    {
    	    setTimeout('MMBannerTimeout('+this.id+','+tType+')', this.timeout);
    	}
	}
}

function MMBannerTimeout(id, toType)
{
	if (mmbo[id] == null)
		return;
	switch (toType)
	{
		case 1:
			mmbo[id].mkTransitionNonIE();
			break;
		case 2:
			mmbo[id].changeFlash();
			break;
	}
}

function getBannerObj(imgObj)
{
	if( imgObj.getAttribute('bannerObj')==null )	
		return;
	return eval(imgObj.getAttribute('bannerObj'));
}

function getMouseOverAction(obj)
{
	bannerVar = getBannerObj(obj);
	if(bannerVar!=null)
		if (bannerVar.altImgID!='0' && bannerVar.altImgID!='')
		{
			obj.src = bannerVar.imageUrl + "&mo="+bannerVar.altImgID + "&c=" + bannerVar.counter++;				
		}
}

function imageLoaded(obj)
{	
	bannerVar = getBannerObj(obj);	
	if( bannerVar!=null )
	{
		bannerVar.imgObj.style.width = obj.width;
		bannerVar.imgObj.style.height = obj.height;
	}
}

function getMouseOutAction(obj)
{
	bannerVar = getBannerObj(obj);	
	if(bannerVar!=null)
		if (bannerVar.altImgID!='0' && bannerVar.altImgID!='')
		{
			obj.src = bannerVar.imageUrl + "&mo="+bannerVar.imgID + "&c=" + bannerVar.counter++;
		}
}
