﻿// arguments: image id, rotation speed, path to images (optional), 
// new (optional) arguments: for transitions, mouse events and random rotation 
function circ_img(id, speed, path, bTrans, bMouse, bRand) {
    var imgObj = document.getElementById(id); 
    if (!imgObj) { // in case name, not id attached to image
        imgObj = document.images[id];
        if (!imgObj) return;
        imgObj.id = id;
    }
    this.id = id; this.speed = speed || 4500; // default speed of rotation
    this.path = path || "";  this.bRand = bRand;
    this.ctr = 0; this.timer = 0; this.imgs = []; 
    this._setupLink(imgObj, bMouse);
    this.bTrans = bTrans && typeof imgObj.filters != 'undefined';
    var index = circ_img.col.length; circ_img.col[index] = this;
    this.animString = "circ_img.col[" + index + "]";
}

circ_img.col = []; // hold instances
circ_img.resumeDelay = 400; // onmouseout resume rotation after delay

// mouse events pause/resume
circ_img.prototype._setupLink = function(imgObj, bMouse) { 
    if ( imgObj.parentNode && imgObj.parentNode.tagName.toLowerCase() == 'a' ) {
        var parentLink = this.parentLink = imgObj.parentNode;
        if (bMouse) {
            Circ_Event.add(parentLink, 'mouseover', circ_img.pause);
            Circ_Event.add(parentLink, 'mouseout', circ_img.resume);
        }
    }
}

// so instance can be retrieved by id (as well as by looping through col)
circ_img.getInstanceById = function(id) {
    var len = circ_img.col.length, obj;
    for (var i=0; i<len; i++) {
        obj = circ_img.col[i];
        if (obj.id && obj.id == id ) {
            return obj;
        }
    }
    return null;
}

circ_img.prototype.on_rotate = function() {}

circ_img.prototype.addImages = function() { // preloads images
    var img;
    for (var i=0; arguments[i]; i++) {
        img = new Image();
        img.src = this.path + arguments[i];
        this.imgs[this.imgs.length] = img;
    }
}

circ_img.prototype.rotate = function() {
    clearTimeout(this.timer); this.timer = null;
    var imgObj = document.getElementById(this.id);
    if ( this.bRand ) {
        this.setRandomCtr();
    } else {
        if (this.ctr < this.imgs.length-1) this.ctr++;
        else this.ctr = 0;
    }
    if ( this.bTrans ) {
        this.doImageTrans(imgObj);
    } else {
        imgObj.src = this.imgs[this.ctr].src;
    }
    this.swapAlt(imgObj); this.prepAction(); this.on_rotate();
    this.timer = setTimeout( this.animString + ".rotate()", this.speed);   
}

circ_img.prototype.setRandomCtr = function() {
    var i = 0, ctr;
    do { 
        ctr = Math.floor( Math.random() * this.imgs.length );
        i++; 
    } while ( ctr == this.ctr && i < 6 )// repeat attempts to get new image, if necessary
    this.ctr = ctr;
}

circ_img.prototype.doImageTrans = function(imgObj) {
    imgObj.style.filter = 'blendTrans(duration=1)';
    if (imgObj.filters.blendTrans) imgObj.filters.blendTrans.Apply();
    imgObj.src = this.imgs[this.ctr].src;
    imgObj.filters.blendTrans.Play(); 
}

circ_img.prototype.swapAlt = function(imgObj) {
    if ( !imgObj.setAttribute ) return;
    if ( this.alt && this.alt[this.ctr] ) {
        imgObj.setAttribute('alt', this.alt[this.ctr]);
    }
    if ( this.title && this.title[this.ctr] ) {
        imgObj.setAttribute('title', this.title[this.ctr]);
    }
}

circ_img.prototype.prepAction = function() {
    if ( this.actions && this.parentLink && this.actions[this.ctr] ) {
        if ( typeof this.actions[this.ctr] == 'string' ) {
            this.parentLink.href = this.actions[this.ctr];
        } else if ( typeof this.actions[this.ctr] == 'function' ) {
            // to execute function when linked image clicked 
            // passes id used to uniquely identify instance  
            // retrieve it using the circ_img.getInstanceById function 
            // so any property of the instance could be obtained for use in the function 
            var id = this.id;
            this.parentLink.href = "javascript: void " + this.actions[this.ctr] + "('" + id + "')";
        } 
    }
}

