﻿

/********************************************************
 NOTE : THIS REQUIRES IMAGE-PRELOADER TO BE LOADED FIRST
********************************************************/
var intro_rotator = 
{
    //properties
    images : [
                {"path": "common/img/home_rotator/home_rotate_men.jpg", "key": "MSCNS"}
               ,{"path": "common/img/home_rotator/home_rotate_women.jpg ", "key": "FICNS"}
               ,{"path": "common/img/home_rotator/home_rotate_original.jpg", "key": "FICLU"}
             ],
    current_image : 0,
    wait_time : 6,
    timer : 0,
    image_id : "intro_image",
    interval_id : "",
    tied_to_selector : true,
    
    
    //METHODS
    Start_Timer: function()
    {
        if(this.interval_id=="")
        {
            this.timer = this.wait_time;
            this.interval_id = window.setInterval("intro_rotator.Timer_Interval();", 1000);
        }
    },
    
    Stop_Timer: function()
    {
        window.clearInterval(this.interval_id);
        this.interval_id="";
    },
    
    Timer_Interval: function()
    {       
        if(this.timer>0)
            this.timer--;
        else
            this.Show_Next_Image();
    },
    
    
    //methods
    Show_Next_Image : function()
    {
        this.Stop_Timer();
        
        if(this.current_image == this.images.length-1)
            this.current_image = 0;
        else
            this.current_image++;

        //slide image down
        var id = this.image_id;
        var newimage = this.images[this.current_image].path;
        $("#"+id).animate({
            opacity: 0
            //,top: 400
            }
            ,200
            ,function() 
            {
                // Animation complete.
                $("#"+id).attr("src", newimage);
            }
        );
        
        
        
        if(this.tied_to_selector)
            product_selector.Update_Product_Selectors(this.images[this.current_image].key);
        
        //slide image up
        $("#"+this.image_id).animate({
            opacity: 1
            //,top: '+150'
            }
            ,200
            ,function() 
            {
                // Animation complete.
            }
        );
        
        //restart timer
        this.Start_Timer();
    },
    
    Preload : function()
    {
        //preload images
        var loader = new image_preloader();
        loader.Init
        (
            function(){}, //run after each image loads
            function()
            {
                product_selector.Update_Product_Selectors(intro_rotator.images[0].key);
                intro_rotator.Start_Timer();
            } //run when done loading all images
        );
        
        var imgs = [];
        for(var x in this.images)
            imgs[x] = this.images[x].path;
        
        loader.Preload_Images(imgs);
    }
}
