// Initialize main configuration.
var config = {
	'siteRoot'					: '/',
	'siteRootAbsolute'	: 'http://www.ardc-israel.org/'
}
	
$().ready(function(){
	initFuncs();
	if ($('#hp_newsletter_form').length)
	{
		attach_hp_newsletter();
	}
	if ($('#block_testimonials').length)
	{
		attach_block_pager($('#block_testimonials'));
	}
	$('a.mailtoaddr').bind('click', mailtoHrefContact)
})

var initFuncs = function(){
	// bind language selection
	var languageSelect = $('#language SELECT');
	languageSelect.bind('change', function(){
		var currentLang = $('HTML').attr('lang');
		if (this.value == currentLang || this.value == '#')
			return false;
		
		var new_loc = 'http://'+window.location.host+'/'+this.value+'/'+window.location.pathname.replace(/^\/(([a-z]{2})(\/)?)?/,'');
		window.location = new_loc;
	});
	
	// Bind header menu functionality
	var headermenuMain = $('#menu'), headermenuSub = $('#submenu');
	headermenuMain.find('UL LI').each(function(i){
		
		$(this).bind('mouseover', function(){
			var relatt = this.getAttribute('rel'), li_offsetLeft = parseInt(this.offsetLeft), selected = '';
			headermenuSub.children('UL').each(function(i){
				if (this.getAttribute('rel') == relatt)
				{
					selected = relatt;
				}
			})
//			if (selected != '')
//			{
				headermenuSub.children('UL').each(function(i){
					if (this.getAttribute('rel') == selected)
					{
						$(this).css('display','inline-block');
					}
					else
					{
						$(this).hide();
					}
				})
//			}
		})
	});
};



function attach_block_pager(block_elem)
{
	var controls_block = block_elem.find('.project-controls');
	var item_type = controls_block.get(0).getAttribute('rel');
	var current_item = block_elem.children('#'+item_type+'_0');
	var current = 0;
	controls_block.children().each(function(){
		$(this).bind('click', function(){
			tmp_last_current = current;
			switch (this.className)
			{
				case 'prev':
					if (current == 0)
						current = block_elem.children('.'+item_type+'_item').length-1;
					else
						current = current-1;
					break;
				case 'next':
					if (current == block_elem.children('.'+item_type+'_item').length-1)
						current = 0;
					else
						current = current+1;
			}
			if (current != tmp_last_current)
			{
				current_item.hide();
				current_item = block_elem.children('#'+item_type+'_'+current);
				current_item.show();
				controls_block.find('.current_item').text(''+(current+1));
				this.blur();
			}
			return false;
		})
	})
}


function attach_hp_newsletter()
{
	$('#hp_newsletter_form #hp_newsletter_form_email').bind('focus', function(){
		if (this.value.match(/\s+/))
			this.value = '';
	})
}


function mailtoHrefContact()
{
	// TODO
}
