var Popups = new Array();

var Popup = $.Class.create({
   controls_height:27,
   templates: {
      flash: '<object width="#{width}" height="#{height}" data="#{source}" type="application/x-shockwave-flash"><param name="movie" value="#{source}" /><param name="wmode" value="opaque" /></object>'
      //flash_ie6: new Template('<embed width="#{width}" height="#{height}" src="#{source}" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" wmode="opaque" />'),

      //image: new Template('<img src="#{source}" width="#{width}" height="#{height}" alt="#{source}" />')
   },

   initialize: function(params) {
      this.params = params;
      if (Popups[this.params.id]) Popups[this.params.id].remove();
      this.isIE6 = /msie|MSIE 6/.test(navigator.userAgent);
      this.index=0;

      this.wrapper = $.create('div', {id : this.params.id, 'class':"popup-wrapper"});
      $(this.wrapper).css('display', 'none');
      this.content = $.create('div', {'class':"popup-content"});
      this.controls={};
      this.controls.wrapper =  $.create('div', {'class':"popup-controls"});
      this.controls.close =  $.create('div', {'class':"popup-close"});
      this.controls.label =  $.create('div', {'class':"popup-label"});
      this.controls.prev_label =  $.create('div', {'class':"popup-prev-label"});
      $(this.controls.prev_label).html($L.HPMsg1331);
      this.controls.next_label =  $.create('div', {'class':"popup-next-label"});
      $(this.controls.next_label).html($L.HPMsg1332);
      this.controls.prev =  $.create('div', {'class':"popup-prev"});
      this.controls.next =  $.create('div', {'class':"popup-next"});
      // 1px correct for ie6
      if (this.isIE6) {
         $(this.controls.prev).css('bottom', '-1px');
         $(this.controls.next).css('bottom', '-1px');
         $(this.controls.label).css('bottom', '-1px');
      }

      //stylize
      $(this.wrapper).append($.create('div', {'class':'popup-top-left'}));
      $(this.wrapper).append($.create('div', {'class':'popup-top-right'}));
      $(this.wrapper).append($.create('div', {'class':'popup-bottom-left'}));
      $(this.wrapper).append($.create('div', {'class':'popup-bottom-right'}));
      $(this.wrapper).append($.create('div', {'class':'popup-top'}));
      $(this.wrapper).append($.create('div', {'class':'popup-bottom'}));
      $(this.wrapper).append($.create('div', {'class':'popup-left'}));
      $(this.wrapper).append($.create('div', {'class':'popup-right'}));

      $(this.controls.wrapper).append(this.controls.label);
      $(this.controls.wrapper).append(this.controls.prev_label);
      $(this.controls.wrapper).append(this.controls.next_label);
      $(this.controls.wrapper).append(this.controls.next);
      $(this.controls.wrapper).append(this.controls.prev);

      $(this.wrapper).append(this.controls.close);
      $(this.wrapper).append(this.content);
      $(this.wrapper).append(this.controls.wrapper);

      $('body').append(this.wrapper);

      $(this.controls.close).bind('click', this.close);
      $(this.controls.next).bind('click', this.next);
      $(this.controls.prev).bind('click', this.prev);

      $(this.wrapper).css('top', '0px');
      $(this.wrapper).css('left', '0px');

      Popups[this.params.id]=this;
   },

   _setFilter: function() {
      if (!this.filter) {
         this.filter = $.create('div');
         $(this.filter).css('position', 'absolute');
         $(this.filter).css('top', this.scroll_top+'px');
         $(this.filter).css('left', '0px');
         $(this.filter).css('width', this.dimensions.width + 'px');
         $(this.filter).css('height', this.dimensions.height + 'px');
         $(this.filter).css('backgroundColor', '#000000');
         $(this.filter).css('MozOpacity', 0);
         $(this.filter).css('filter', 'alpha(opacity=0)');
         $(this.filter).css('opacity', 0);
         $(this.filter).css('zIndex', 999);
         $(this.wrapper).prepend(this.filter);

         $('html').css('overflow', 'hidden');
         $('html').scrollTop = this.scroll_top;
         $(this.filter).fadeTo(0.65);
      }
      else {
         $(this.filter).css('width', this.dimensions.width + 'px');
         $(this.filter).css('height', this.dimensions.height + 'px');
      }
   },

   _unsetFilter: function() {
      $('html').css('overflowY', 'auto');
      $('html').css('overflowX', 'hidden');
      $('html').scrollTop = this.scroll_top;
      $(this.filter).hide(0, function() {
         this.filter.remove();
         this.filter=null;
      });
   },

/*
   _getTemplate: function(template, params) {
      var postfix='';
      //if (this.isIE6) postfix='_ie6';
      return this.templates[template+postfix].evaluate(params);
   },
*/

   setData: function(data) {
      this.data = data;
   },

   setScope: function(scope) {
      this.scope=scope;
   },

   center: function() {
      this.dimensions= {
         height: $(window).height(),
         width: $(window).width()
      };
      if ($(this.wrapper).css('top') == '0px' && $(this.wrapper).css('left') == '0px') {
         $(this.wrapper).css('top', this.scroll_top+(this.dimensions.height - $(this.wrapper).height())/2+'px');
         $(this.wrapper).css('left', (this.dimensions.width - $(this.wrapper).width())/2+'px');
      } else {
         $(this.wrapper).animate({
            left: (this.dimensions.width - $(this.wrapper).width())/2,
            top: this.scroll_top + (this.dimensions.height - $(this.wrapper).height())/2
         }, 0.3);
      }
      $(this.filter).css('width', this.dimensions.width + 'px');
      $(this.filter).css('height', this.dimensions.height + 'px');
   },

   show: function(index) {
      if (!this.data[index]) return false;
      this.scroll_top = $('html').scrollTop;
      $(this.content).html();
      this.index = index;
      this.dimensions= {
         height: $(window).height(),
         width: $(window).width()
      };
      this._setFilter();
      $(window).resize(function() {
         $(this.center);
      });
      var current = this.data[this.index];
      $(this.controls.label).html(current.l);
      if ($(this.wrapper).is(':visible')) {
         $(this.wrapper).show();
         /*new Effect.Morph(this.wrapper, {
            style:'width:'+current.w+'px;height:'+(parseInt(current.h)+this.controls_height)+'px;left:'+((this.dimensions.width-current.w)/2)+'px;top:'+(this.scroll_top+((this.dimensions.height-current.h-this.controls_height)/2))+'px;',
            afterFinish: function(current) {
               $(this.content).html(this._getTemplate(this.params.template, {source: current.s, width:current.w, height:current.h}));
         }.bind(this, current)});*/
      } else {
         $(this.wrapper).css('width', current.w+'px');
         $(this.wrapper).css('height', current.h+this.controls_height+'px');
         $(this.wrapper).show();
         $(this.content).html('<object width="' + current.w + '" height="' + current.h + '" data="' + current.s + '" type="application/x-shockwave-flash"><param name="movie" value="' + current.s + '" /><param name="wmode" value="opaque" /></object>');
         /*
         $(this.content).html(this._getTemplate(this.params.template, {source: current.s, width:current.w, height:current.h}));
         */
         //alert(this.content.);
         this.center();
      }

   },

   next: function() {
      if (this.scope==0) {
         if (this.data[this.index+1]) this.show(this.index+1);
         else this.show(0);
      }
      else {
         var index = this.index+1;
         if (!this.data[index]) index=0;
         while(this.data[index].t!=this.scope) {
            index++;
            if (!this.data[index]) index=0;
         }
         this.show(index);
      }
   },

   prev: function() {
      if (this.scope==0) {
         if (this.data[this.index-1]) this.show(this.index-1);
         else this.show(this.data.length-1);
      }
      else {
         var index = this.index-1;
         if (!this.data[index]) index=this.data.length-1;
         while(this.data[index].t!=this.scope) {
            index--;
            if (!this.data[index]) index=this.data.length-1;
         }
         this.show(index);
      }
   },

   close: function() {
      $(window).resize(function() {
         $(this.center);
      });
      $(this.wrapper).hide(0,function() {
         $(this.content).html();
         $(this.wrapper).css('top', '0px');
         $(this.wrapper).css('left', '0px');
      });
      this._unsetFilter();
   },

   remove: function() {
      this.wrapper.remove();
      this._unsetFilter();
      delete Popups[this.params.id];
      delete this;
   }

});