circ_img.prototype.showCaption = function() {
    if ( this.captions && this.captionId ) {
        var el = document.getElementById( this.captionId );
        if ( el && this.captions[this.ctr] ) {
            el.innerHTML = this.captions[this.ctr];
        }
    }
}

// Start rotation for all instances 
circ_img.start = function() {
    var len = circ_img.col.length, obj;
    for (var i=0; i<len; i++) {
        obj = circ_img.col[i];
        if (obj && obj.id ) 
            obj.timer = setTimeout( obj.animString + ".rotate()", obj.speed);
    }
}

// Stop rotation for all instances 
circ_img.stop = function() {
    var len = circ_img.col.length, obj;
    for (var i=0; i<len; i++) {
        obj = circ_img.col[i];
        if (obj ) { clearTimeout(obj.timer); obj.timer = null; }
    }
}

// for stopping/starting (onmouseover/out)
circ_img.pause = function(e) {	
    e = Circ_Event.DOMit(e);
    var id = e.target.id;
    var obj = circ_img.getInstanceById(id);
    if ( obj ) { clearTimeout( obj.timer ); obj.timer = null; }
}

circ_img.resume = function(e) {
    e = Circ_Event.DOMit(e);
    var id = e.target.id;
    var obj = circ_img.getInstanceById(id);
    if ( obj && obj.id ) {
        obj.timer = setTimeout( obj.animString + ".rotate()", circ_img.resumeDelay );
    }
}

/////////////////////////////////////////////////////////////////////
// Use this function to set up when using object literals to hold data 
// it calls constructor, addImages, adds actions, etc.

circ_img.setup = function () {
    if (!document.getElementById) return;
    var i, j, rObj, r, imgAr, len;
    for (i=0; arguments[i]; i++) {
        rObj = arguments[i];
        r = new circ_img(rObj.id, rObj.speed, rObj.path, rObj.bTrans, rObj.bMouse, rObj.bRand);
        try {
            imgAr = rObj.images; len = imgAr.length;
            for (j=0; j<len; j++) { r.addImages( imgAr[j] ); }
            if( rObj.num ) r.ctr = rObj.num; // for seq after random selection
            if ( rObj.actions && rObj.actions.length == len ) {
                r.addProp('actions', rObj.actions);
            }
            if ( rObj.alt && rObj.alt.length == len ) {
                r.addProp('alt', rObj.alt);
            }
            if ( rObj.title && rObj.title.length == len ) {
                r.addProp('title', rObj.title);
            }
            if ( rObj.captions ) {
                r.addProp('captions', rObj.captions);
                r.captionId = rObj.captionId;
                circ_img.addRotateEvent(r, function () { circ_img.getInstanceById(rObj.id).showCaption(); } ); 
            }
        } catch (e) { 
            //alert(e.message); 
        }
    }
    circ_img.start();
}

// add to on_rotate for specified instance (r)
// see usage above for captions
circ_img.addRotateEvent = function( r, fp ) {
    var old_on_rotate = r.on_rotate;
    r.on_rotate = function() { old_on_rotate(); fp(); }
}

// for adding actions, alt, title
circ_img.prototype.addProp = function(prop, ar) {
    if ( !this[prop] ) {
        this[prop] = [];
    }
    var len = ar.length; 
    for (var i=0; i < len; i++) {
        this[prop][ this[prop].length ] = ar[i]; 
    }
}

