var bodyHeight = 0;
var bodyIndex = 250;
var currentlyOver = false;

window.addEvent('load', function(){
	if ($E('#wrapper'))
	{
		var bodyIndex = 250;
		var bodyHeight = $E('#wrapper').getCoordinates().height - bodyIndex;
		
		$ES('#content .left .spotlight ul li').addEvent('mouseenter', function(){
		    if ($E('div ul', this))
		    {
				bh = getBodyHeight();
		        
		        coords = $E('div ul', this).getCoordinates();
		        childHeight = coords.height + coords.top;

		        if (childHeight > bh)
		        {
		            currentlyOver = true;
		            $('wrapper').setStyle('height', childHeight + $E('#footer').getCoordinates().height);
		        }
		        else
		        {
		            currentlyOver = false;
		            $('wrapper').removeProperty('style');
		        }
		    }	    
		});
		
		$ES('#content .left .spotlight ul li').addEvent('mouseleave', function(){
		    if ($E('div ul', this))
		    {
				currentlyOver = false;
		    }	    
		});
		
		if ((navigator.appName == "Microsoft Internet Explorer") && (parseInt(navigator.appVersion) < 7))
		{
			(function(){bodyIndex = 300; bodyHeight = $E('#wrapper').getCoordinates().height - bodyIndex;}).delay(1000);
		}
		
		setBodyHeight();
	}
});

function getBodyHeight()
{
	left = $E('#content .left').getCoordinates();
	right = $E('#content .right').getCoordinates();
	
	return Math.max(left.height + left.top, right.height + right.top);
}

function setBodyHeight()
{
	if (!currentlyOver && (getBodyHeight() < $('wrapper').getCoordinates().height || getBodyHeight() > $('wrapper').getCoordinates().height))
	{
		$('wrapper').removeProperty('style');
	}
	setTimeout("setBodyHeight();", 500);
}

function prepareRoundedBoxes()
{
	/*$ES('.menu li div', $('header')).each(
		function (element) 
		{ 
			$(element).appendChild(new Element('b', {'class': 'cbl'}));
			$(element).appendChild(new Element('b', {'class': 'b'})); 
			$(element).appendChild(new Element('b', {'class': 'cbr'}));
		}
	);*/
	
	$ES('.right .rating', $('content')).each(
		function (element) 
		{ 
			$(element).appendChild(new Element('b', {'class': 'cbl'}));
			$(element).appendChild(new Element('b', {'class': 'ctl'})); 
			$(element).appendChild(new Element('b', {'class': 'cbr'}));
			$(element).appendChild(new Element('b', {'class': 'ctr'}));
		}
	);
}

function prepareSmallFormLabels()
{
	$ES('input', $('header')).each(
		function (element)
		{
			if (label = $E('label[for=' + element.id + ']'))
			{
				$(element).addEvent('focus', 
					function ()
					{
						$E('label[for=' + this.id + '] span').setStyle('display', 'none');
					}
				);
				$(element).addEvent('blur', 
					function ()
					{
						if (this.value.length <= 0)
						{
							$E('label[for=' + this.id + '] span').setStyle('display', 'block');
						}
					}
				);
				
				if (element.value.trim().length > 0)
				{
					$E('label[for=' + element.id + '] span').setStyle('display', 'none');
				}
			}
		}
	);
}

function prepareLanguageChoice()
{
	if ($('language_field'))
	{
		$('language_field').addEvent('change',
			function ()
			{
				this.form.submit();
			}
		);
	}
}

function prepareLoginForm()
{
	if ($('login_form'))
	{
		$E('h2', $('login_form')).setStyle('cursor', 'pointer').addEvent('click',
			function ()
			{
				form = $E('div', $('login_form'));
				
				if (form.getStyle('display') == 'none')
				{
					form.setStyle('display', 'block');
				}
				else
				{
					form.setStyle('display', 'none');
				}
			}
		);
	}
}

