HomeFlipper = Class.create();
HomeFlipper.prototype = {

    initialize: function(base_url)
    {
        this.baseUrl=base_url;
        this.prevBtn=$('previous_link');
        this.nextBtn=$('next_link');

        this.prevBtn.onclick=this.getPrev.bind(this);
        this.nextBtn.onclick=this.getNext.bind(this);

        this.prevBtn.onmouseover=this.offset.bind(this);
        this.nextBtn.onmouseover=this.offset.bind(this);
        this.prevBtn.onmouseout=this.reset.bind(this);
        this.nextBtn.onmouseout=this.reset.bind(this);

        this.pars="";
        this.url=base_url+"/";       
        this.el=$('screenshot_dynamic');

        this.autoFlip = this.autoLoad.delay(20);
    },

    reset: function(e)
    {
        if(!e)  var e = window.event;
        el=Event.element(e);
        if(el.id == 'next_link')
            el.style.backgroundPosition="left top";
        else
            el.style.backgroundPosition="-34px top";
    },
    offset: function(e)
    {
        if(!e)  var e = window.event;
        el=Event.element(e);
        if(el.id == 'next_link')
            el.style.backgroundPosition="right top";
        else
            el.style.backgroundPosition="left top";
    },

    autoLoad: function()
    {
        // taking out Highlight effect to slide background position
        // new Effect.Highlight(homeFlipper.nextBtn,{ startcolor: '#AAC949'});
        homeFlipper.nextBtn.style.backgroundPosition="right top";
        setTimeout("homeFlipper.getNext()" , 1000);
        homeFlipper.autoFlip = homeFlipper.autoLoad.delay(20);
    },

    clear: function()
    {
        window.clearTimeout(homeFlipper.autoFlip);
    },

    getNext: function ()
    { 
        this.clear();
        this.pars="which=next";
        this.prevBtn.hide();
        this.nextBtn.hide();
        Effect.Puff(this.el.id,{duration: 1.3});
        setTimeout("homeFlipper.loadContent()", 1300);
        // homeFlipper.nextBtn.style.backgroundPosition="left top";
    },

    getPrev: function ()
    {
        this.clear();
        this.pars="which=prev";
        this.prevBtn.hide();
        this.nextBtn.hide();
        Effect.Puff(this.el.id,{duration: 1.3});
        setTimeout("homeFlipper.loadContent()", 1300);
    },

    loadContent: function()
    {
        this.clear();
        var myAjax = new Ajax.Updater(
            this.el.id,
            this.baseUrl + 'home/get_screenshot', 
            {
                method: 'post', 
                parameters: this.pars, 
                onComplete: this.screenshotResponse.bind(this)
            });

    },

    screenshotResponse: function(res)
    {
        this.prevBtn.style.backgroundPosition="-34px top";
        this.nextBtn.style.backgroundPosition="left top";
        Effect.Appear(this.el.id, {duration:1.5});
        this.prevBtn.show();
        this.nextBtn.show();
        // reset the auto flipper
        this.autoFlip = this.autoLoad.delay(20);
    }

}
