﻿/*
Extending Math object with some Motion Tweens
See http://www.robertpenner.com/easing/penner_chapter7_tweening.pdf around p14 to understand what they are doing.
(c) 2003 Robert Penner (http://www.robertpenner.com/easing/), Open Source BSD License.
*/
Math.linearTween = function(t, b, c, d) {
    return c * t / d + b;
};
Math.easeInOutSine = function(t, b, c, d) {
    return c / 2 * (1 - Math.cos(Math.PI * t / d)) + b;
};
Math.easeInOutExpo = function(t, b, c, d) {
    if ((t /= d / 2) < 1)
        return c / 2 * Math.pow(2, 10 * (t - 1)) + b;
    return c / 2 * (-Math.pow(2, -10 * --t) + 2) + b;
};

/*
GGP.Effects 
Custom GGP Transition and Animations helpers.
*/
GGP.Effects = {};
GGP.Effects.Transition = function(def) {

    if (!def.duration) def.duration = 500;
    if (!def.fps) def.fps = 50;
    if (!def.uom) def.uom = 'px';
    if (!def.tween) def.tween = Math.easeInOutSine;

    def.run = function(valuefrom, valueto, when, onstart, onfinish) {

        if (this.timer) return;

        this.when = when ? when : 0;
        this.valuefrom = valuefrom;
        this.valueto = valueto;
        this.onfinish = onfinish;
        this.starttime = new Date().getTime() + this.when;
        this.finishtime = this.starttime + this.duration;
        this.timer = setInterval(this.bindstep(), Math.round(1000 / this.fps));
    };

    def.step = function() {

        var timenow = new Date().getTime();
        var valuenow = null;

        if(timenow < this.starttime){
            valuenow = this.valuefrom;
        } else if (timenow < this.finishtime) {
            var timelapsed = timenow - this.starttime;
            valuenow = this.tween(timelapsed, this.valuefrom, this.valueto - this.valuefrom, this.duration);
        } else {
            clearInterval(this.timer);
            this.timer = null;
            valuenow = this.valueto;
        }

        var style = this.element.style;

        if (this.type == 'opacity') {
            if (valuenow == 0) {
                if (style.visibility != 'hidden') style.visibility = 'hidden';
            } else {
                if (style.visibility != 'visible') style.visibility = 'visible';
            }

            if (window.ActiveXObject)
                style.filter = 'alpha(opacity=' + valuenow * 100 + ')';

            style.opacity = valuenow;

        } else {

            style[this.type] = valuenow + this.uom;

        }

        if (this.timer == null && this.onfinish) {
            this.onfinish();
            this.onfinish = null;
        }

    };

    def.bindstep = function() {
        var rEf = this;
        return function() {
            return rEf.step();
        };
    };

    return def;
};

GGP.Effects.Animation = function(def) {

    def.transitions = new Array();

    for (var i = 0; i < def.length; i++) {

        def.transitions[i] = new GGP.Effects.Transition(def[i]);

    }

    def.run = function() {
        for (var i = 0; i < this.transitions.length; i++) {

            this.transitions[i].run.apply(this.transitions[i], arguments[i]);

        }
    };

    return def;
};

