// --------------------------------------------------------------------------------
//  GALLERY
// --------------------------------------------------------------------------------

var activettGallery = false;

function ttGallery(data){
    this.item = data;

    this.isActive = false;

    this.bg = null;
    this.cnt = null;
    this.arrr = null;
    this.arrl = null;
    this.xbtn = null;
    this.photo = null;
    this.opts = null;
    
    this.curpos = 0;
    this.nxtpos = -1;
    this.swing = false;
    
    this.tmp = null;
    this.inited = false;
}

ttGallery.prototype.init = function(){
    this.bg = $('#gWndBG')[0];
    this.cnt = $('#gWndCNT')[0];
    if(this.cnt){
        this.arrl = $(this.cnt).find('.arrl')[0];
        this.arrr = $(this.cnt).find('.arrr')[0];
        this.xbtn = $(this.cnt).find('.close')[0];
        this.screen = $(this.cnt).find('.screen')[0];
        this.photo = $(this.screen).find('.item')[0];
        
        this.opts = $(this.cnt).find('.opt');
        if(!this.opts.length){this.opts = false;}
        this.inited = true;
        return (true);
    }
    return (false);
}

ttGallery.prototype.setEvents = function(obj,drop){
  if(!obj){obj = this;}
  if(drop){
    if(obj.arrl) $(obj.arrl).unbind('click',obj.next);
    if(obj.arrr) $(obj.arrr).unbind('click',obj.next);
    if(obj.photo) $(obj.photo).unbind('click',obj.next);
    if(obj.xbtn) $(obj.xbtn).unbind('click',obj.close);
    if(obj.cnt) $(obj.cnt).unbind('click',obj.cnt_click);
  }else{
    if(obj.arrl) $(obj.arrl).bind('click',{dir:false,refObj:obj},obj.next);
    if(obj.arrr) $(obj.arrr).bind('click',{dir:true,refObj:obj},obj.next);
    if(obj.photo) $(obj.photo).bind('click',{dir:true,refObj:obj},obj.next);
    if(obj.xbtn) $(obj.xbtn).bind('click',{refObj:obj},obj.close);
    if(obj.cnt) $(obj.cnt).bind('click',{refObj:obj},obj.cnt_click);
  }
}

ttGallery.prototype.open = function(pos){

    if(!this.inited){if(!this.init()){return;}}

    if(pos < 0){pos=this.item.length-1;}
    else if(pos >= this.item.length){pos = 0;}
    
    activettGallery = this;
    
    if(this.item[pos]){
        if(this.swing){return;}
        this.swing = true;
        
        if(!this.isActive){
            $(this.bg).fadeIn(300);
            $(this.cnt).css('top',0);//$(document).scrollTop()
            $(this.cnt).fadeIn(300);
            this.setEvents(this);
            this.isActive = true;
        }
        
        this.tmp = new Image();
        $(this.tmp).bind('load', {refObj: this,pos: pos},function(e){
            var refObj = e.data.refObj;
            var item = refObj.item[e.data.pos];
            
            try{
              if(refObj.opts && refObj.opts.length){
                  for (var i = 0; i < refObj.opts.length; i++) {
                      $(refObj.opts[i]).html((item.opts.length ? (item.opts[i] ? item.opts[i].replace("\n", " ") : "") : "&nbsp;"));
                  }
              }
            }catch(e){}

            refObj.pos = e.data.pos;
            
            var image = this;
            $(refObj.photo).fadeOut(150,function(){
              refObj.photo.style.backgroundImage = 'url('+image.src+')';
              $(refObj.screen).animate({width:image.width,height:image.height},100,function(){
                $(refObj.photo).fadeIn(200);
              });
            });

            refObj.swing = false;
            refObj.tmp = null;
        });
        
        this.tmp.src = this.item[pos].image;
        
    }
    else {
        alert('ERROR: no such item ' + pos);
    }
}

ttGallery.prototype.click = function(e){
  if(typeof(e.data.refObj) != 'undefined' && typeof(e.data.pos) != 'undefined'){
    e.data.refObj.open(e.data.pos);
  }
}

ttGallery.prototype.cancelEvent = function(event){
  var ev=event.originalEvent||event;
  if(ev.preventDefault){ev.preventDefault();}
  if(ev.stopPropagation){ev.stopPropagation();}
  ev.cancelBubble=true;ev.returnValue=false;return(false);
}

ttGallery.prototype.cnt_click = function(e){
  var ro = e.data.refObj;
  if((e.target.id == 'gWnd') || (e.target.id == 'gWndCNT')){
    ro.close(e);
  }else{
    return(ro.cancelEvent(e));
  }
}

ttGallery.prototype.next = function(e){
    var refObj = e.data.refObj;
    var dir = e.data.dir;
    var newpos = refObj.pos + (dir ? 1 : -1);
    refObj.open(newpos);
}

ttGallery.prototype.close = function(e){
    var refObj = e.data.refObj;
    refObj.setEvents(refObj,true);

    if(refObj.opts.length){for(var i=0;i<refObj.opts.length;i++){$(refObj.opts[i]).html('');}}

    $(refObj.bg).fadeOut(300);
    $(refObj.cnt).fadeOut(300,function(){
      refObj.photo.style.backgroundImage = '';
      refObj.isActive = false;
      activettGallery = false;
    });
}