function prepareZebraTables()
{
	$ES('#content .right table').each(
		function (table)
		{
			$ES('tbody tr', $(table)).each(
				function (row, index)
				{
					if (index % 2 != 0) row.addClass('even');
				}
			);
		}
	);
}

function prepareSectionHandles(firstOpen)
{
	firstOpen = typeof(firstOpen) != 'undefined' ? firstOpen : true;
	var entityType = ($E('#content .right').hasClass('entertainment')) ? 'entertainment' : 'education';
	
	searchexp = new RegExp('search_term', 'i');
	
	if (window.location.href.match(searchexp)) return;
	
	$ES('#content .right div.section').each(
		function (section)
		{
			div = $E('div', section);
			if ($defined(div) && !div.hasClass('text'))
			{	
				heading = $E('h2', section);
				if ($defined(heading))
				{
					handle = $E('a', heading);
					heading.setStyle('cursor', 'pointer');
					//section.setStyle('padding-bottom', '5px');
					regexp = new RegExp(section.id + '[a-z0-9]+', 'i');

					firstCondition = (firstOpen == true) ? (Cookie.get(entityType + heading.getParent().id) == 'open') : false;
					thirdCondition = (firstOpen == true) ? ($ES('#content .right div.section').length == 1) : false;

					if (firstCondition || (section.id.length > 0 && window.location.href.match(regexp)) || thirdCondition)
					{
						handle.setProperty('class', 'handle-minimize');
						div.setStyle('display', 'block');
					}
					else
					{
						div.setStyle('display', 'none');
					}
					
					heading.addEvent('click', 
						function ()
						{
							if ($E('a', this).getProperty('class') == 'handle-maximize')
							{
								if (firstOpen == true)
								{
									Cookie.set(entityType + this.getParent().id, 'open');
								}
								$E('a', this).setProperty('class', 'handle-minimize');
								$E('div', this.getParent()).setStyle('display', 'block');
							}
							else
							{
								if (firstOpen == true)
								{
									Cookie.set(entityType + this.getParent().id, 'closed');
								}
								$E('a', this).setProperty('class', 'handle-maximize');
								$E('div', this.getParent()).setStyle('display', 'none');
							}
						}	
					);
				}
			}
		}
	);
	
	$ES('#content .entertainment div.section').each(
		function (section)
		{
			div = $E('div', section);
			
			if (!$defined(div))
			{
				$E('h2', section).addClass('no_hierarchy');
			}
		}
	);
}

function prepareGalleryPhotos()
{
	$ES('#content ul.gallery li a').each(
		function (anchor)
		{
			anchor.addEvent('click', function() {window.open(this.location, 'gallery', 'width=792,height=600,location=no,scrollbars=yes,resizable=no');});
			anchor.location = anchor.href;
			anchor.href = '#';
		}
	);
}

function showPopup(href, name)
{
	window.open(href, name, 'width=792,height=600,location=no,scrollbars=yes,resizable=no');
}

function checkAll(id, check)
{
	$ES('input[type=checkbox]', $(id)).each(
		function (input, index)
		{
			if (check)
			{
				input.checked = 'checked';
			}
			else
			{
				input.checked = null;
			}
			input.fireEvent('click');
		}
	);
}

function prepareAdvertisementTable()
{
	if ($('advertisementTypeSelect'))
	{
		$('advertisementTypeSelect').addEvent('change', function() { this.form.submit(); });
	}
	
	if ($('checkAllLink'))
	{
		$('checkAllLink').checked = false;
		$('checkAllLink').addEvent('click', function() { this.checked = (this.checked === true) ? false : true; checkAll('advertisementTable', this.checked); });
	}
	
	$ES('input[type=checkbox]', $E('.right .section table tbody', $('content'))).each(
		function (input, index)
		{
			input.addEvent('click',
				function ()
				{
					var JsonRequest = new Json.Remote(config[0] + "/advertisement/_check");
					JsonRequest.send({'category':this.name, 'index': this.value, 'state': this.checked});
				}
			);
		}
	);
	
	$ES('tbody a', $('advertisementTable')).each(
		function (anchor)
		{
			if (anchor.hasClass('edit') || anchor.hasClass('delete') || anchor.hasClass('priority') || anchor.hasClass('emphasize') || anchor.getProperty('target') == '_self')
			{
				return;
			}
			anchor.location = anchor.href;
			anchor.href = '#';
			anchor.addEvent('click', 
				function ()
				{
					window.open(this.location, 'advertisementWindow', 'width=792,height=600,location=no,scrollbars=yes,resizable=no');
				}
			);
		}
	);
}

