/* contentlist.js */
if (typeof SwJs == "undefined") SwJs = { };

if (typeof SwJs.Preview == "undefined") SwJs.Preview = {

    running: false,
    index: -1,
    flasher: false,
    object_aref: null,
    object1: null,
    object_x: 0,
    object_y: 0,
    object_ticks: null,
    object_frames: null,
    object_ul: null,
    object_ul_rect: null,
    basename: null,
    timer_start: null,
    image: null,
    timer: null,
    run_delay: ($.browser.msie)?400:500,
    preload_index: 1,
    timeout: 0,
    
    Init: function() {
        $("a.frame").mouseover(function(event) {
            SwJs.Preview.Start_obj(this,event);
        }).mouseout(function(event) {
            SwJs.Preview.Stop();
        }).mousemove(function(event) {
            SwJs.Preview.Track(this,event);
        });
    },
    Start_obj: function(sender, evt) {
        this.object_aref = sender;
        this.object_frames = this.object_aref.parentNode.getElementsByTagName('img');
        this.object_ul = this.object_aref.parentNode.getElementsByTagName('ul')[0];
        this.object_ul_rect = this.getbounds(this.object_ul);

        var screenshot = this.object_frames[0].src;
        var baselen = screenshot.lastIndexOf('-1');
        if (baselen < 0) baselen = screenshot.lastIndexOf('.');
        if (baselen < 0) return;
        this.basename = screenshot.substring(0, baselen) + '-';

        this.object1 = this.object_frames[0];
        if (this.object_frames.length == 1) {
            for (i = 1; i <= 7; i++) {
	  this.object_frames[i] = new Image();
                this.object_frames[i].style.visibility = "hidden";
                this.object_aref.parentNode.appendChild(this.object_frames[i]);
            }
            this.preload_index = 2;
            var callbackRef = this;
            setTimeout(function() { callbackRef.PreloadImage(); }, 5);
        }
        this.object_x = 0;
        this.object_y = 0;
        for (obj1 = sender; obj1; obj1 = obj1.offsetParent) {
            var rect1 = this.getbounds(obj1);
            this.object_x += rect1.left;
            this.object_y += rect1.top;
        }
        this.object_ticks = this.object_aref.parentNode.getElementsByTagName('li');
        for (k1 = 0; k1 < this.object_ticks.length; k1++)
            this.object_ticks[k1].className = "";

        this.object_ticks[0].className = "active";
        this.object_ul.style.visibility = "visible";

        if (this.index <= 0) this.index = 1;

        this.timeout = 0,
        this.running = false;
        this.Track(sender, evt);
    },
    PreloadImage: function() {
        try {
            //console.log(new Date().getTime()+" preload:"+this.preload_index);
            if (this.preload_index>=1 && this.preload_index<=7 && this.object_frames[this.preload_index].src == "") {
                this.object_frames[this.preload_index].src = this.basename + (this.preload_index+1) + '.jpg'; 
                this.preload_index++;
                var callbackRef = this;
               setTimeout(function() { callbackRef.PreloadImage(); }, 50);
            }
        } catch (e) { /* alert("Run error: " + e); */ }
    },
    Run: function() {
        try {
            if (this.object1 == null) return;

            //console.log(new Date().getTime()+" run:"+this.index);
            this.image = this.object_frames[this.index];
            if (this.image.src == "")
                this.image.src = this.basename + (this.index+1) + '.jpg'; 

            this.timer_start = new Date().getTime();

            if (this.running) {
                if (this.timer != null) clearTimeout(this.timer);
                var callbackRef = this;
                this.timer = setTimeout(function() { callbackRef.RunImage(); }, this.timeout);
                this.timeout = this.run_delay;
            }
            else {
                this.timer = null;
                this.RunImage();
            }
        } catch (e) { /* alert("Run error: " + e); */ }
    },
    RunImage: function() {

        try {
            if (this.object1 == null) return;

            //console.log(new Date().getTime()+" runimage:"+this.image.complete);
            if (!this.image.complete) {
                if (!this.running) {
                    this.flasher = !this.flasher;
                    for (i = 0; i < 8; i++) {
                        if (this.flasher && this.index == i)
                            this.object_ticks[i].className = "active";
                        else
                            this.object_ticks[i].className = "";
                   }
                }

                if (this.timer != null) clearTimeout(this.timer);
                var callbackRef = this;
                this.timer = setTimeout(function() { callbackRef.RunImage(); }, 20);
                return;
            }
            if (this.image.width == 0) {
                this.Stop();
                return;
            }
            this.Activate();

            if (!this.running) return;

            var timer_delta = 0;
/*
            if (this.timer_start != 0) {
                timer_delta = new Date().getTime() - this.timer_start;
                if (timer_delta > this.run_delay)
                    timer_delta = 0;
                else {
                    timer_delta = this.run_delay - timer_delta;
                    //if (timer_delta < 150) timer_delta = 150;
                }
            }
            else
                timer_delta = this.run_delay;
*/
            if (++this.index > 7) this.index = 0;

            if (this.timer != null) clearTimeout(this.timer);
            var callbackRef = this;
            //console.log(new Date().getTime()+" runimage timer_delta:"+timer_delta);
            this.timer = setTimeout(function() { callbackRef.Run(); }, timer_delta);
        } catch (e) { /* alert("RunImage error: " + e); */ }
    },
    Activate: function() {
        try {
            for (k1 = 0; k1 < this.object_ticks.length; k1++) {
                if (this.index == k1) {
                    this.object_ticks[k1].className = "active";
                    this.object_frames[k1].style.visibility = "visible";
                }
                else {
                    this.object_ticks[k1].className = "";
                    this.object_frames[k1].style.visibility = "hidden";
                }
            }
        } catch (e) { /* alert("Activate error: " + e); */ }
    },
    Track: function(sender, evt) {
        try {
            if (this.object_aref != sender) return;

            var scroll = this.getPosition(evt);

            if (scroll.x < 0 || scroll.y < 0) return false;

            if (this.Hold(scroll.x, scroll.y)) {
                if (this.running) {
                    this.running = false;
                    if (this.timer != null) {
                        clearTimeout(this.timer);
                        this.timer = null;
                    }
                }
                var ix = this.HoldFrame(scroll.x, scroll.y);
                if (ix != this.index) {
                    this.index = ix;
                    this.Run();
                }
            }
            else {
                if (!this.running) {
                    this.running = true;
                    this.Run();
                }
            }
        } catch (e) { /* alert("Track error: " + e); */ }
    },
    Hold: function(x, y) {
        if (this.object_ul == null) return false;
        return x >= this.object_ul_rect.left && x <= (this.object_ul_rect.left + this.object_ul_rect.width)
            && y >= this.object_ul_rect.top && y <= (this.object_ul_rect.top + this.object_ul_rect.height)
    },
    HoldFrame: function(x, y) {
        var ix = Math.floor(((x - this.object_ul_rect.left-5)*8) / (this.object_ul_rect.width - 10) );
        if (ix > 7) ix = 7;
        else if (ix < 0) ix = 0;
        return ix;
    },
    getPosition: function(evt) {
        try {
            var scroll = SpkJs.Window.getScrollPos();

            if (evt == null) {
                scroll.x += event.clientX - this.object_x;
                scroll.y += event.clientY - this.object_y;
            }
            else {
                scroll.x += evt.clientX - this.object_x;
                scroll.y += evt.clientY - this.object_y;
            }

            return scroll;
        } catch (e) { /* alert("getPosision error: " + e); */ }
    },
    getbounds: function(obj) {
        try {

            return { left: obj.offsetLeft, top: obj.offsetTop, width: obj.clientWidth, height: obj.clientHeight };

            //var rect = obj.getBoundingClientRect();
            //return {left: rect.left, top: rect.top, width: obj.clientWidth, height: obj.clientHeight };
        } catch (e) { /* alert("getBounds error: " + e); */ }
    },
    Click: function(sender, evt) {
        try {
            var scroll = this.getPosition(evt);

            if (scroll.x < 0 || scroll.y < 0) return;

            var url = sender.href;

            if (this.Hold(scroll.x, scroll.y))
            {
                var percent = (this.HoldFrame(scroll.x, scroll.y) + 1) * 10;
                if (percent == 100) percent = 95;
                url += "?seekPercent=" + percent;
            }

            window.location.href = url;
        } catch (e) { /* alert("Click error: " + e); */ }
        return SpkJs.Event.CancelClick(evt);
    },
    Stop: function() {
        if (this.object1 == null) return;

        if (this.timer != null) {
            clearTimeout(this.timer);
            this.timer = null;
        }

        this.object_aref = null;

        this.object_ul.style.visibility = "hidden";
        this.index = 0;
        this.running = false;
        this.Activate();
    }
};