// Tabs code
var MGFX = MGFX || {};

MGFX.Tabs = new Class({
	
	Extends: MGFX.Rotater,
	
	options: {
		autoplay: false,
		onShowSlide: function(slideIndex) {
			this.tabs.removeClass('active');
			this.tabs[slideIndex].addClass('active');
		}
	},
	
	initialize: function(tabs, slides, options){
		this.setOptions(options);
		this.tabs = $$(tabs);
		this.createTabs();
		if(this.options.hash && window.location.hash) {
			var hash = window.location.hash.substring(1);
			this.tabs.each(function(el, index) {
				if(el.get('id') == hash) {
					options.startIndex = index;
				}
			});
		}
		return this.parent(slides,options);
	},
	
	createTabs: function () {
		this.tabs.each(function(tab,index){
			tab.addEvent('click', function(event){ 
				event.preventDefault();
				this.showSlide(index);
				this.stop(true);
			}.bind(this));
		}.bind(this));
	}.protect()
	
});

function showAttachments(){
	$( 'popup_attachment' ).removeClass( 'hidden' );
	$( 'popup_links' ).addClass( 'hidden' );
	$( 'tool_popup_overlay' ).removeClass( 'hidden' );
}

function showLinks(){
	$( 'popup_attachment' ).addClass( 'hidden' );
	$( 'popup_links' ).removeClass( 'hidden' );
	$( 'tool_popup_overlay' ).removeClass( 'hidden' );
}

function hidePopup(){
	$( 'popup_attachment' ).addClass( 'hidden' );
	$( 'popup_links' ).addClass( 'hidden' );
	$( 'tool_popup_overlay' ).addClass( 'hidden' );

	// attachement
	$('attachlinks').getElements('tr[id!=trbijlages]').each(function(el){
		el.destroy();
	});
	$('attachments').getElements('input').each(function(el){
		var tr = $('trbijlages').clone();
		tr.setStyle('display', '');
		tr.getElement('td').innerHTML = el.value;
		tr.inject('attachlinks');
	});

	// links
	$('tablelinks').getElements('tr[id!=trlinks]').each(function(el){
		el.destroy();
	});
	$('popup_links').getElements('tr').each(function(trel){
		var value = '';
		var el1 = trel.getElements('input').each(function(a){
			value += ' '+a.value;
		});

		value = value.trim();
		if (value != 'http://')
		{
			var tr = $('trlinks').clone();
			tr.setStyle('display', '');
			tr.getElement('td').innerHTML = value;
			tr.inject('tablelinks');
		}
	});




}

window.addEvent('domready',function(){

	//showAttachments();
		
	// Add tool , attachments stuff
	if( $( 'addAttachment' ) && $( 'attachments' ) ){
		$( 'addAttachment' ).addEvent( 'click' , function( e ){
			e.stop();
			
			var div1 = new Element( 'div' , {
				styles : { height : '30px' }
			} ).inject( $( 'attachments' ) );
			
			var div2 = new Element( 'div' , {
				styles : { float : 'left' },
				html : '<input type="file" name="attachment[]" />'
			} ).inject( div1 );
			
			var div3 = new Element( 'div' , {
				styles : { float : 'right' }
			} ).inject( div1 );
			
			var button = new Element( 'button' , { html : '&nbsp;' , 'class' : 'remove_row' , type : 'button' } );
			button.addEvent( 'click' , function(){ this.getParent().getParent().destroy(); } ).inject( div3 );
			
			/*
			var el_tr = new Element( 'tr' , { 'class' : 'row' } );
			//el_tr.inject( $( 'attachments' ) );
			$( 'attachments' ).grab( el_tr );
			
			var el_td = new Element( 'td' );
			//el_td.inject( el_tr );
			el_tr.grab( el_td );
			
			var el_field = new Element( 'input ' , { type : 'file' , name : 'attachment[]' } )
			//el_td.inject( el_td );
			el_td.grab( el_field );
			
			console.log( $( 'attachments' ).innerHTML );
			*/
		} );
	}
	

	// Table sorting
	if( $( 'htmltable' ) ){
		var ht = new HtmlTable( $( 'htmltable' ) , {
			
		} );
	}

	$$( '.clearFilter' ).each( function( el , ind ){
		el.addEvent( 'click' , function( e ){
			e.stop();
			this.parentNode.parentNode.parentNode.getChildren()[ 1 ].getChildren().each( function( li , ind2 ){
				li.getChildren( 'input' )[ 0 ].checked = false;
			} );
		} );
	} );

  if ( document.id('slideHome') ) {
    this.tabs4 = new MGFX.Tabs('#slideHome .btn','#slideHome .img',{
      autoplay: true,
      transitionDuration:500,
      hover:false
    });
  }
  if ( document.id('partnersLogo') ) {
    this.tabs5 = new MGFX.Tabs('#partnersLogo .btn','#partnersLogo .feature',{
      autoplay: true,
      transitionDuration:500,
      slideInterval:4000,
      hover:false
    });
  }

  var menushow;
  var menuhide;
  var timermenushow;
  var timermenuhide;

  menushow = function(list, elem) {
    //console.log('show');
    list.setStyle('visibility', 'visible');
    elem.addClass('hover');
  }

  menuhide = function(list, elem) {
  //  console.log('hide');
    list.setStyle('visibility', 'hidden');
    elem.removeClass('hover');
  }

  $('drop_down_menu').getElements('li.menu').each( function( elem ){
    var list = elem.getElement('ul.links');
    if(!list || list == null) return;
    list.setStyle('visibility', 'hidden');
    elem.addEvents({
      'mouseenter' : function(){
        timermenushow = menushow.delay(200, this, [list, elem] );
      },
      'mouseleave' : function(){
        $clear(timermenushow);
        timermenuhide = menuhide.delay(200, this, [list, elem] );
      }
    });
  })
  
  
  // Check all forms
  $$( 'form.validate' ).each( function( form , formid ){
  	form.addEvent( 'submit' , function( e ){
  		e.stop();
  		checkForm( form );
  	} );
  } );
  

});


function checkForm( form ){
	var errors = 0;
	var focusElm = null;
	
	if( $( 'agreementMessage' ) ){ $( 'agreementMessage' ).setStyle( 'display' , 'none' ); }
	
	$$( '.required' ).each( function( elm , elmid ){
		var tn = elm.tagName.toLowerCase();
		if( tn == 'input' || tn == 'textarea' ){
			if( elm.id == 'acceptAgreement' && !elm.checked ){
				if( $( 'agreementMessage' ) ){ $( 'agreementMessage' ).setStyle( 'display' , '' ); }
				if( focusElm == null ) focusElm = elm;
			}else{
				if( elm.value == '' ){
					errors++;
					elm.addClass( 'error' );
					if( focusElm == null ) focusElm = elm;
				}else{
					elm.removeClass( 'error' );
				}
			}
		}else if( tn == 'select' ){
			
		}
	} );
	
	if( focusElm != null ) focusElm.focus();
	
	if( errors == 0 ) form.submit()
}
