/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


Object.prototype.addEvent = function(evtType, func) {
    if (this.addEventListener) {
        this.addEventListener(evtType, func, true);
    } else if (this.attachEvent) {
        this.attachEvent('on' + evtType, func);
    } else {
        this['on' + evtType] = func;
    }
}

function handleEvent(obj, evtType, func){
    if (obj.addEventListener) {
        obj.addEventListener(evtType, func, true);
    } else if (obj.attachEvent) {
        obj.attachEvent(evtType, func);
    } else {
        obj['on' + evtType] = func;
    }
}

function whichBrs() {
    var agt=navigator.userAgent.toLowerCase();
    if (agt.indexOf("opera") != -1) return 'Opera';
    if (agt.indexOf("staroffice") != -1) return 'Star Office';
    if (agt.indexOf("webtv") != -1) return 'WebTV';
    if (agt.indexOf("beonex") != -1) return 'Beonex';
    if (agt.indexOf("chimera") != -1) return 'Chimera';
    if (agt.indexOf("netpositive") != -1) return 'NetPositive';
    if (agt.indexOf("phoenix") != -1) return 'Phoenix';
    if (agt.indexOf("firefox") != -1) return 'Firefox';
    if (agt.indexOf("safari") != -1) return 'Safari';
    if (agt.indexOf("skipstone") != -1) return 'SkipStone';
    if (agt.indexOf("msie") != -1) return 'Internet Explorer';
    if (agt.indexOf("netscape") != -1) return 'Netscape';
    if (agt.indexOf("mozilla/5.0") != -1) return 'Mozilla';
    if (agt.indexOf('\/') != -1) {
        if (agt.substr(0,agt.indexOf('\/')) != 'mozilla') {
            return navigator.userAgent.substr(0,agt.indexOf('\/'));
        }else return 'Netscape';
    } else if (agt.indexOf(' ') != -1)
        return navigator.userAgent.substr(0,agt.indexOf(' '));
    else return navigator.userAgent;
}

function SlideShow(slideel, fadingSpeed, stopTime, stopOnMouseOver) {
    var mouseIsOver = false;
    var nexttime= null;
    var self = this;
    var ie = (document.all) ? 1 : 0;
    var p = (ie) ? "filter" : "MozOpacity";
    var browser = whichBrs();
    if(browser=='Safari'){
        p = "KhtmlOpacity";
    }

    function op(n,v){
        v=v*100;
        v = (ie) ? "alpha(opacity="+v+")" : v/100;
        n.style[p] = v;
    }

    function createSlides() {
        var imgs = slideel.getElementsByTagName('img');
        var slides = [];

        for (var i = 0; i < imgs.length; i++) {
            slides[i] = new Slide(imgs[i], self);
        }
        for (var i = 0; i < slides.length; i++) {
            if (i == slides.length - 1)
                slides[i].nextSlide = slides[0];
            else
                slides[i].nextSlide = slides[i + 1];
        }

        if(slides.length!=0){
            self.current =  slides[0];
            slides[0].start();
        }
        function Slide(img, slideShow) {
            op(img,'0');
            //img.style.opacity = '0';

            this.start = function() {
                window.setTimeout(function() {
                    fadeIn(10);
                }, fadingSpeed);
            }

            this.stop = function() {
                window.setTimeout(function() {
                    fadeOut(10);
                }, fadingSpeed);
            }
            function fadeIn(i) {
                //img.style.opacity = 1. - i/10.;
                var opac = 1. - i/10.;
                op(img, opac);
                if(--i>=0) {
                    window.setTimeout(function() {
                        fadeIn(i);
                    }, fadingSpeed);
                } else {
                    nexttime= window.setTimeout(function() {
                        self.next();
                    }, 10 * fadingSpeed + stopTime);
                }
            }
            function fadeOut(i) {
                outtime= null;
                //img.style.opacity = i/10.;
                var opac1 = i/10.;
                op(img, opac1);
                if(--i>=0) {
                    window.setTimeout(function() {
                        fadeOut(i);
                    }, fadingSpeed);
                }
            }
        }
    }

    createSlides(slideel);

    if (stopOnMouseOver) {
        //slideel.addEvent('mouseover', function() {mouseIsOver = true;});
        handleEvent(slideel, 'onmouseover', function() {mouseIsOver = true;});
        handleEvent(slideel, 'onmouseout', function() {
             mouseIsOver = false;
             if(nexttime!=null) {
                 window.clearTimeout(nexttime);
                 self.next();
             }
         });
        /*
         slideel.addEvent('mouseout', function() {
             mouseIsOver = false;
             if(nexttime!=null) {
                 window.clearTimeout(nexttime);
                 self.next();
             }
         });
         */
    }

        this.next = function() {
        nexttime= null;
        if (mouseIsOver) {
            window.setTimeout(function() {
                self.next();
            }, 100);
            return;
        }
        this.current.stop();
        this.current = this.current.nextSlide;
        this.current.start();
    }


}