/*
 * ELSlideshowCore
 *
 * Copyright 2010 Estee Lauder - www.esteelauder.com
 * Lovingly crafted By Nick Warner - www.nwarner.com
*/


var ELSlideshow = Class.create({ 
  "loop" :             true,
  "autoplay" :         true,
  "width" :            970,
  "height" :           440,  
  "slides" :           [],
  "overlays" :         [],
  "underlays" :        [],
  "pager" :            undefined
});


var ELLayer = Class.create({ 
  "initialize" :  function(i){
    this.id = i;
  },
  "element" :     undefined,
  "assets" :      undefined,
  "type" :        "layer",
  "name" :        undefined,
  "url" :         undefined,
  "x"  :          0,
  "y" :           0,
  "z" :           0,
  "width" :       undefined,
  "height":       undefined,
  "onload":       undefined,
  "onunload" :    undefined,
  "onmouseover":  undefined,
  "onmouseout" :  undefined,
  "onclick" :     undefined
});


var ELPager = Class.create({ 
  "start_x" :          0,
  "start_y" :          0,
  "width" :            970,
  "height" :           440,  
  "src" :              undefined,
  "src_current" :      undefined,
  "onload_action" :     undefined,
  "onmouseover_action" :  undefined,
  "onmouseout_action" : undefined
});


var ELSlide = Class.create(ELLayer, {
    "type" :              "slide",
    "metrics_id" :        undefined,
    "layers" :            [],   
    "background_color" :  "transparent",
    "interval" :           7,  
    "interval_in_milliseconds" : function(){ return this.interval * 1000; }
});



var ELImage = Class.create(ELLayer, {
  "type" :  "image",
  "src" :   undefined,
  "image" : new Image()
});



var ELText = Class.create(ELLayer, {
  "type" :          "text",
  "text" :          undefined,
  "font" :          "Helvetica",
  "fontsize" :      15,
  "color" :         "#fff",
  "letterspacing" : 15
});



var ELAnimation = Class.create({
  initialize :  function(player){
    this.player =   player;
    this.play =     function(layer, animations){ 
      var self = this;
      var max_delay = 0;
      
      // Clear overlays
      if ($(layer.id).select('.animation_fade_to_color').length > 0) {
        $(layer.id).select('.animation_fade_to_color').each(function(f){ f.remove(); });
      }
      
      if (Object.isArray(animations)) {      
        animations.each(function(animation_string, index){
          animation_string = animation_string.split(',');
          var animation = animation_string.shift();
          max_delay = max_delay + self[animation](layer, animation_string);
        });
      } else {
        animations = animations.split(',');
        var animation = animations.shift();
        max_delay = this[animation](layer, animations);
      }
      return max_delay;// Run animation and return the time in seconds required to play itself
    };
  }
});



var ELAction = Class.create({
  initialize :  function(layer_to_act_on, initial_actions, player){    
    if (initial_actions === null) {
      initial_actions = undefined;
    }
    this.player =     player;
    this.layer =      layer_to_act_on;
    this.actions =    initial_actions;
    this.run =        function() { 
    
      var max_delay = 0;
      if ((this.actions !== null) && (this.actions !== undefined) && (this.actions !== "")) { 
        var action_list = this.actions.evalJSON();
        for (var action in action_list) {
          if (action_list.hasOwnProperty(action)) {
            if (this[action]) { 
              var delay = this[action](action_list[action]);
              if (delay > max_delay) {
                max_delay = delay;
              }
            }
          }
        }
      }  
      return max_delay; //return the time in seconds required to complete the action
    };
  },

  'url': function(value){ 
    parent.location = value;
  },
  'animation': function(animation){    
    return new ELAnimation(this.player).play(this.layer, animation);
  },
  'script': function(value){
    return eval(value);
  }
});



