document.observe('dom:loaded', function()
{

	$('bar').setStyle( {width: document.viewport.getWidth() + "px"});
	window.onresize = function()
	{
		$('bar').setStyle( {width: document.viewport.getWidth() + "px"});
	};

	/*if( $('page').getWidth() < document.viewport.getWidth() )
	{
		diff = document.viewport.getWidth() - $('page').getWidth();
		$('container').style.width = $('container').getWidth() + (diff/2 - 45) + 'px';
	}
	
	if( $('page').getHeight() < document.viewport.getHeight() )
	{
		diff = document.viewport.getHeight() - $('page').getHeight();
		$('container').style.height = $('container').getHeight() + (diff/2 - 10) + 'px';
	}*/
	
	update_favorites_counter();
	
	$$('.job').each( function(job) 
	{
		if( job.readAttribute('favorized') == 'true' )
		{
			add_unfavorize_event(job);
		}
		else
		{
			add_favorize_event(job);
		}
		
		add_close_event(job);
	});

	
	$$('.activator').each ( function(el)
	{
		el.observe('click', function(event)
		{
			el.next('.selector').show();
			$(el.next('.selector')).observe('mouseout', function(event)
			{
				//We could probably replace the following with Event.element(event), but oh well.
				 var target = $(el.next('.selector'));
				 var mouse_over_element;  //What the mouse is currently over...
				 //So let's check to see what the mouse is now over, and assign it to mouse_over_element...
				 if( event.toElement ) {
				    mouse_over_element = event.toElement;
				 }
				 else if(event.relatedTarget) {
				   mouse_over_element = event.relatedTarget;
				 }
				 //In the event that the mouse is over something outside the DOM (like an alert window)...
				 if(mouse_over_element == null) {
				    return;
				 }
				 //Now we just make sure that what the mouse is currently over is NOT a descendant of
				 //the dropdown, and that the target is not the current mouse_over_element (I can't
				 //remember which case this covers, but it's important)
				 if(!mouse_over_element.descendantOf(target) && target != mouse_over_element) {
				    target.hide();
				 }

			});
			
		}.bind(this));
	}.bind(this));
	
	
	
	if($('branch-selector')) $('branch-selector').select('.text')[0].update($('branch-selector').down('.active').title);
	if($('region-selector')) $('region-selector').select('.text')[0].update($('region-selector').down('.active').title);
	if($('begin-selector')) $('begin-selector').select('.text')[0].update($('begin-selector').down('.active').title);
	if($('vacancy-selector')) $('vacancy-selector').select('.text')[0].update($('vacancy-selector').down('.active').title);
	
	
	if($('search-field')) 
	{
		$('search-field').observe('click', function(event)
		{
			Event.stopObserving($('search-field'), 'click');
			$('search-field').value = "";
		});
	}
});







function add_favorize_event(job)
{
	Event.stopObserving(job.down('.favorize-function'), 'click');
	job.down('.favorize-function').observe('click', function(event)
	{
			favorize(job);
			event.stop(); // to prevent ie from jumping to the main page
	}.bind(this));
}

function add_unfavorize_event(job)
{
	Event.stopObserving(job.down('.favorize-function'), 'click');
	job.down('.favorize-function').observe('click', function(event)
	{
			unfavorize(job);
			event.stop(); // to prevent ie from jumping to the main page
	}.bind(this));
}


function add_close_event(job)
{
	job.down('.close-function').observe('click', function(event)
	{
		Effect.toggle(job);
	}.bind(this));
}


function favorize(job)
{
	createCookie('favorized['+job.readAttribute('vacancy_number')+']', true);
	job.writeAttribute('favorized', true);
	job.down('.favorize-function').addClassName('true');
	
	createCookie('favorized_counter', parseInt(readCookie('favorized_counter') || 0) + 1);
	update_favorites_counter();
	
	add_unfavorize_event(job);
}


function unfavorize(job)
{
	eraseCookie('favorized['+job.readAttribute('vacancy_number')+']')
	job.writeAttribute('favorized', false);
	job.down('.favorize-function').removeClassName('true');
	
	var count = readCookie('favorized_counter');
	if( count != null && count != 0 ) createCookie('favorized_counter', parseInt(readCookie('favorized_counter')) - 1);
	update_favorites_counter();
	
	add_favorize_event(job);
}


function update_favorites_counter()
{
	// for every guy who has to change something here.. try removing "var" and IE will not do any js. yes. really.
	var count = parseInt(readCookie('favorized_counter') || 0);

	if($('favorites-selector')){
		if( count == 0 )
		{
			$('favorites-selector').down('.text').innerHTML = 'Keine Favoriten';
		}
	
		else if( count == 1 )
		{
			$('favorites-selector').down('.text').innerHTML = '1 Favorit';
		}
	
		else
		{
			$('favorites-selector').down('.text').innerHTML = count + ' Favoriten';
		}
	}
}



/** thanks to quirksmode.org **/
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}


