﻿// JScript File
// this script requires jQuery v1.2.6 

function setCookie(c_name,value,expiredays)
{
    var exdate=new Date()
    exdate.setDate(exdate.getDate()+expiredays)

    document.cookie=c_name+ "=" +escape(value)+
        ((expiredays==null) ? "" : ";expires="+exdate.toGMTString() + ";path=/")
}

function getCookie(c_name)
{
    if (document.cookie.length>0)
    {
      c_start=document.cookie.indexOf(c_name + "=");
      if (c_start!=-1)
      { 
        c_start=c_start + c_name.length+1 
        c_end=document.cookie.indexOf(";",c_start)
        if (c_end==-1) c_end=document.cookie.length
            return unescape(document.cookie.substring(c_start,c_end))
      } 
    }
    return ""
}



(function($){
    $.fn.webshots = function(options){
        $.fn.webshots.defaults = {    
            easing: "linear",
            mainDiv : "#webshots",
            shotDivs : ".webshot",
            showSpeed : 1000,
            showDelay : 200,
            hideSpeed : 800,
            fadeDelay : 400,
            showEvent : "mouseenter",
            hideEvent : "mouseleave",
            startDelay : 0,
            animTimer : null,
            onShow : null,
            onHide : null,
            startAnim : null,
            showWebShots: null
        };
        
        var opts = $.extend({}, $.fn.webshots.defaults, options);
        var $ws = this;
        var $wss = this.find(opts.shotDivs || ".webshot");
        var $length = $wss.length;
        var isover = false;
        
        var currWebShot = null;
        var prevRandom = 0;
        var random = 0;
        var animTimer = opts.animTimer || null;
        
        function selectNextRandom()
        {
            var next = Math.floor($length * Math.random());
            while(next == random || next == prevRandom)
                next = Math.floor($length * Math.random());
            
            prevRandom = random;
            random = next;
            return next;
        }
        
        return this.each(function() 
        {            
            
            var this_onShow = opts.onShow = opts.onShow || onShow;
            var this_onHide = opts.onHide = opts.onHide || onHide;
            var this_showWebShots = opts.showWebshots || showWebShots;
            //var this_startAnim = opts.startAnim || startAnim;
            
            // set up the individual webshots
            initWebShots(opts.shotDivs || ".webshot");
            
            // show them
            showWebShots($wss, opts);            
            
            function showWebShots(wss, options)
            {
                // start from the first one
                var i = 0;                
                
                // showWebshot will recursively call itself until all are shown
                window.setTimeout(function(){
                    showWebshot(wss, i, options);
                }, options.startDelay || 0);
                
            }
            
            function showWebshot(wss, index, options)
            {
                var cookie = getCookie("beyond.webshots");
                
                if(cookie == null || cookie == "")
                {
                    var _ws = $(wss[index]);
                    // find the anchor and make it opaque
                    $(_ws).find("a").animate({"opacity":"1"}, options.hideSpeed || 800);
                    index++;
                    // set up the timeout for the next one
                    if(index < $wss.length)
                    {
                        window.setTimeout(function() {
                            showWebshot(wss, index, options);
                        }, 500);
                        
                    }
                    else // we've shown them all - start animating if not mousing over one
                    {
                        if(!isover)
                        {
                            setCookie("beyond.webshots","done");
                            startAnim(wss, options);
                            
                        }
                    }
                } 
                else 
                {
                    $(wss).find("a").animate({"opacity":"1"}, options.hideSpeed || 800);
                    startAnim(wss, options);
                }
            }
            
            function startAnim(wss, options)
            {
                // set up regular interval to show
                var interval = options.showDelay + options.showSpeed + options.fadeDelay + options.hideSpeed;
                animTimer = window.setInterval(function()
                {
                    // select a random casestudy
                    random = selectNextRandom(length);
                    elm = $(wss[random]).find("img");                    
                    // fade in image                    
                        this_onShow(elm, options.showSpeed);                    
                        //set up the fade out 
                        window.setTimeout(function()
                        {                        
                            this_onHide(elm, options.hideSpeed);
                        }, options.hideSpeed + options.fadeDelay);                                    
                }, interval);
            
            }
            
            function initWebShots(selector)
            {
                $(selector).each(function()
                {
                    // make anchors transparent initially
                    var this_a = $(this).find("a");
                    this_a.css("opacity","0").css("display","block").css("visibility", "visible");
                    // make images transparent initially
                    var this_img = this_a.find("img");
                    this_img.css("opacity","0").css("display","block").css("visibility", "visible");                    
                    
                    // bind show and hide events
                    $(this).bind(opts.showEvent, function(e)
                    {
                        isover = true;
                        // kill the animation
                        window.clearInterval(animTimer);
                        //fade image in                        
                        
                        this_onShow(this_img, opts);
                        
                    }).bind(opts.hideEvent, function(e)
                    {
                        isover = false;
                        // fade image out
                        this_onHide(this_img, opts);
                        //restart the animation
                        startAnim($wss, opts);
                    });
                
                });
            
            }
            
            function onShow(elm, options)
            {
                // save our current opacity
                var op = parseFloat(elm.css("opacity")) || 0;
                // select a speed - dependant upon current opacity
                var speed = (1 - op) * (options.speed || opts.showSpeed);
                
                // select an animation
                var anim = options.animation || {"opacity" : "1"};                
                // perform animation
                $(elm).stop().animate(anim, speed);
            }
            
            function onHide(elm, options)
            {
                var op = parseFloat(elm.css("opacity")) || 0;
                // select a speed
                var speed = op * (options.speed || opts.hideSpeed);
                
                // select an animation
                var anim = options.animation || {"opacity" : "0"};
                
                // perform animation
                $(elm).stop().animate(anim, speed);            
            }
        });
        
    }
    
})(jQuery);


$(document).ready(function()
{
    $('div#webshots').webshots( 
        { 
            startDelay: 0, 
            showSpeed: 500,
            hideSpeed: 500,
            showDelay: 200,
            fadeDelay: 800
            
        });

});