function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
	window.onload = func;
	} else {
	window.onload = function() {
	oldonload();
	func();
		}
	}
}

function disable_text_select() {
	document.onselectstart = function() {return false;}
}


  function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
 }	
	
 function externalLinks() {
 if (!document.getElementsByTagName) return;
 var anchors = document.getElementsByTagName("a");
 for (var i=0; i<anchors.length; i++) {
   var anchor = anchors[i];
   if (anchor.getAttribute("href") &&
       anchor.getAttribute("rel") == "external")
     anchor.target = "_blank";
 }
}		

function contact() {
	if($('sendMessage')) {
		$('sendMessage').addEvent('click', function(e) {
			var clickEvent = new Event(e);
			clickEvent.stop();
			$('form').setProperty('action', 'modules/contact/');
			$('form').send({ 
				update: $('content'), 
				onSuccess: function () {
					externalLinks();
					contact();
					meniu();
					disable_text_select();
				}
			});
		});
	}
}

function meniu ()
{
  var main_menu_spans = $$('#main_menu span');
  var main_menu_uls = $$('#main_menu ul');
  var main_menu_lks = $$('#main_menu a');
  var main_menu_togglers = main_menu_spans.concat(main_menu_lks);
  var submenus = $$('#navigation .submeniu');
  var navigation =  $('navigation');
  var content = $('content');
 
  
  
  for(i=0; i<submenus.length; i++)
  {
	  submenus[i].parentNode.removeChild(submenus[i]);
	  navigation.appendChild(submenus[i]);
  }
  
  for(i=0; i<main_menu_togglers.length; i++)
  { 
	  main_menu_togglers[i].addEvent('mouseover', function () {this.addClass('hovered')}, false)
	  main_menu_togglers[i].addEvent('mouseout', function () {this.removeClass('hovered')}, false)
	  //main_menu_togglers[i].onmouseover = function () {this.addClass('hovered');}
	  //main_menu_togglers[i].onmouseout = function () {this.removeClass('hovered');}
     
  }
  for(i=0; i<main_menu_spans.length; i++)
  {
	  
	  main_menu_spans[i].onclick = function () 
	  { 
	   var selectate = $$('#navigation .selectat');
		for (y=0; y<selectate.length; y++)
		{
			selectate[y].removeClass('selectat');
		}
	   this.addClass('selectat');
		for(x=0; x<submenus.length; x++)
		{  submenus[x].removeClass('on');
			submenus[x].addClass('off');
			submenus[x].style.left = '-595px';
		}
		 
		this_inner = this.innerHTML;
		this_inner = this_inner.toLowerCase();
		submeniu = document.getElementById(this_inner);
		submeniu.removeClass('off');
		submeniu.addClass('on');
		var submeniu_motion = new Fx.Style(submeniu, 'left', {duration:500 });
		submeniu_motion.custom(-595, 215);
		content.innerHTML = '';
	  }
	 
 }
  
  for(i=0; i<main_menu_lks.length; i++)
  {
	  main_menu_lks[i].onclick = function () 
	  { 
	   
		if(this.getAttribute('id')=='contact_link'||this.getAttribute('id')=='consultanta_link') {
			 
			var selectate = $$('#navigation .selectat');
			for (y=0; y<selectate.length; y++)
			{
				selectate[y].removeClass('selectat');
			}
			this.addClass('selectat');
			
			for(x=0; x<submenus.length; x++)
					{  submenus[x].removeClass('on');
						submenus[x].addClass('off');
						submenus[x].style.left = '-595px';
					}
			
			
			if(this.getAttribute('id')=='contact_link') {
			
					
					 
					
					this_inner = this.innerHTML;
					this_inner = this_inner.toLowerCase();
					submeniu = document.getElementById(this_inner);
					submeniu.removeClass('off');
					submeniu.addClass('on');
					var submeniu_motion = new Fx.Style(submeniu, 'left', {duration:500 });
					submeniu_motion.custom(-595, 215);
			}
		}
		
		else {
			var this_ul = this.parentNode.parentNode;
			var selectate = getElementsByClass('selectat',this_ul,'a');
			for (y=0; y<selectate.length; y++)
			{
				selectate[y].removeClass('selectat');
			}
			this.addClass('selectat');
		}
/**
 *  later edit
 *  ajax request & loading
 */		if(this.getProperty('rel') == 'external')
 			return;

		new Ajax( 'modules/' + this.getProperty('href'), {
			update: $('content'),
			onRequest: function() {
				$('loader').removeClass('off');	
			},
			onSuccess: function() {
				$('loader').addClass('off');
				
				if($('proiect_foto')&&!window.opera) {
					$('proiect_foto').setStyle('visibility', 'hidden');
					$('proiect_foto').addEvent('load', function() {this.style.visibility = 'visible';});
				} else if(!$('proiect_foto')) contact();
				init_proiecte();
				externalLinks();
			}
		}).request();

		return false;
	  }
	  
  }
  
  
}
 
 
function galerie_foto () {
 var proiect_foto = document.getElementById('proiect_foto');
 if(!proiect_foto) {return;}
 var proiect_fotos =  document.getElementById('proiect_fotos');
 if(proiect_fotos) {
	 var proiect_fotos_links = proiect_fotos.getElementsByTagName('a');
	 if(proiect_fotos_links) {
		 proiect_fotos_links[0].className = 'thumb_on'; 
		 for(i=0; i<proiect_fotos_links.length; i++) {
			proiect_fotos_links[i].onclick = function () { 
				for(x=0; x<proiect_fotos_links.length; x++)
					{
					  proiect_fotos_links[x].className = ''; 	
					 }
					if(this.getAttribute('id') != 'proiect_pdf')
						{
							if(window.opera) {proiect_foto.src = this.getAttribute('href');}
							else {
							proiect_foto.style.visibility = 'hidden';
							proiect_foto.src = this.getAttribute('href');
							proiect_foto.onload = function () {proiect_foto.style.visibility = 'visible';}
							}
							
						}
					this.className = 'thumb_on';
					if(this.getAttribute('id') == 'proiect_pdf')
						{
							return;
						}
						else
						{
							return false;
						}
				   }
			  }
		 }
 }
}



	 

