/**
 * @author Jan Cinert <jan.cinert@gmail.com>
 * @require prototype.js, scriptaculous.js
 */

PanzaniAnimatedBox = Class.create({
	initialize: function( box, launcher ){ 
	  this.init( box, launcher );
	},
	init: function( box, launcher ) {
	  this.hideDelay = 1200;
		this.visible = false;
		this.timeout = false;
		this.progress = false;
		this.box = $(box);
		this.launcher = $(launcher);
		
		Event.observe( this.box, 'mouseover', this.show.bindAsEventListener( this ) );
    Event.observe( this.box, 'mouseout', this.hide.bindAsEventListener( this ) );
    Event.observe( this.launcher, 'mouseover', this.show.bindAsEventListener( this ) );
    Event.observe( this.launcher, 'mouseout', this.hide.bindAsEventListener( this ) );
		
		this.box.hide();
		this.launcher.show();
		Element.setStyle( this.box, { opacity: 0.8 } );
	},
	show: function(){   
	  if( this.progess ) return;
	  
	  if( this.timeout ) {
	    window.clearTimeout( this.timeout );
	    this.timeout = false;
	  }	  
	  if( !this.visible ) {
	    this.showBox();
	  }	  
	},
	hide: function(){
	  if( this.progess ) return;
		if( !this.visible ) {
	    return;
	  }	  
	  if( !this.timeout ) {
	    this.timeout = window.setTimeout( this.hideBox.bind( this ), this.hideDelay );
	  }

	},
	showBox: function(){
	  this.visible = true;
    this.progress = true;
    this.launcher.hide();
	  new Effect.SlideUpIn( this.box, { duration: 0.5, afterFinish: this.effectFinish.bind( this ) } );
	  //this.box.show();
	},
	hideBox: function(){
	  this.visible = false;
	  this.progress = true;
	  this.launcher.show();
	  new Effect.SlideDownOut( this.box, { duration: 0.5, afterFinish: this.effectFinish.bind( this ) } );
		//this.box.hide();
	},
	effectFinish: function() {
	  this.progress = false;
	}
});


SearchBox = Class.create( PanzaniAnimatedBox, {
	initialize: function( box, launcher ){ 
	  this.init( box, launcher );
	},
	showBox: function(){
	  this.visible = true;
    this.progress = true;
    this.launcher.hide();
	  new Effect.SlideUpIn( this.box, { duration: 0.4, afterFinish: this.effectFinish.bind( this ) } );
	  //this.box.show();
	},
	hideBox: function(){
	  this.visible = false;
	  this.progress = true;
	  this.launcher.show();
	  new Effect.SlideDownOut( this.box, { duration: 0.4, afterFinish: this.effectFinish.bind( this ) } );
		//this.box.hide();
	}
});


LoginBox = Class.create( PanzaniAnimatedBox, {
	initialize: function( box, launcher ){ 
	  this.init( box, launcher );
	}

});


Effect.SlideUpIn = function(element) {
/* 
	SlideUpIn need to have the content of the element wrapped in a container element with fixed height!
*/
	element = $(element).cleanWhitespace();
	var elementDimensions = element.getDimensions();
	return new Effect.Parallel ( [
		new Effect.Move(element, { y: -(element.getHeight()), sync: true, mode: 'relative' }),
		new Effect.Scale(element, 100, 
			Object.extend({
				sync: true,
				scaleContent: false,
				scaleX: false,
				scaleFrom: 0,
				scaleMode: {originalHeight: elementDimensions.height, originalWidth: elementDimensions.width},
				beforeSetup: function(effect) {
					effect.element.hide();
				},
				afterSetup: function(effect) {
					effect.element.makeClipping().setStyle({height: '0px'}).show();
				},
				afterFinishInternal: function(effect) {
					effect.element.undoClipping();
				}
			}, arguments[1] || {})
		)
	], Object.extend({
		afterSetup: function(effect) {
			effect.effects[0].element.setStyle({top: elementDimensions.height + 'px' });
		}
	}, arguments[1] || {}));
}

