var sfHover = function() {
	if (!document.getElementsByTagName) return false;
	var ls = document.getElementById("menuprincipal").getElementsByTagName("li");
	for (var i=0; i< ls.length; i++) {
		ls[i].onmouseover=function() {this.className+=" hover";}
		ls[i].onmouseout=function() {this.className=this.className.replace(new RegExp(" hover\\b"), "");}
	}
};
if (window.attachEvent) window.attachEvent("onload", sfHover);

var Enquete = {
  votar:function() {
    Form.send('EnqueteForm'); return;
  },
  select:function(ref) {
    var list = Form.getElements('EnqueteForm');
    for(var i=0;i<list.length;i++) if(list[i].type=='radio') list[i].parentNode.className='';
    ref.parentNode.className = 'selected';
    ref.blur();
  }
}

var Gallery = {
  Pictures:null,
  page:0,
  index:0,
  process:function(){
    if(!Gallery.Pictures) return;
    if(!$('image-large') || !$('image-list')) return;
    Gallery.viewPage(0);
  },
  viewPage:function(p) {
    var k = p*4;
    if(k>=Gallery.Pictures.length) return;
    $E('image-list').clearNodes();
    var c=0;
    for(var i=k;i<k+4;i++) {
      if(Gallery.Pictures[i]) {
        $$('td').addClass('item').appendTo('image-list').append($$('img').set('src',
          Gallery.imagesURL+Gallery.Pictures[i].file+'?w=70&h=51').set('onclick',Gallery.zoom(i)));
        c++;
      }
    }
    if(c<4) for(i=1;i<=4-c;i++) $$('td').appendTo('image-list').set('innerHTML','&nbsp;');
    Gallery.page = p;
    Gallery.zoom(k)();
  },
  zoom:function(id){
    return function(){
      if(!Gallery.Pictures[id]) return;
      var src = Gallery.imagesURL + Gallery.Pictures[id].file;
      $E('image-large').clearNodes();
      $$('a').append($$('img').set('width',250).set('height',170).set('src',src+'?w=250&h=170')).appendTo('image-large').set('href',src);
      //.set('onclick',Gallery.over);
      Gallery.index = id;
    }
  }/*,
  over:function(){
    var src = this.href;
    OverBox.show({width:512,height:384,off:10});
    $$('img').set('src',src+'?w=502&h=374').appendTo(document.body).css({display:'none'}).set('onload',
      function(){$E('container').css({'background':'url('+this.src+') no-repeat center'})});
    $$('a').set('innerHTML','&times;').css({display:'block','width':'20px',color:'#000',textAlign:'center','textDecoration':'none',fontSize:'16pt','float':'right','background':'#fff'}).appendTo('container').set('onclick',OverBox.close).set('href','#');
    return false;
  }*/,
  overbox:function(id){
    if(!id) return;
    OverBox.show({width:395,height:285,off:20,url:'/galerias-ver.php?id='+id});
  }
};

var OverBox = {
  url:'',
  pointer:null,
  over:null,
  supr:null,
  object:null,
  width:400,
  height:300,
  off:25,
  enabled:false,
  init:function() {
    this.pointer = $P(this,true);
    this.over = $$('div').set('id','overlay').appendAsFirst(document.body);
    this.supr = $$('div').set('id','superlayer').appendAsFirst(document.body).addClass('round');
    this.object = $$('div').set('id','container').appendTo('superlayer').addClass('round');
    this.over.onclick = OverBox.close;
    return this;
  },
  show:function(data) {
    this.enabled = true;
    if(!this.over) this.init();
    var sz = Screen.pageSize(), sh = sz.h;
    var ps = Screen.pageScroll().y;
    if(/[%]/.test(data.width)) data.width = Math.ceil(parseInt(data.width.replace('%','')) * sz.w) / 100 ;
    if(/[%]/.test(data.height)) data.height = Math.ceil(parseInt(data.height.replace('%','')) * sz.h) / 100;
    $extend(this,data);
    this.over.css({height:(ps + sh - 2)+'px'}).show();
    window.scrollTo(0,0);
    this.supr.css({padding:this.off+'px'}).show();//css({height:this.height+'px',width:this.width+'px'})
    this.object.css({height:this.height+'px',width:this.width+'px'}).show();//-this.off
    this.supr.centerX();
    this.object.centerX();
    if(this.url!='') new Ajax.Request({url:this.url,target:'container'});
    return this;
  },
  hide:function(){
    if(!this.enabled) return this;
    this.enabled = false;
    this.over.destroy();
    this.supr.destroy();
    //this.object.destroy();
    this.over = null;
    this.supr = null;
    this.object = null;
    return this;
  },
  close:function(){
    OverBox.hide();
  }
};

window.onscroll = function() {
  if(!OverBox.enabled) return;
  var s = Screen.pageScroll().y;
  //var e=$E('superlayer'),p=e.getCenter();
  //e.setPosition(p.x,p.y);
  //var e=$E('container'),p=e.getCenter();
  //e.setPosition(p.x,p.y);
  var h = s + Screen.pageSize().h-2;
  $E('overlay').css({height:h+'px'});
};