GGP.Effects.SlideShow = function(sscid, options) {

    if(!options.delay) options.delay = 5000;
    if(!options.ttoffset) options.ttoffset = [0,0];
    
    var rtn = {
        init: function(sscid, index, options) {

            this.container = document.getElementById(sscid);
            this.index = index;
            this.navcontainer = document.getElementById(sscid + 'N');
            this.imageTransitions = true;
            this.options = options;
                        
            this.elements = new Array();

            for (i = 0; i < this.container.childNodes.length; i++) {

                var child = this.container.childNodes[i];

                if (child.tagName == 'DIV') {

                    if(this.container.style.width==''){
                       this.container.style.width = child.offsetWidth+'px';
                    }
                    if(this.container.style.height==''){
                       this.container.style.height = child.offsetHeight+'px';
                    }

                    child.style.position = 'absolute';
                    child.style.top = '0px';
                    child.style.left = '0px';
                    child.style.width = this.container.style.width;
                    child.style.height = this.container.style.height;
                    child.style.visibility = 'hidden';
                    child.delay = parseInt(child.getAttribute('delay'));
                    if (isNaN(child.delay)||child.delay<500) child.delay = this.options.delay;
                    
                    child.swf = child.getAttribute('SWF');
                    
                    if(child.swf){
                        child.swf_elementid = child.id+'_SWF';
                        child.swf_phclass = document.getElementById(child.swf_elementid).className;
                        child.swf_width = this.container.offsetWidth;
                        child.swf_height = this.container.offsetHeight;
                        child.swf_version = '9.0.0';
                        child.swf_installer =  '/Scripts/SWFObject/expressInstall.swf';
                        child.swf_vars = {autoplay:1};
                        child.swf_params = {wmode:'transparent', menu:'false'};
                        child.swf_atts = {id:child.swf_elementid,name:child.swf_elementid};  
                        child.embedSWF = function(){
                                            swfobject.embedSWF(this.swf + '?no_cache=true',
                                                               this.swf_elementid,
                                                               this.swf_width,
                                                               this.swf_height,
                                                               this.swf_version,
                                                               this.swf_installer,
                                                               this.swf_vars,
                                                               this.swf_params,
                                                               this.swf_atts);    
                                         };
                        child.removeSWF = function(){
                                             swfobject.removeSWF(this.swf_elementid);
                                             var placeholder = document.createElement('div');
                                             placeholder.id = this.swf_elementid;
                                             placeholder.className = this.swf_phclass;
                                             this.appendChild(placeholder);
                                        };                                         
                    }
                            
                    if (this.imageTransitions) {
                        child.transition = new GGP.Effects.Transition(
                                                        { element: child, type: 'opacity', duration: 500, tween: Math.linearTween }
                                                    );
                    }
                    this.elements.push(child);
                }
            }
            this.initNav();
            this.showNextSlide();
            this.container.index = this.index;
            this.container.onmouseout = function(){GGP.Effects.SlideShows[this.index].continueShow();};
        }
        , initNav: function() {
               
            if(!this.navcontainer) return;
            
            var n = '<table onmouseout="GGP.Effects.SlideShows[' + this.index + '].hideToolTip();"><tr>';
            for (i = 0; i < this.elements.length; i++) {
                    
                    var navText = this.elements[i].getAttribute('navText');
                    if(!navText)navText=(i+1);
                    
                    n+='<td class="ssNavLink" ';
                    n+='onclick="GGP.Effects.SlideShows['+this.index+'].showNextSlide('+i+');" ';
                    n+='onmouseover="GGP.Effects.SlideShows['+this.index+'].showToolTip('+i+');" ';
                    n+='>'+navText+'</td>';
            }
            n+='</tr></table>';
            
            this.navcontainer.innerHTML = n;
            this.navelements = _ggp_get_elements(this.navcontainer,['TD']);
                 
        }
        , showNextSlide: function(i) {

            var isFirstTime = this.iCur==undefined;
            var isNavEvent = i!=undefined;
            var isTimedEvent = !isFirstTime && !isNavEvent;
            
            if (isFirstTime) {
                
                this.iCur = 0;
                this.iPrv = this.elements.length - 1;
                
            } else if(isNavEvent){
                
                this.pauseShow();
                if(this.iCur==i)return;
                this.iPrv = this.iCur;
                this.iCur = i;                
            
            } else if(isTimedEvent){
                
                this.iPrv = this.iCur;
                this.iCur = this.iCur + 1;
                if (this.iCur >= this.elements.length) {
                    this.iCur = 0;
                }
            }
            
            var elCur = this.elements[this.iCur];
            var elPrv = this.elements[this.iPrv];
            
            if(window[this.container.parentNode.id+'_AnimateSlide']!=undefined){
                window[this.container.parentNode.id+'_AnimateSlide'](this,elCur);   
            }

                        
            if(elCur.swf) elCur.embedSWF();
            
            if (this.imageTransitions && !isFirstTime) {
                
                if(elPrv.swf){
                    elPrv.style.visibility = 'hidden';
                }else{
                    elPrv.transition.run(1, 0, 0);
                }
                
                if(elCur.swf){
                    elCur.style.visibility = 'visible';
                }else{
                    elCur.transition.run(0, 1, 0);
                }
                
            } else {
                elPrv.style.visibility = 'hidden';
                elCur.style.visibility = 'visible';
            }
            
            if(elPrv.swf)elPrv.removeSWF();
            
            if(this.navelements){
                this.navelements[this.iCur].style.color = elCur.style.backgroundColor;
                this.navelements[this.iCur].style.borderColor = elCur.style.backgroundColor;
                this.navelements[this.iPrv].style.color = '';
                this.navelements[this.iPrv].style.borderColor = '';
            }
            
            
            
            if (!isNavEvent) this.continueShow();
        }
        , continueShow: function() {
      
            this.pauseShow();
            if(this.elements.length>1){
                this.timeout = setTimeout("GGP.Effects.SlideShows[" + this.index + "].showNextSlide()", this.elements[this.iCur].delay);
            }
        }
        , pauseShow: function() {
      
            if(this.timeout){
                clearTimeout(this.timeout);
                this.timeout = undefined;                    
            }                 
        }
        , showToolTip: function(i) {
            
            this.hideToolTip();

            var ne = this.navelements[i];
            if(ne.tooltip==undefined){
                ne.tooltip = _ggp_get_single_element(this.elements[i],['DIV'],function(o){return o.className=='ssNavLinkTip'});
                if(ne.tooltip){
                    this.elements[i].removeChild(ne.tooltip);
                    this.navcontainer.appendChild(ne.tooltip);   
                }
            }

            if(ne.tooltip){
                this.curtip = i;
                var nepos = _ggp_get_element_info(ne);
                if(_ggp_get_style_value(this.navcontainer,'position')=='relative'){
                    var ncpos = _ggp_get_element_info(this.navcontainer);
                    nepos.top -= ncpos.top;
                    nepos.left -= ncpos.left;
                    nepos.bottom -= ncpos.top;
                    nepos.right -= ncpos.left;
                }
                ne.tooltip.style.top = (nepos.bottom+this.options.ttoffset[0]) + 'px';
                ne.tooltip.style.left = (nepos.right+this.options.ttoffset[1]) + 'px';                 
                ne.tooltip.style.display = 'block';
            }
        }
        , hideToolTip: function(i) {
            
            if(this.curtip!=undefined)
                this.navelements[this.curtip].tooltip.style.display = 'none';
            this.curtip = undefined;            
        }        
    };

    if (GGP.Effects.SlideShows == undefined)
        GGP.Effects.SlideShows = new Array();

    var index = GGP.Effects.SlideShows.length;

    rtn.init(sscid,index,options);

    GGP.Effects.SlideShows[index] = rtn;

    return rtn;
}; 
  
