﻿function Banner(name, refreshTime, width, height, start, random){
	this.objName = name;
	this.imgName = "img" + name ;
	this.lnkName = "lnk" + name ;
	this.trackImgName = "trackingImg" + name;
	this.trackingUrl = vContext + "/banner/trackview.aspx" ;
	this.clickUrl = vContext + "/banner/trackclick.aspx" ;

	eval(this.objName + "=this");

	if (!refreshTime) this.refreshTime = 5000; else this.refreshTime = refreshTime*1000;
	if (!width) this.width = 460; else this.width = width;
	if (!height) this.height = 68; else this.height = height;
	if (random == null) this.random = 1; else this.random = random;
	this.ads = [];
	if (start) this.currentAd = start-1; else start = null;
	this.mySize = 0;

	this.Ad = function(id, src, href, alt, target, mouseover) {
		var tempImage = new Image();
		tempImage.src = src;
		this.ads[this.mySize] = new Object();
		var ad = this.ads[this.mySize];
		ad.src = src;
		if (typeof(target) == "undefined" || target == null) ad.target = "_self"; else ad.target = target;
		//ad.href = href;
		ad.href = this.clickUrl + "?AdId=" + id;
		ad.mouseover = mouseover;
		ad.altText = alt;
		ad.id = id;
		this.mySize++;
	}

	this.link = function(){
		var	ad = this.ads[this.currentAd];
		if (ad.target == "_self"){
			location.href = ad.href;
		}
		else if (ad.target == "_blank" || ad.target == "_new"){
			open(ad.href,this.objName + "Win");
		}
		else top.frames[ad.target].location.href = ad.href;
	}

	this.showStatus = function(){
		var ad = this.ads[this.currentAd];
		if (ad.mouseover) status = ad.mouseover;
		else status = ad.href;
	}

	this.randomAd = function(){
		var n;
		do { n = Math.floor(Math.random() * (this.mySize)); } 
		while(n == this.currentAd);
		this.currentAd = n;
	}

	this.startAd = function(){
		setTimeout(this.objName+'.newAd()',this.refreshTime)
	}
	this.output = function(){
		var tempCode = "";
		if (this.mySize > 1){
			if (this.currentAd == null) this.randomAd();
			if (this.currentAd >= this.mySize) this.currentAd = this.mySize - 1;
			tempCode = '<a href="javascript:'+this.objName+'.link();"';
			tempCode += ' onMouseOver="' + this.objName + '.showStatus(); return true"';
			tempCode += ' onMouseOut="status=\'\';return true">';
			tempCode += '<img src="' + this.ads[this.currentAd].src + '" width="' + this.width;
			tempCode += '" name="' + this.objName + 'Img" height="' + this.height + '" ';
			if (this.altText) tempCode += 'alt="'+this.altText + '" ';
			tempCode += 'border="0" /></a>';
			document.write(tempCode);
			this.nextAd();
		} else document.write("Error: two banners must be defined for the script to work.");
	}

	this.newAd = function(){
		if (!this.random){	
			this.currentAd++;
			if (this.currentAd >= this.mySize)
			   this.currentAd = 0;
		}
		else {
			this.randomAd();
		}
		this.nextAd();
	}

	this.nextAd = function(){
		//document.images[this.objName+ 'Img'].src = ;
        var imgObj = document.getElementById(this.imgName) ;
        var lnkObj = document.getElementById(this.lnkName) ;
        var ad = this.ads[this.currentAd] ;
        if (lnkObj){
           lnkObj.href=ad.href; 
        }
        var filterFunction="blendTrans(duration=2.0)"
        var canBeFiltered=false;
        if (imgObj.style && imgObj.style.filters){
            canBeFiltered=true;
        } else if (document.getElementById(this.imgName)) {
            imgObj=document.getElementById(this.imgName);
            canBeFiltered=true;
        }

        //if (newImage.complete) {
            // SET, APPLY, PLAY FILTER
            if (canBeFiltered) {
                imgObj.style.filter=filterFunction;
                if (imgObj.filters && imgObj.filters[0]) {
                    imgObj.filters[0].Apply();
                    imgObj.filters[0].Play();
                }
            } 
        //}	

        // SWAP IMAGE
        imgObj.src=ad.src;
        imgObj.alt=ad.altText;
        this.trackAd(ad.id);

		setTimeout(this.objName+'.newAd()',this.refreshTime)
	}
	
	this.trackAd = function(id) {
        var imgObj = document.getElementById(this.trackImgName) ;
        //If Tracking Image Existing it means wnat to track opens, set the tracking URL
        if (imgObj) {
            imgObj.src = this.trackingUrl + "?AdId=" + id + "&RandomTime=" + Math.random() ;
        }
	}
}
Banner.count = 0;
/*
var frames = 0; 
function animateImages(){ 
   frames=(frames+1); 
   if (frames >= images.length) {
    frames = 0 ;
   }
   imgObj = document.getElementById(adImage) ;
   lnkObj = document.getElementById(adLink) ;
   transformPicture(imgObj, images[frames]);
   if (lnkObj){
       lnkObj.href=links[frames].value; 
   }
   imgObj.alt=altTexts[frames].value;
   trackAd(adId[frames].value) ;
   timeout_id=setTimeout("animateImages()",displayInterval); 
}
function transformPicture(oldImage, newImage, filterFunction){
    if (document.images) {
        if (filterFunction==null)
            filterFunction="blendTrans(duration=2.0)"

        var canBeFiltered=false;
        if (oldImage && oldImage.style && oldImage.style.filters){
            canBeFiltered=true;
            target=oldImage;
        } else if (document.getElementById(adImage)) {
            target= document.getElementById(adImage);
            canBeFiltered=true;
        }
        if (newImage.complete) {
            // SET, APPLY, PLAY FILTER
            if (canBeFiltered) {
                target.style.filter=filterFunction;
                if (target.filters && target.filters[0]) {
                    target.filters[0].Apply();
                    target.filters[0].Play();
                }
            } 
            // SWAP IMAGE
            oldImage.src=newImage.src;
        }	
    }
}
function trackAd(adId){
    //Get Tracking Image
    var trackingImgObj = document.getElementById(trackingImage) ;
    //If Tracking Image Existing it means wnat to track opens, set the tracking URL
    if (trackingImgObj) {
        trackingImgObj.src = trackingUrl + "?AdId=" + adId + "&RandomTime=" + Math.random() ;
    }
}
*/
