﻿/*
	Image Cross Fade Redux
	Version 1.0
	Last revision: 02.15.2006
	steve@slayeroffice.com

	Rewrite of old code found here: http://slayeroffice.com/code/imageCrossFade/index.html
*/

//window.addEventListener?window.addEventListener('load',so_init,false):window.attachEvent('onload',so_init);

function rotator(id, itemTagName, pauseTime, isRandom, banners, width, height) {
    this.ID = id;
    this.ItemTagName = itemTagName;
    this.PauseTime = pauseTime;
    this.IsRandom = isRandom;
    this.Banners = banners;
    this.BannerWidth = width;
    this.BannerHeight = height;
    
    this.objs = new Array();
    this.current = 0;    
    
    if (this.IsRandom) {
        //shuffle the banners items
    }        
    this.renderBanners = renderBanners;
    this.start = start;
    this.fade = xfade;
}

function xfade() {   
    with (this) {                    
        cOpacity = objs[current].xOpacity;
        nIndex = objs[current+1]?current+1:0;
        nOpacity = objs[nIndex].xOpacity;

        cOpacity-=.05;
        nOpacity+=.05;

        objs[nIndex].style.display = 'block';
        objs[current].xOpacity = cOpacity;
        objs[nIndex].xOpacity = nOpacity;

        setOpacity(objs[current]);
        setOpacity(objs[nIndex]);

        if(cOpacity<=0) {
	        objs[current].style.display = 'none';
	        current = nIndex;		    	   	    
	        setTimeout(function(){fade();}, PauseTime);
        } else {		    	    
	        setTimeout(function(){fade();}, 50);
        }    
    }
}


function renderBanners() {
    with(this) {
        if (Banners == null || Banners.length == 0) return;
    
        for (i=0; i<Banners.length; i++) {
            var banner = Banners[i]; //0: image type; 1: image path; 2: link; 3: alt; 4: expiry date       
            
            if (banner != null) {
                show = true;
                //check expiry date
                if (banner.expdate!=null && banner.expdate!='') {
                   days = banner.expdate.split("/");
			       edate = new Date(days[2], days[1], days[0])
			       if (edate < today) show=false;
                }
                
                if (show) {
                    switch(banner.type) {
                        case 'img':
                            document.write('<div><a href="'+banner.link+'" target="_blank"><img src="'+banner.path+'" width="'+BannerWidth+'px" height="'+BannerHeight+'px"  alt="'+banner.alt+'" border="0" /></a></div>');
                            break;
                        case 'swf':
                            document.write('<div>');
                            document.write('<a href="'+banner.link+'" target="_blank">');
                            document.write('<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" width="'+BannerWidth+'" height="'+BannerHeight+'" align="middle">');
                            document.write('<param name="allowScriptAccess" value="sameDomain" />');
                            document.write('<param name="movie" value="'+banner.path+'" />');
                            document.write('<param name="quality" value="high" />');
                            document.write('<param name="bgcolor" value="#ffffff" />');
                            document.write('<embed src="'+banner.path+'" quality="high" bgcolor="#ffffff" width="'+BannerWidth+'" height="'+BannerHeight+'" align="middle" allowscriptaccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />');
                            document.write('</object>');
                            document.write('</a>');
                            document.write('</div>');
                            break;
                    }
                }            
            }
        }
    }
}

function start() {
	if(!document.getElementById || !document.createElement)return;
    with (this) {
        //init objs
        objs = document.getElementById(this.ID).getElementsByTagName(this.ItemTagName);        
        for(i=1;i<objs.length;i++) objs[i].xOpacity = 0;
        objs[0].style.display = 'block';
        objs[0].xOpacity = .99;
    	
	    //call xfade 
	    setTimeout(function(){fade();}, PauseTime);	
	}
}



function setOpacity(obj)
{
	if(obj.xOpacity>.99) {
		obj.xOpacity = .99;
		return;
	}

	obj.style.opacity = obj.xOpacity;
	obj.style.MozOpacity = obj.xOpacity;
	obj.style.filter = 'alpha(opacity=' + (obj.xOpacity*100) + ')';
}
