var Site = {
	start: function(){
		if($('file_')) Site.changeInput();	
		
	},
	
	changeInput: function(){
		var input = $('contactUs').getElement('input[type=file]');	
		var container = new Element('span',{
			'class':'input_file_wrapper'
		});
		var fake_text = new Element('input',{
			'type':'text',
			'class':'fake-text',
			'value':' '
		});
		input.set({
			styles:{
				'opacity':0.0001,
				'width':317,
				'height':19,
				'margin':0,
				'padding':0,
				'font-size':11,
				'border':'none',
				'z-index':10000
			}		  
		})
		container.injectBefore(input).adopt([input,fake_text]);
		input.addEvents({
			'change':function(){
				fake_text.set({
					'value':input.value
				});
			}			
		})		
	}
}



window.addEvent('domready', function() {
									 
	new Asset.images(['/fileadmin/templates/images/design/browse_button.jpg', '/fileadmin/templates/images/design/browse_button_over.jpg']);							 

	// Tab Module 
	$$('dl.tabsModule').each(function (tabModule) {
		var tabs = $$('dl.tabsModule dt');
		var isFirstTab = true;
		var firstTab;		
		
		tabModule.getChildren().each(function (tab, i) {
			if (tab.getTag() == 'dt') {
				tab.addEvent('click', function (e) {
					var activeContent = false;		
					this.getParent().getChildren().each(function (child) {
						switch (child.getTag()) {
							case 'dt' :
								if (child == tab) {
									activeContent = true;
									child.getChildren('a').removeClass('inactive');
									child.getChildren('a').addClass('active');
								} else {
									child.getChildren('a').removeClass('active');
									child.getChildren('a').addClass('inactive');
								}
								break;
							case 'dd' :
								if (activeContent) {
									child.removeClass('hide');
									activeContent = false;
								} else {
									child.addClass('hide');
								}
								break;
						}
					});
				});
				if (isFirstTab) {
					firstTab   = tab;
					isFirstTab = false;
				}
			}
		});
		tabs.each( function(t, j) {
			var tabWidth = 0;
			if (j > 0) {
				var tabSize = tabs[j-1].getSize();
				tabWidth = tabSize.size.x;
			} 
			t.setStyle('left', j*tabWidth + 'px');
			t.addClass('tabsJS');
		});
		firstTab.fireEvent('click');
	});	
	$$('dl.tabsModule').addClass('showTab');


	// Add open in new window functionality
	document.getElements('a[rel=external]').each(function(a) {
		a.addEvent('click', function(e) {
			e = new Event(e);
			window.open(a.href);
			e.stop();
		})
	});

	// Rollover Background Color in Projects
	$$('.rolloverstate').each(function (a) {
		a.addEvent('mouseenter', function() {
				a.setStyle('background','#3d3d3d');							 
		});
		a.addEvent('mouseleave', function() {
				a.setStyle('background','transparent');							 
		});
	});

	// Make Submit Buttons Links
	$ES('input.submit').each(function (input) {
		var subLink = new Element('a');
		subLink.setProperty('href', '#');
		subLink.setText(input.value);
		subLink.addClass('sendit');
		subLink.addEvent('click', function (e) {
			e = new Event(e);
			// CML code - start. check if form validation is required - affects only forms
			if ($E('.validate')) {
				submitValidParentForm(this);
			} else {// CML code - end
				submitParentForm(this);
			}
			e.stop();
		});
		input.replaceWith(subLink);
	});

	// Add pipes at the navigation elements
	function addSeparatorsToUl(ulEl) {
		if (!ulEl)
			return;
		
		var lis = $(ulEl).getElements('li');
		$(ulEl).getElements('li').each(function (li, i) {
		
			if (i < lis.length -1) {
				li.addClass('sep');
				return;
			}
			
		});
	}
	
	$$('ul#mainMenu').each(function (ul) {addSeparatorsToUl(ul); });
	$$('ul#footerMenu').each(function (ul) {addSeparatorsToUl(ul); });

	//Add &raquo;
	$$('p.more a').each(function (a) {
		var aText = a.getText();
		a.setHTML(aText + " &raquo;");
	});
	
	$$('p.backToNews a').each(function (a) {
		var aText = a.getText();
		a.setHTML("&laquo; " + aText);
	});
	$$('li.prev a').each(function (a) {
		var aText = a.getText();
		a.setHTML("&laquo; " + aText);
	});
	$$('li.next a').each(function (a) {
		var aText = a.getText();
		a.setHTML(aText + " &raquo;");
	});

//Browse Button
if($('file_')){
	Site.start();			
	$('file_').addEvent('mouseover', function() {
		//alert('hi');
		$('file_').getParent().addClass('input_file_wrapper_over');
		$('file_').getParent().removeClass('input_file_wrapper');
		
	});
	$('file_').addEvent('mouseout', function() {
		$('file_').getParent().addClass('input_file_wrapper');
		$('file_').getParent().removeClass('input_file_wrapper_over');
		
	});
	$('file_').addEvent('mouseup', function() {
		document.getElementById( 'file_' ).click();	
	});
}

}); // End of DOM Load

// Forms validation
function submitParentForm(el) {
	var parent = el.getParent();
	while (parent.getTag() != 'form') {
		parent = parent.getParent();
	}
	parent.submit();
}

// CML Code - start same as submitParentForm just added validation before submit - affects only forms
function submitValidParentForm(el) {
	var parent = el.getParent();
	while (parent.getTag() != 'form') {
		parent = parent.getParent();
	}if(formValid.validate()){
		parent.submit();}
}

// CML Code - end
function formCallback(result, form) {
	window.status = "Please make sure you've entered everything correctly...";
}