//event file 
var Circ_Event = {
  
    add: function(obj, etype, fp, cap) {
        cap = cap || false;
        if (obj.addEventListener) obj.addEventListener(etype, fp, cap);
        else if (obj.attachEvent) obj.attachEvent("on" + etype, fp);
    }, 

    remove: function(obj, etype, fp, cap) {
        cap = cap || false;
        if (obj.removeEventListener) obj.removeEventListener(etype, fp, cap);
        else if (obj.detachEvent) obj.detachEvent("on" + etype, fp);
    }, 
    
    DOMit: function(e) { 
        e = e? e: window.event; // e IS passed when using attachEvent though ...
        if (!e.target) e.target = e.srcElement;
        // don't seem to work if not using attachEvent (moral: be consistent, use old OR new models)
        if (!e.preventDefault) e.preventDefault = function () { e.returnValue = false; return false; }
        if (!e.stopPropagation) e.stopPropagation = function () { e.cancelBubble = true; }
        return e;
    },
    
    getTarget: function(e) {
        e = Circ_Event.DOMit(e); var tgt = e.target; 
        if (tgt.nodeType != 1) tgt = tgt.parentNode; // safari...
        return tgt;
    }
    
}

// Danny Goodman's version (DHTML def ref)
function addLoadEvent(func) {
    var oldQueue = window.onload? window.onload: function() {};
    window.onload = function() {
        oldQueue();
        func();
    }
}

//end rotator code

//aux file 
// display image at random
// rObj: object literal holding data 
function Circ_getRandomImage(rObj) {
    var imgAr = rObj.images;  if (!imgAr ) return;
    var num = Math.floor( Math.random() * imgAr.length );
    var imgStr = '';   var imgFile = imgAr[ num ];
    rObj.num = num; // hold which img selected
    var path = rObj.path || ''; var id = rObj.id || '';
    var title, alt = '', url;
    // If there are *any* entries for actions, alt or title include them here 
    if (rObj.alt) {
        alt = rObj.alt[num]? rObj.alt[num]: rObj.alt[0]? rObj.alt[0]: '';
    }
    if (rObj.title) {
        title = rObj.title[num]? rObj.title[num]: rObj.title[0]? rObj.title[0]: '';
    }
    if (rObj.actions) {
        url = rObj.actions[num]? rObj.actions[num]: rObj.actions[0]? rObj.actions[0]: null;
    }
    if (url) {
        imgStr += '<a href="';
        imgStr += typeof url == 'string'? url: 'javascript: void ' + url;
        imgStr += '">';
    }
    
    imgStr += '<img src="' + path + imgFile + '"';
    imgStr += id? ' id="' + id + '"': '';
    if (title) {
        imgStr += ' title="' + title + '"';
    }
    imgStr += ' alt="' + alt + '" border="0" />';
    if (url) {
        imgStr += '</a>';
    }
    document.write(imgStr); document.close();
}

/////////////////////////////////////////////////////////////////////
//  code to add stop/restart links

circ_img.addControls = function() {
    var els = Circ_getElementsByClassName('rotator_controls');
    for (var i=0; els[i]; i++) {
        var links = els[i].getElementsByTagName('a');
        for (var j=0; links[j]; j++) {
            if ( Circ_hasClass( links[j], 'stop') ) {
                links[j].onclick = function () { circ_img.stop(); return false }
            } else if ( Circ_hasClass( links[j], 'start') ) {
                links[j].onclick = function () { circ_img.restart(); return false }
            } 
        }
        els[i].style.display = 'block';
    }
}

// restart rotation for all instances 
circ_img.restart = function() {
    var len = circ_img.col.length, obj;
    for (var i=0; i<len; i++) {
        obj = circ_img.col[i];
        if (obj && obj.id ) //obj.rotate(); // no delay? 
            obj.timer = setTimeout( obj.animString + ".rotate()", circ_img.resumeDelay );
    }
}

function Circ_hasClass(el, cl) {
    var re = new RegExp("\\b" + cl + "\\b", "i");
    if ( re.test( el.className ) ) {
        return true;
    }
    return false;
}

function Circ_getElementsByClassName(sClass, sTag, oCont) {
    var result = [], list, i;
    var re = new RegExp("\\b" + sClass + "\\b", "i");
    oCont = oCont? oCont: document;
    if ( document.getElementsByTagName ) {
        if ( !sTag || sTag == "*" ) {
            list = oCont.all? oCont.all: oCont.getElementsByTagName("*");
        } else {
            list = oCont.getElementsByTagName(sTag);
        }
        for (i=0; list[i]; i++) 
            if ( re.test( list[i].className ) ) result.push( list[i] );
    }
    return result;
};