function info_scoll () {
	      var continut = document.getElementById('continut');
			var scroll_box =  document.getElementById('scroll_box');
			if(!scroll_box) {return false;}
			var scroll_up = document.createElement('a');
		  scroll_up.className = 'scroll_up';
		  scroll_up.setAttribute('id','scroll_up');
		  scroll_up.setAttribute('href','#');
		  
		  
		  var scroll_down = document.createElement('a');
		  scroll_down.className = 'scroll_down';
		  scroll_down.setAttribute('id','scroll_down');
		  scroll_down.setAttribute('href','#');
		  
		  
		  continut.insertBefore(scroll_up,scroll_box);
		  continut.insertBefore(scroll_down,scroll_box);
		  
		  var inner_div = scroll_box.getElementsByTagName('div')[0];
		  if(inner_div.offsetHeight>320) {
			  scroll_up.style.visibility = 'visible';
			}
		  
		  div_scroll = new TextScroll('div_scroll', 'scroll_box');
		  
		  continut.style.visibility = 'visible';
		
		
 
}



	 
	
 function drag_over ()
  {
	var drag_div = document.getElementById('drag');
	if(!drag_div) {return;}
	drag_div.onmouseover = function ()
	{
		this.className = 'verde';
	 }
	
	drag_div.onmouseout = function ()
	 {
		this.className = '';  
	  }
   }

function verify(id, what, path) {
	var enabled = document.createElement('input');
	enabled.setAttribute('type','hidden');
	enabled.setAttribute('name','enabled');
	enabled.setAttribute('value','1');
	document.getElementById('form').appendChild(enabled);

	if(enabled)
		{
			if(what=='email') {var filter=/^(\w+(?:\.\w+)*)@((?:\w+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i}
			if(what=='nume') {var filter=/^([a-zA-Z0-9._ -]{3,255})$/i}
			if(what=='mesaj') {var filter=/^([a-zA-Z0-9\~ !?\n@#$%^&*()_\-+\{\}\[\]():;"'<>,.`\/\\=]{4,255})$/i}
		
			eval('var row = document.getElementById(\'row_' + id + '\');');
					
			var field = document.getElementById(id);

			if(what == 'mesaj')
				{
					limit = 0;
				}
				else
				{
					limit = 1;
				}			
			if(row.getElementsByTagName('input').length <= limit)
				{
					var hidden = document.createElement('input');
					eval('hidden.setAttribute(\'name\',\'valid_' + id + '\');');
					eval('hidden.setAttribute(\'id\',\'valid_' + id + '\');');
					hidden.setAttribute('type','hidden');
					row.appendChild(hidden);
				}
				else
				{
					eval('var hidden = document.getElementById(\'valid_' + id + '\');');
				}
			
				if (filter.test(field.value)) {
					field.className = 'text';
					hidden.setAttribute('value','1');
				}
				else {
					field.className = 'error';
					hidden.setAttribute('value','0');
				}
				
				if(!field.value) {
					field.className = 'text';
					hidden.setAttribute('value','0');
				}
		}
} 

addLoadEvent(externalLinks);
// addLoadEvent(preloadImage);
addLoadEvent(meniu);
addLoadEvent(disable_text_select);


function textarea(asta)
  {
   maxWidth = 200;
   charWidth = 8;
   charHeight = 15;
   
  // textu = 'unu asfs asdg asd gas g <asta e un stringgg de teeeeesst care nu trebuie numarat!!>';
 //  textu = textu.replace(/^(<)+([a-zA-Z0-9\~ !?\n@#$%^&*()_\-+\{\}\[\]():;"'<>,.`\/\\=])*(>)+/gi, '');
//   alert(textu);
   iframe = document.getElementById(asta);
   window.document.getElementsByTagName('html');
  // alert(bodys.length);
	//alert(iframe.style.height);
   var content = iframe.innerHTML;
  // alert(content);
   rand = Math.round(content.length / 21);   
//   iframe.style.height = rand * charHeight + "px";
  }

function init_proiecte() {
galerie_foto();
info_scoll();
drag_over();
//externalLinks();
}