function prepareEmploymentTable()
{
	$('employmentTypeSelect').addEvent('change', function() { this.form.submit(); });
	
	$('checkAllLink').checked = false;
	$('checkAllLink').addEvent('click', function() { this.checked = (this.checked === true) ? false : true; checkAll('employmentTable', this.checked); });
	var JsonRequest = new Json.Remote(config[0] + "/employment/_check", {async: false});
	
	$ES('input[type=checkbox]', $E('.right .section table tbody', $('content'))).each(
		function (input, index)
		{
			input.addEvent('click',
				function ()
				{
					JsonRequest.send({'category':this.name, 'index': this.value, 'state': this.checked});
				}
			);
		}
	);
	
	$ES('tbody a', $('employmentTable')).each(
		function (anchor)
		{
			anchor.location = anchor.href;
			anchor.href = '#';
			anchor.addEvent('click', 
				function ()
				{
					window.open(this.location, 'advertisementWindow', 'width=792,height=600,location=no,scrollbars=yes,resizable=no');
				}
			);
		}
	);
}

function prepareProfessionMeetingType()
{
	$E('select', $('professionMeetingType')).addEvent('change', function() { this.form.submit(); });
}

function prepareAdvertisementForm()
{
	$('preview_btn').addEvent('click', function () { $('act').setAttribute('value', 'preview'); });
	$('submit_btn').addEvent('click', function () { $('act').setAttribute('value', 'submit'); });
	prepareTextAreas();
}


function prepareEmploymentForm()
{
	$('preview_btn').addEvent('click', function () { $('act').setAttribute('value', 'preview'); });
	$('submit_btn').addEvent('click', function () { $('act').setAttribute('value', 'submit'); });
	prepareTextAreas();
}

function prepareRegistrationSwitch()
{
	$('usertype_2').addEvent('click', 
		function ()
		{
			$ES('.private').each(
				function (element)
				{
					element.addClass('hidden');
				}
			);
			$ES('.juridic').each(
				function (element)
				{
					element.removeClass('hidden');
				}
			);
		}
	);
	
	$('usertype_1').addEvent('click', 
		function ()
		{
			$ES('.private').each(
				function (element)
				{
					element.removeClass('hidden');
				}
			);
			$ES('.juridic').each(
				function (element)
				{
					element.addClass('hidden');
				}
			);
		}
	);	
	
	if ($('usertype_1').checked)
	{
		$('usertype_1').fireEvent('click');
	}
	else if ($('usertype_2').checked)
	{
		$('usertype_2').fireEvent('click');
	}
	else
	{
		$('usertype_1').fireEvent('click');
	}
}

var length = 255;

function prepareTextAreas()
{
	$ES('input[type="text"]').filterByAttribute('id','!=','phonenumber').filterByAttribute('id','!=','email').each(
		function (element)
		{
			counter = new Element('span', {'class': 'counter'}).appendText(length - element.value.length);
			element.getParent().adopt(counter);
				
			element.counter = counter;
				
			element.addEvent('keyup', checkLength);
			element.addEvent('mouseup', checkLength);
			element.addEvent('blur', checkLength);
		}
	);
}
			
function checkLength()
{
	remaining = length - this.value.length;
				
	this.counter.setText('').appendText(remaining);
							
	if (remaining < 0)
	{
		this.counter.setText('').appendText(0);
		this.value = this.value.substr(0, length);
		alert(config[1]);
	}
}

function prepareMenuHovers()
{
	
}