// Viewer Class
var Viewer = Class.create();
Viewer.prototype = {
    _locale: null,
    _urlRequest: '/xml_json.php?file=galeria.xml',
    _gallery: null,
    _show: null,
    _object: null,
    
    _actualShow: null,

    _startObject: null,

    _arrayShows: null,
    
    _showAll: null,

    _viewerContent: 'viewer_content',

    _previous: 'anterior',
    _next: 'siguiente',

    _close: 'viewer_close_img',

    _footer: 'viewer_footer',

    initialize: function(locale, show, object, showAll) {
//        $$('html')[0].style.overflowY='hidden';
//        if (document.viewport.getHeight()>=540){
//          $(this._viewerOutterContainer).setStyle({marginTop:((document.viewport.getHeight()-540)/2-10)+'px'});
//        }
        this._locale = locale;
        this._actualShow = show;
        this._startObject = object;
        this._arrayShows = new Array();
        this._showAll = showAll;
        this._previous = $(this._previous);
        this._next = $(this._next);
        this._close = $(this._close);
        this._footer = $(this._footer);
        this._close.setStyle({cursor: 'pointer'});
        [this._previous, this._next].each(function(el) {
          el.setStyle({cursor: 'pointer'});
          var options = {
            enabled: false,
            //mouseOverStyle: 'algun estilo',
            //mouseOutStyle: 'algun estilo',
            setEnabled: function(value) {
              //means that status has changed
              if (this.enabled!=value) {
                this.enabled = value;
                if (this.enabled) {
                  //Effect.Appear(this, { duration: 1.0 });
                	this.show();
                }else{
                  //Effect.Fade(this, { duration: 1.0 });
                  this.hide();
                }
              }
            },
            handleButtonMouseOver: function() {
              //if (this.enabled) new Effect.Morph(this, {style: this.mouseOverStyle,duration: 0.1})
            },
            handleButtonMouseOut: function() {
              //if (this.enabled) new Effect.Morph(this, {style: this.mouseOutStyle,duration: 0.1})
            }
          };
          Object.extend(el, options);
          el.observe('mouseover', el.handleButtonMouseOver);
          el.observe('mouseout', el.handleButtonMouseOut);
        }.bind(this));

        this._previous.observe('click', this.handlePreviousClick.bind(this));
        this._next.observe('click', this.handleNextClick.bind(this));
        this._close.observe('click', this.close.bind(this));

        this.loadJSON();
    },
    close: function() {
      litbox.remove();
    },
    clearViewerContent: function() {
      $(this._viewerContent).innerHTML='';
    },
    loadJSON: function() {
      new Ajax.Request( this._urlRequest, {
          method: 'get',
          onSuccess: this.processContent.bindAsEventListener(this)
        }
      );
    },
    processContent: function(response) {
      var json = response.responseText.evalJSON();
      this._gallery = json.gallery;
      this._gallery.show.each(function(c, i) {
      	this._arrayShows[i] = c['@attributes'].slug;
      }.bind(this));
      this.loadShow(this._actualShow);
      this.loadObject(this._startObject);
    },
    loadShow: function(slug) {
      var index = 0;
      if (typeof this._gallery.show[0] !== 'undefined') {
        this._gallery.show.each(function(c, i) {
        	if(c['@attributes'].slug==slug) index = i
        }.bind(this));
      }
      this.setShow(index);
    },
    loadObject: function(slug) {
      var index = 0;
      if (typeof this._show.obj[0] !== 'undefined') {
        this._show.obj.each(function(o, i) {
        	if(o['@attributes'].slug==slug) index = i
        }.bind(this));
      }
      this.setObject(index);
    },
    setShow: function(index) {
      if (typeof this._gallery.show[index] === 'undefined') {
        this._show = this._gallery.show;
        Object.extend(this._show, this._show['@attributes']);
        Object.extend(this._show, {index: index, isFirst: true, isLast:true});
      }else{
        this._show = this._gallery.show[index];
        Object.extend(this._show, this._show['@attributes']);
        Object.extend(this._show, {index: index, isFirst: (index==0), isLast:(index==(this._gallery.show.length-1))});
      }
      this._actualShow = this._show['@attributes'].slug;
    },
    setObject: function(index) {
      if(!this._show.obj[index]){
    	  this._object = this._show.obj;
      }else{
    	  this._object = this._show.obj[index];
      }
      Object.extend(this._object, this._object['@attributes']);
      Object.extend(this._object, {index: index, isFirst: (index==0), isLast:(typeof this._show.obj.length === 'undefined' || index==(this._show.obj.length-1))});

      this._footer.innerHTML='';

      if (this._locale=='es'){
    	  this._footer.innerHTML = this._show.sinopsis_es;
      }else{
    	  this._footer.innerHTML = this._show.sinopsis_pr;
      }

      $("show-name").innerHTML = this._show.name;

      this.showImagen();
    },
    showImagen: function() {
      this.clearViewerContent();

      var el = $(this._viewerContent);
      var img = document.createElement('img');
      img.setAttribute('id', this._viewerContent + "_img");
      img.setAttribute('src', this._show.src_path + this._object.src);
      img.setAttribute('width', 680);
      img.setAttribute('height', 460);
      el.appendChild(img);

      //this._previous.setEnabled(!this._object.isFirst);
      //this._next.setEnabled(!this._object.isLast);

      if(!this._object.isLast){
    	  this._next.setEnabled(true);
      }else{
    	  if(this._showAll){
        	  if (typeof this._gallery.show[this._arrayShows.indexOf(this._actualShow)+1] === 'undefined') {
        		  this._next.setEnabled(false);
        	  }else{
        		  this._next.setEnabled(true);
        	  }
    	  }else{
    		  this._next.setEnabled(false);
    	  }
      }
    	  
      if(!this._object.isFirst){
    	  this._previous.setEnabled(true);
      }else{
    	  if(this._showAll){
        	  if (typeof this._gallery.show[this._arrayShows.indexOf(this._actualShow)-1] === 'undefined') {
        		  this._previous.setEnabled(false);
        	  }else{
        		  this._previous.setEnabled(true);
        	  }
    	  }else{
    		  this._previous.setEnabled(false);
    	  }
      }
    },
    handlePreviousClick: function(event) {
      var el = Event.element(event);
      if (el.enabled){
    	  if (typeof this._show.obj[this._object.index-1] === 'undefined') {
    		  this.loadShow(this._arrayShows[this._arrayShows.indexOf(this._actualShow)-1]);
    		  this.loadObject(this._show.obj.length);
    	  }else{
    		  this.setObject(this._object.index-1);
    	  }
      }
    },
    handleNextClick: function(event) {
      var el = Event.element(event);
      if (el.enabled){
    	  if (typeof this._show.obj[this._object.index+1] === 'undefined') {
    		  this.loadShow(this._arrayShows[this._arrayShows.indexOf(this._actualShow)+1]);
    		  this.loadObject(1);
    	  }else{
    		  this.setObject(this._object.index+1);
    	  }
      }
    }
}

