//
//  Application
//

window.addEvent('domready', function() {
 
	$$('a.video_pop').addEvent( 'click', function(e){
	  caption_size = ( this.title.length > 0 ) ? 46 : 0;
		pop( this.href, this.rel.split('x')[0], parseFloat(this.rel.split('x')[1]) + 46 + caption_size, 'standard' );
		return stop_event(e);
	});
	
  // $$('a.protected').addEvent( 'click', function(e){
  //   
  //   scrollTo( 0, 0 ); // scroll to the top   
  //   
  //   $('project').value = this.href.split('lab/')[1];
  //   
  //   var elem = $('login-box-container');
  //   
  //   // first store original height
  //   elem.setStyles({ 'display': 'block' });
  //   
  //   var fx = new Fx.Slide( elem, {
  //     duration: 500,
  //     transition: Fx.Transitions.Back.easeOut,
  //     onComplete: function(e)
  //     {
  //       $('password').focus();
  //     }
  //   });
  //   
  //   fx.hide(); fx.slideIn();
  //   
  //   return stop_event(e);
  //   
  // });
	
	if( $('validator') )
	{
	  $('validator').addEvent( 'click', function(e){
	    $('login-form').submit();
  		return stop_event(e);
  	});
	}
	
	
	if( $('multi-prev') )
	{
	  $('multi-prev').addEvent( 'click', function(e){
	    var img_el = $('actual-image');
	    position = ( position == 0 ) ? item_count : position - 1 ;
	    img_el.src = images[position];
	    $$('span.paginator').set( 'html', ( position + 1 ) + ' sur ' + ( item_count + 1 ) );
      return stop_event(e);
	  });
	}
 
  if( $('multi-next') )
	{
	  $('multi-next').addEvent( 'click', function(e){
	    var img_el = $('actual-image');
	    position = ( position == item_count ) ? 0 : position + 1 ;
	    img_el.src = images[position];
	    $$('span.paginator').set( 'html', ( position + 1 ) + ' sur ' + ( item_count + 1 ) );
      return stop_event(e);
	  });
	}
 
 
});

//  Stop Event Propagation Function
//  Usage: return stop_event(e); at the end of an click event
//
var stop_event = function(e)
{
  if(e.stopPropagation)
	{
		e.stopPropagation();
		e.preventDefault();
  }else{
		e.returnValue = false;
		e.cancelBubble = true;
	}
	return false;
};


//  Pop-up window opening

function pop(url, width, height, style)
{
  var style = (style == null) ? "POP" : style;

  var x = 0; var y = 0; var offset = 30;

  if (screen) { x = (screen.availWidth - width) / 2 };
  if (screen) { y = (screen.availHeight - height) / 2 };

  if (style == 'standard')
  {
    var popped = window.open(url,'popup','width='+width+',height='+(height)+',status=no,menubar=no,scrollbars=no,resizable=no,screenX='+x+',screenY='+y+',left='+x+',top='+y);
  }

  if (style == 'scroll')
  {
    var popped = window.open(url,'popup','width='+width+',height='+(height)+',status=no,menubar=no,scrollbars=yes,resizable=no,screenX='+x+',screenY='+y+',left='+x+',top='+y);
  }

  if (style == 'fullscreen')
  {
    var h = screen.availHeight;
    var w = screen.availWidth;
    var popped = window.open(url,'popup','width='+w+',height='+h+',status=no,menubar=no,scrollbars=no,resizable=no,screenX=0,screenY=0,left=0,top=0');
  }

  if (!popped.opener) { popped.opener = window; }
  popped.focus();
  return popped;
}