/* reg exp functions */
function validateEmail(elementValue)
{
  var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;  
  return emailPattern.test(elementValue);
}  

/* function check contact form fields*/
function checkContactFields(form)
{

   var error = 0;
  form.getElements('input[class*=required]').each(function(el)
  {
    el.getParent().getPrevious().removeClass('error');
    if (el.hasClass('email') && !validateEmail(el.value) )
    {
      el.getParent().getPrevious().addClass('error');
      error++;
    }
    else
    if (el.hasClass('required') && el.value.length == 0 )
    {
      el.getParent().getPrevious().addClass('error');
      error++;
    }
  });
  if (error == 0)
  {
    form.submit();
  }
}

/* functions module projects */
function getProjectsByDiscipline(url, disciplineID)
{
  var myRequest = new Request({method: 'get', url: url, onComplete: function(response)
    {
      $('contentProjects').set('html', response);
      $('contentProjects').getElements('div[class=project]').each(function(el)
      {
        el.addEvent('mouseenter', function(){
            showOverlayProjects(el);
        });
        el.addEvent('mouseleave', function(){
            hideOverlayProjects(el);
        });
      })
    }
  });
  myRequest.send('disciplineID='+disciplineID+'&m=getProjectsByDiscipline');
}

function setActiveTab(activeEl)
{
  $('customerNav').getElements('a').each(function(el)
  {
    if (el.hasClass('active') && el != activeEl)
    {
      el.removeClass('active');
    }
    if (el == activeEl)
    {
      el.addClass('active');
    }
  });
}

function setProjectsCustomers(url, customerID)
{
  var myRequest = new Request({method: 'get', url: url, onComplete: function(response)
    {
      $('contentProjects').set('html', response);
      $('contentProjects').getElements('div[class=project]').each(function(el)
      {
        el.addEvent('mouseenter', function(){
            showOverlayProjects(el);
        });
        el.addEvent('mouseleave', function(){
            hideOverlayProjects(el);
        });
      })
    }
  });
  myRequest.send('customerID='+customerID+'&m=getProjectsByCustomer');
}

function setClients(url)
{
  var myRequest = new Request({method: 'get', url: url, onComplete: function(response)
    {
      $('contentProjects').set('html', response);
    }
  });
  myRequest.send('m=getCustomers');
}

var myFx1;
var myFx2;

function showOverlayProjects(el)
{
  var overlay = el.getElement('div')
  var myFx2 = overlay.retrieve('tween', new Fx.Tween(overlay, {link:'cancel'}));
  myFx2.start('height', '50');

}

function hideOverlayProjects(el)
{
  var overlay = el.getElement('div');
  var myFx2 = overlay.retrieve('tween', new Fx.Tween(overlay, {link:'cancel'}));
  myFx2.start('height', '0');
}

/* end functions projects */


function setfontSize( size )
{
  var sizes = new Array( 'normal', 'bigger', 'biggest' );

  if( !sizes.contains( size ) )
    size = sizes[0];

  if(document.id('linkFontSize'))
    document.id('linkFontSize').setProperty( 'href', 'layout/default/' + size + '.css' );
  else
    new Asset.css('/layout/default/' + size + '.css', {id: 'linkFontSize'});

  Cookie.write('fontSize', size, { path:'/', duration:999});
}



// 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()
	
});

var currentSideTab = 'search';
function sidebarTab(tab) {
  var showTab = (tab=='contact'?'contact':'search');
  var hideTab = (showTab=='contact'?'search':'contact');
  $('sideBar-'+showTab).setStyle('display', '');
  $('sideBar-'+hideTab).setStyle('display', 'none');
  $('sidebarTab'+showTab.capitalize()).getParent().addClass('active');
  $('sidebarTab'+hideTab.capitalize()).getParent().removeClass('active');

  // clicked on tab that is already open & sidebar is open -> close sidebar
  var sb = $('sideBarContents');
  var sbw = sb.getStyle('width').toInt();
  if(currentSideTab == showTab && sbw > 290) {
    sb.morph({'width':0});
   (function(){ sb.setStyle('display', 'none'); }).delay(500);
  } else {
    sb.morph({'width':300});
    sb.setStyle('display', 'block');
  }

  currentSideTab = showTab;
}

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

  document.addEvent('click', function (evt)
  {
    if ( !$('sideBarContents').hasChild(evt.target) )
    {
      if ($('sideBarContents').getStyle('width').toInt() > 0)
        sidebarTab($('sideBar-contact').getStyle('display') == 'none'?'search':'contact'  );
    }
  });

  if ($('referer') && $('contentProjects'))
  {
    if ($('referer').value == 'discipline')
    {
      getProjectsByDiscipline('index.php?a=17&action=ajax', $('refererID').value);
    }
    else if ($('referer').value == 'customer')
    {
      setProjectsCustomers('index.php?a=17&action=ajax', $('refererID').value);
    }
  }

  if ( document.id('sideBar') ) {
    $$('#sidebarBtns a').each(function(el) {
      el.addEvent('click', function(e){
        e.stop();
        if(el.id == 'sidebarTabContact') {
          sidebarTab('contact');
        } else if(el.id = 'sidebarTabSearch') {
          sidebarTab('search');
        }
      });
    });
  }
//  if ( document.id('sideBarTab') ) {
//		document.id('sideBarTab').addEvent('click', function(e){extendContract()});
//	}

  if(typeof tooltipTheWords != 'undefined') {
    tooltipWords = new TooltipWords(tooltipTheWords, 'content');
  }

	new NavDropDown('nav');
 
  if ( document.id('contentSlide') ) {
    this.tabs4 = new MGFX.Tabs('.btn','.contentImage',{
      autoplay: true,
      transitionDuration:500,
      slideInterval:4000,
      hover:true
    });
  }
  if ( document.id('projectImgSlide') ) {
    this.tabs5 = new MGFX.Tabs('.btn','.projectImage',{
      autoplay: true,
      transitionDuration:500,
      slideInterval:4000,
      hover:true
    });
  }

	if ( document.id('img') ) {
		this.tabs2 = new MGFX.Tabs('#list ul li','.discipline',{
			autoplay: false,
			transitionDuration:500,
			slideInterval:3000,
			hover:false
		});

		this.tabs3 = new MGFX.Tabs('.logoCustomer','.feature',{
			autoplay: true,
			hover:true
		});
	}
	
	if ( document.id('contentLatest') ) {
			this.tabs1 = new MGFX.Tabs('.tab','.feature1',{
			autoplay: false,
			transitionDuration:500,
			slideInterval:3000,
			hover:false
		});
	}

});
