/*
 * Default View for estost.com
 * 
 */
 
function handleAdd2Set(data){
  messageBox.hide();
  if(data.stat=='fail'){
  	alert('an error occured:' + data.error+"\n please try to re-login! ")
  	return;
  }

}

function handleAdd2SetAJAX(o){
  messageBox.hide();
  var obj=eval('('+o.responseText+')');
  if(obj.stat=='fail' ){
    alert('request failed!');
	return;
  }
}


ZACK.kit.defaultView=function(conf){
  this.currentImage=0;
  this.tooltip=null;
  this.conf={};
  if(!conf){
  	this.conf.tooltips=true;
  	this.conf.info=true;
  	this.conf.control=true;
  }
  else  
    Object.extend(this.conf, conf)
}


ZACK.kit.defaultView.prototype={

  setConfig: function(conf) {
    if(conf)
      Object.extend(this.conf, conf);	
  },

  render: function(data){
  	
    this.imgList=data.toc || data.items;
    var numItems=this.imgList.length;
    var imgListDiv=$('imgList');
    var tpl = TrimPath.parseDOMTemplate('tplImage');
	var html='';
    for(i=0;i<this.imgList.length;i++){
	  var img=this.imgList[i];
	  img.flip=i;
      html+=tpl.process(img);

/*        var img=new Image();
        img.src=this.imgList[i].media.s;
        img.width=120;
        img.height=120;
        YAHOO.util.Event.on(img, 'click', this.showImageEvnt, i, this);
        imgListDiv.appendChild(img); **/
    }
    imgListDiv.innerHTML=html;
	
    // preloading images
    if(YAHOO.env.ua.gecko || YAHOO.env.ua.opera || YAHOO.env.ua.webkit)
      for(i=0;i<this.imgList.length;i++){
        this.imgList[i].img=new Image();
        this.imgList[i].img.src=this.imgList[i].media.l;
      }
  
    // dom ready?
    var pag='';
    for(i=0;i<this.imgList.length;i++){
      pag+='<a id="pag_'+i+'" href="javascript:dvw.showImage('+i+')" >'+(i+1)+'</a> '; 
    }
   if($('setPagination'))$('setPagination').innerHTML=pag;
    this.showImage(0);
  },


  makeRequestString: function(params){
    var rv='';
	for (var p in params) {
	  rv+= p+'='+ /*escape_utf8*/(params[p])+'&';
	}	
    return rv;
  },
 
  add2LightBox: function(){
  	messageBox.show();
  	var params={};
    params.method='zack.obj.add2Set';
    params.api_key=ZACK.api_key;
    params.ID=this.imgList[this.currentImage].ID;
    params.set_id=ZACK.user_id;
    params.auth_token=ZACK.auth_token;
    //params.rspm= 'json';
    params.target='http://bilderbeute.de/api/request.json';
    var request = YAHOO.util.Connect.asyncRequest('POST', '/zkitSRV/proxy.php',
     {success:handleAdd2SetAJAX, 
	  failure: function(o){alert('error: got no information');}
	 } , makeRequestString(params)); 
    //return true;
  },
   
  redirectDownload: function(){
    var ID=this.imgList[this.currentImage].ID;
    var sig=md5(ID+ZACK.auth_key);
    var url='http://static.bilderbeute.de/download.php?ID='+ID;
    url +='&auth_token='+ZACK.auth_token;
    url +='&sig='+md5(ID+ZACK.auth_key);
    location.href=url;
  },
   
  nextPreviousImage: function(delta){
    var ni=this.currentImage+delta;
    if(ni<0)
      return this.showImage(0);
    if(ni>this.imgList.length-1)
      return this.showImage(this.imgList.length-1);
    this.showImage(ni);
  },
  
  showIndex:function (){
    if(this.tooltip)delete this.tooltip;
      $('imgDiv').style.display='none';
      if($('imgInfo'))$('imgInfo').style.visibility='hidden';
      $('imgList').style.display='block';
  },
  
  showInfo: function (e, id){
    //$('imgDiv').style.display='none';
    //$('imgList').style.display='none';

    var info=this.imgList[id];
    info.copyright_holder='';
    info.location_country='';
    info.location_region='';
    info.alias='';
    if(!info.description2)info.description2=info.title;
    tpl = TrimPath.parseDOMTemplate('tplImageInfo');
    if($('imgInfo'))$('imgInfo').innerHTML=tpl.process(info)
    if($('imgInfo'))$('imgInfo').style.visibility='visible';
  },

  sizeImageOnLoad: function(e, imgL){

    var boxH=480; 
    if(imgL.clientHeight>boxH){
	  imgL.style.width = Math.round(imgL.clientWidth * (boxH/imgL.clientHeight)) + "px"; 	
	  imgL.style.height = boxH + "px";
    }
    var boxW=640; // 
    if(imgL.clientWidth>boxW){
	  imgL.style.height = Math.round(imgL.clientHeight * (boxW/imgL.clientWidth)) + "px"; 	
	  imgL.style.width = boxW + "px";
    }
    imgL.style.visibility='visible';
    //$('imgDiv').style.height=imgL.style.height;
    //$('imgInfo').style.width=imgL.style.width;
  },
  
  showImageEvnt: function(e, id){
    return this.showImage(id)
  },
  

  showImage: function(id){

    var ci=this.currentImage;
    $('pag_'+ci).style.color='#999';
    this.currentImage=id;
    $('pag_'+id).style.color='#fff';
  
    if(this.tooltip)delete this.tooltip;
  
    if($('imgDiv').style.display=='none'){
      $('imgList').style.display='none';
      if($('imgInfo'))$('imgInfo').style.visibility='hidden';
      $('imgDiv').style.display='block';
    }
    $('imgDiv').innerHTML='';

    // do we already have a scaled image?
    if(imgL=this.imgList[id].image){
      $('imgDiv').appendChild(imgL)
    }
    else {
      var imgL=document.createElement('img');
      this.imgList[id].image=imgL;
      imgL.src=this.imgList[id].media.l;
      imgL.style.visibility='hidden';
      //if(YAHOO.env.ua.ie==7)
      //  imgL.style.visibility='visible';
      YAHOO.util.Event.on(imgL, 'click', this.showInfo, id, this);
      YAHOO.util.Event.on(imgL, 'load', this.sizeImageOnLoad, imgL, this);
      $('imgDiv').appendChild(imgL)
    }
    var boxH=480; // should be defined elsewhere

    var imgInfo =this.imgList[id].description+'<br/>';
    imgInfo+='<b>Date taken:</b> '+this.imgList[id].date_taken+'<br/>';
    imgInfo+='<b>photographer:</b> '+this.imgList[id].author+'<br/>';
    imgInfo+='<b>File:</b> '+this.imgList[id].alias;
    //if(this.conf.tooltips)
      this.tooltip = new YAHOO.widget.Tooltip("ttx", { context:'tooltip_i', text:imgInfo, autodismissdelay:10000 }); 
  
/**    if(imgL.clientHeight>boxH){
	  imgL.style.width = Math.round(imgL.clientWidth * (boxH/imgL.clientHeight)) + "px"; 	
	  imgL.style.height = boxH + "px";
    } **/
    this.showInfo({}, id);
  }
} // end defaulView