Effect.SlideDownOut = function(element) {
/* 
	SlideDown need to have the content of the element wrapped in a container element with fixed height!
*/
	element = $(element).cleanWhitespace();
	var elementDimensions = element.getDimensions();
	return new Effect.Parallel ( [
		new Effect.Move(element, { y: element.getHeight(), sync: true, mode: 'relative' }),
		new Effect.Scale(element, 0,
			Object.extend({ 
				sync: true,
				scaleContent: false,
				scaleX: false,
				restoreAfterFinish: true,
				beforeSetup: function(effect){
					effect.element.makeClipping();
				},
				afterFinishInternal: function(effect) {
					effect.element.hide().undoClipping();
				}
			}, arguments[1] || {})
		)
	], Object.extend({
	}, arguments[1] || {}));
}

observeSearchForm = function() {
  Event.observe( window, 'load', function() {
    form = $$('div#content form')[0];
    observeSearchForm.post( form );
    new Form.Observer( form, 0.3, function( form, value ){
      observeSearchForm.post( form );
    })
  })
}

observeSearchForm.post = function( form ) {
  new Ajax.Request( 
      AjaxUrl + '?mode=4', 
      {
        method: 'post',
        parameters: form.serialize(true),
        onComplete: function( transport ){ $('resultCount').update( transport.responseText ) }
      }
    );
}


observeAddCommentBtn = function() {
  document.observe("dom:loaded", observeAddCommentBtn.handle )
  Event.observe( window, "load", observeAddCommentBtn.handle )
}

observeAddCommentBtn.handle = function( event ) {
  if( observeAddCommentBtn.run ) return;
  observeAddCommentBtn.run = true;
  if( isPOST ) {
    $('btn_add_comment').hide();
  }
  else {
    $('commentForm').hide();
  }
  $('btn_add_comment').observe( 'click', function( event ) { 
      new Effect.toggle( $('commentForm'), 'blind' ); 
      $('btn_add_comment').hide() 
  } );
}

observeShowAllComments = function() {
  document.observe("dom:loaded", observeShowAllComments.handle );
  Event.observe( window, "load", observeShowAllComments.handle );
  
  $('btn_show_all_comments').observe( 'click', observeShowAllComments.show );
}

observeShowAllComments.show = function( event ) {
  var elements = $$('div.jot-row');
  for( var i = 4; i < elements.size() - 1; i++ ) {
    elements[i].show();
  }
  Event.element( event ).hide();
}

observeShowAllComments.hide = function() {
  var elements = $$('div.jot-row');
  for( var i = 4; i < elements.size() - 1; i++ ) {
    elements[i].hide();
  }
  if( elements.size() < 5 ) {
    $('btn_show_all_comments').hide();
  }
}

observeShowAllComments.handle = function( event ) {
  if( observeShowAllComments.run ) return;
  observeShowAllComments.run = true;
  observeShowAllComments.hide();
}

Event.observe( window, 'load', function() {
  	if (navigator.userAgent.indexOf('Opera') != -1) {
    	var oLink = document.createElement("link")
      oLink.href = "assets/templates/default/opera.css";
      oLink.rel = "stylesheet";
      oLink.type = "text/css";
      document.getElementsByTagName("head")[0].appendChild(oLink);
    }
    
    if ( typeof( isHP ) == 'boolean' && isHP == true ) {
      if( document.getElementById( 'wlpeLoginBtn' ) ) $('wlpeLoginBtn').hide();
      $('searchBtn').hide();
      if( document.getElementById( 'wlpeLogin' ) ) $('wlpeLogin').addClassName('static');
      $('searchForm').addClassName('static');
      if( document.getElementById( 'wlpeLogin' ) ) $('wlpeLogin').show();
      $('searchForm').show();
    }
    else {
      if( document.getElementById( 'wlpeLogin' ) ) loginBox = new LoginBox( 'wlpeLogin', 'wlpeLoginBtn' );
      if( document.getElementById( 'searchForm' ) ) searchBox = new SearchBox( 'searchForm', 'searchBtn' );
    }
  }
)