/////////////////////////////////////////////////////////////////////
// example use of function pointer in actions
// id: id by which the instance can be obtained using circ_img.getInstanceById
// (id passed to constructor - id attached to img tag)
function displayImgInSubWin(id) {
    var rObj = circ_img.getInstanceById(id);
    var file = rObj.imgs[rObj.ctr].src;
    openSubWin(file);
    return false;
}

// arguments: file to open, subwindow name, left, top, width, height, other attributes
// common attributes: (comma separator, no spaces!)
// "resizable,scrollbars,toolbar,location,directories,status,menubar"
// all but url are optional with defaults provided below 
function openSubWin(url, nm, x, y, w, h, atts) {
    nm = nm || "subwindow";
    atts = atts || "menubar,resizable,scrollbars";
    w = w || 600; h = h || 450;
    x = (typeof x=="number")? x: Math.round( (screen.availWidth - w)/2 );
    y = (typeof y=="number")? y: Math.round( (screen.availHeight - h)/2 );
    atts += ',width='+w+',height='+h+',left='+x+',top='+y;
    var win = window.open(url, nm, atts); 
    if (win) {
        if (!win.closed) { win.resizeTo(w,h); win.moveTo(x,y); win.focus(); return false; }
    } 
    return true;
}


//other required
var rotator1 = {
    path: './CMS/Images/PromoImages/HomePageCenter/',  // path to your images
    id:   'imgDefaultPromo',   // id assigned in image tag
    bTrans: true, // transition filter for IE Win
    images: ['1.jpg', '2.jpg', '3.jpg', '4.jpg','5.jpg'],
    speed:  4500,
    bMouse: true,
    actions: [ 'http://Fechtest/',
               'http://Fechtest/',
               'http://Fechtest/',
               'http://Fechtest/',
               'http://Fechtest/'
            ] 
                
    
}

//other required
var rotator2 = {
    path: './CMS/Images/PromoImages/LeftNavPromo/',  // path to your images
    id:   'imgLeftNavAnimate',   // id assigned in image tag
    bTrans: true, // transition filter for IE Win
    images: ['leftFrame_1.jpg', 'leftFrame_2.jpg', 'leftFrame_3.jpg', 'leftFrame_4.jpg','leftFrame_5.jpg','leftFrame_6.jpg','leftFrame_7.jpg','leftFrame_8.jpg','leftFrame_9.jpg','leftFrame_10.jpg','leftFrame_10.jpg','leftFrame_10.jpg','leftFrame_10.jpg','leftFrame_10.jpg','leftFrame_10.jpg','leftFrame_10.jpg','leftFrame_10.jpg','leftFrame_10.jpg','leftFrame_10.jpg','leftFrame_10.jpg'],
    speed:  500,
    bMouse: true,
    actions: [ '',
               '',
               '',
               '',
               '',
               '',
               '',
               '',
               '',
               'https://www.rkb.us/contentdetail.cfm?content_id=200647',
               'https://www.rkb.us/contentdetail.cfm?content_id=200647',
               'https://www.rkb.us/contentdetail.cfm?content_id=200647',
               'https://www.rkb.us/contentdetail.cfm?content_id=200647',
               'https://www.rkb.us/contentdetail.cfm?content_id=200647',
               'https://www.rkb.us/contentdetail.cfm?content_id=200647',
               'https://www.rkb.us/contentdetail.cfm?content_id=200647',
               'https://www.rkb.us/contentdetail.cfm?content_id=200647',
               'https://www.rkb.us/contentdetail.cfm?content_id=200647',
               'https://www.rkb.us/contentdetail.cfm?content_id=200647',
               'https://www.rkb.us/contentdetail.cfm?content_id=200647'
            ] 
                
    
}


//addLoadEvent(initRotator); //Homepage Promo 
addLoadEvent(initRotator2); //Left Nav Animation


function initRotator() {
    // pass name of variable containing rotator properties
    circ_img.setup(rotator1);
}


function initRotator2() {
    // pass name of variable containing rotator properties
    circ_img.setup(rotator2);
}





