var num_imgs = 0;
var activa_img = 0;
var periodical = null;

document.observe('dom:loaded', function() {
    $$('ul#menu li.menu-item').each(function(item) {
         Event.observe(item, 'mouseover', function(event) {
            menuOut();
            item.setStyle({backgroundPosition: "0px 0px"})
         });

         Event.observe(item, 'mouseout', function(event) {
            menuIn();
         });
    });
 
  var container = $(document.body);
  if (container) {    
    container.observe('click', function(e) {
      var el = e.element();
       /* Ajax paginador */
      if (el.match('.pagination a')) {
        new Ajax.Request(el.href, { method: 'get',
        onComplete: function() {
            create_photo_view();
          }
        });
        e.stop();
      }
      else if(el.match('#go-to-map')) {
         e.stop();
         Effect.ScrollTo($('content-map-obras'), {offset:-100});
      }
      else if(el.match('#go-to-up')) {
         e.stop();
         Effect.ScrollTo($('recursos-container'), {offset:-100});
      }
    });
  }

  create_photo_view();

  if($('show-obra')) {
    $('show-obra').setStyle({display:'none'});
  }

  if($('banner-content')) {    
    baner_control();
    point_controller();
    new PeriodicalExecuter(function(pe) {
         periodical = pe;
         baner_control(-1);
    }, 8);
  }
});

function point_controller() {
    $$('#point_controler li').each(function(item,index){
        Event.observe(item, 'click', function(){
            if(periodical != null)
                periodical.stop();           
            baner_control(index);
        });
    });
}

function baner_control(index) {
  var imagenes = new Array();
   if($$('#banner-content object') != '')
      imagenes = $$('#banner-content div')
   else
      imagenes = $$('#banner-content div')

   
   imagenes = imagenes.concat($$('#banner-content img'));
   rotativo_controller = $$('#point_controler li');

   num_imgs = 0;
   imagenes.each(function(item) {
      item.setStyle({display:'none'});
      num_imgs++;
   });

   if(index >= 0)
       activa_img = index;   

   activar_boton_rotativo(activa_img);

   imagenes[activa_img].setStyle({display:'block'});
   if(activa_img == num_imgs-1 )
      activa_img = 0;
   else
      activa_img++;   
}

function reset_pointer() {
   $$('#point_controler li').each(function(item) {
        item.removeClassName('punto_activo');
   });
}

function activar_boton_rotativo(index) {
   var pulsado = $('point_control_' + index.toString());

   reset_pointer();
   pulsado.addClassName('punto_activo');
}

function create_photo_view() {
     $$('.item-galery a img').each(function(item) {
         Event.observe(item, 'click', function(event) {
           videoVisible(false);
           modal_factory(item.up(),{overlayOpacity: 0.75,fade: true},'modal');
         });
    });
}

 function modal_factory(container,options,modal_class){

     var modal_header = new Element('div',{
         className: 'modal_header'
     });

     var modal_close = new Element('a',{
         className: 'modal_close'
     });

     var modal_contents = new Element('div',{
        className: 'modal_contents'
     });

     var w = new Control.Modal(container,Object.extend({
         className: modal_class,
         closeOnClick: modal_close,
         insertRemoteContentAt: modal_contents,
         afterClose: function(){
            videoVisible(true);
            w.destroy();
         }
     },options || {}));

     modal_close.update('Close X');
     w.container.insert(modal_header);
     modal_header.insert(modal_close);
     w.container.insert(modal_contents);
     
     return w;
 }

function menuOut() {
    $$('ul#menu li.menu-item').each(function(item) {
        item.setStyle({backgroundPosition: "0px -20px"});
    });
}

function menuIn() {
    $$('ul#menu li.menu-item').each(function(item) {
        item.setStyle({backgroundPosition: "0px 0px"});
    });
}

function showObras() {
    $('show-obra').appear();
    Effect.ScrollTo($('anchor-obra'), {offset:100});
    create_photo_view();
}

function showRecursos() {
    $('show-recursos').appear();
    Effect.ScrollTo($('recursos-container'), {offset:100});
}

function videoVisible(val) {
    if($('flash_player_container')) {
        if(val) {
            $('flash_player_container').setStyle({visibility:'visible'})
        } else {
            $('flash_player_container').setStyle({visibility:'hidden'})
        }
    }
}