function init()
{
}

function load_map()
{
	if (GBrowserIsCompatible()) {
		map = new GMap2($('google_map'));
		map.setCenter(new GLatLng(29.94839, -90.086517), 11);
		map.addControl(new GLargeMapControl ());
        map.addControl(new GMapTypeControl());
		geocoder = new GClientGeocoder();
	}
	return false;
}

function show_markers(tab_id)
{
	var pars = 'cmd=get_markers';
	pars += '&id=' + encodeURIComponent(tab_id);

	var myAjax = new Ajax.Request(
			'actions/report.action.php',
			{
				method: 'get',
				parameters: pars,
				onComplete: showResponse
			}
		);

	function showResponse(originalRequest)
	{
		var result = originalRequest.responseText.parseQuery();
		for (i = 0; i < result['count']; i++)
			add_marker(result['location_' + i], result['content_' + i]);
	}
	return false;
}

function add_marker(location, content)
{
	geocoder.getLatLng(
		location,
		function(point)
		{
			if (point) {
				var marker = new GMarker(point);
				GEvent.addListener(
					marker,
					'click',
					function()
					{
						var html = content;
						marker.openInfoWindowHtml(html);
					}
				);
				map.addOverlay(marker);
			}
		}
	);
}

function switch_tab(tab_id)
{
	if (tab_id == 1) {
		$('tab_1').style.backgroundImage = 'url(images/bg_tab_1_a.gif)';	
		$('tab_2').style.backgroundImage = 'url(images/bg_tab_2.gif)';	
		$('tab_3').style.backgroundImage = 'url(images/bg_tab_3.gif)';
	} else if (tab_id == 2) {
		$('tab_1').style.backgroundImage = 'url(images/bg_tab_1.gif)';	
		$('tab_2').style.backgroundImage = 'url(images/bg_tab_2_a.gif)';	
		$('tab_3').style.backgroundImage = 'url(images/bg_tab_3.gif)';	
	} else if (tab_id == 3) {
		$('tab_1').style.backgroundImage = 'url(images/bg_tab_1.gif)';	
		$('tab_2').style.backgroundImage = 'url(images/bg_tab_2.gif)';	
		$('tab_3').style.backgroundImage = 'url(images/bg_tab_3_a.gif)';	
	}
	$('content_top').style.backgroundImage = 'url(images/bg_content_top_' + tab_id + '.gif)';
	$('content_main').style.backgroundImage = 'url(images/bg_content_main_' + tab_id + '.gif)';
	$('content_bottom').style.backgroundImage = 'url(images/bg_content_bottom_' + tab_id + '.gif)';
	load_map();
	show_markers(tab_id);
	return false;
}

function menu_over(element)
{
	element.style.backgroundImage = 'url(images/bg_button.gif)';	
	element.childNodes[0].style.color = '#ffffff';	
}

function menu_out(element)
{
	element.style.backgroundImage = '';	
	element.childNodes[0].style.color = '#787878';	
}

function validate_input(element, rules, message) {
	var result = true;
	switch (rules) {
		case 'select' :
			result = $F(element) != '';
			break;
		case 'checkbox' :
			result = $(element).checked;
			break;
		case 'email' :
			result = ($F(element).indexOf(".") > 2) && ($F(element).indexOf("@") > 0);
			break;
		case 'email_list' :
			result = $F(element).length > 0;
			email_array = $F(element).split(',');
			for (i = 0; i < email_array.length; i++)
				email = email_array[i];
				result = result && (email.indexOf(".") > 2) && (email.indexOf("@") > 0);
			break;
		case 'password' :
			result = ($F(element).length >= 8);
			break;
		case 'required' :
			result = $F(element) != '';
			break;
		case 'empty_password' :
			result = ($F(element).length >= 8) || $F(element) == '';
			break;
		case 'password_confirm' :
			result = ($F('password') == $F(element));
			break;
	}
	if (!result) {
		$('error_' + element).update(message);
		result = 0;
	} else {
		$('error_' + element).update('');
		result = 1;
	}
	return result;	
}

// report
function show_report_window()
{
	Dialog.info(
		{
			url: 'popover_report.php',
			options: {method: 'get'}
		}, 
		{
			className: 'alphacube',
			width: 500,
			closable: true,
			title: '',
			onClose: function()
			{
				refresh_account_window();	
			}
		}
	);
	return false;
}

function show_report_edit_window(report_id)
{
	Dialog.info(
		{
			url: 'popover_report_edit.php',
			options: {method: 'get', parameters: 'id=' + encodeURIComponent(report_id)}
		}, 
		{
			className: 'alphacube',
			width: 500,
			closable: true,
			title: '',
			onClose: function()
			{
				refresh_account_window();	
			}
		}
	);
	return false;
}

function submit_report()
{
	var no_error = 1;

	no_error = validate_input('description', 'required', 'Description is required') && no_error;
	no_error = validate_input('address', 'required', 'Address is required') && no_error;
	no_error = validate_input('city', 'required', 'City is required') && no_error;
	no_error = validate_input('state', 'select', 'Select state') && no_error;
	no_error = validate_input('zip', 'required', 'Zip code is required') && no_error;
	no_error = validate_input('category', 'select', 'Select category') && no_error;
	if ($('email'))
		no_error = validate_input('email', 'email', 'Valid email address is required') && no_error;
	if ($('password'))
		no_error = validate_input('password', 'password', 'Password is required and should be 8 chars or longer') && no_error;
	if ($('password_confirm'))
		no_error = validate_input('password_confirm', 'password_confirm', 'Please confirm password') && no_error;

	Windows.focusedWindow.updateHeight();
	Windows.focusedWindow.showCenter();

	var result = no_error == 1
	if (result)
		show_loader();
	return result;
}

function remove_report(report_id)
{
	show_loader();
	pars = 'cmd=remove';
	pars += '&id=' + encodeURIComponent(report_id);
	var myAjax = new Ajax.Request(
			'actions/report.action.php',
			{
				method: 'get',
				parameters: pars,
				onComplete: showResponse
			}
		);

	function showResponse(originalRequest)
	{
		hide_loader();
		refresh_account_window();
	}
}

// login
function show_login_window()
{
	Dialog.info(
		{
			url: 'popover_login.php',
			options: {method: 'get'}
		}, 
		{
			className: 'alphacube',
			width: 500,
			closable: true,
			title: ''
		}
	);
	return false;
}

function submit_login()
{
	var no_error = 1;

	no_error = validate_input('email', 'email', 'Please enter correct email') && no_error;
	no_error = validate_input('password', 'required', 'Please enter password') && no_error;

	Windows.focusedWindow.updateHeight();
	Windows.focusedWindow.showCenter();

	if (no_error == 1) {

		show_loader();

		pars = 'cmd=login';
		pars += '&email=' + encodeURIComponent($F('email'));
		pars += '&password=' + encodeURIComponent($F('password'));
		var myAjax = new Ajax.Request(
				'actions/user.action.php',
				{
					method: 'get',
					parameters: pars,
					onComplete: showResponse
				}
			);
	}

	function showResponse(originalRequest)
	{
		hide_loader();
		if (originalRequest.responseText == '[OK]') {
			$('account_link').show();
			$('logout_link').show();
			$('login_link').hide();
			Windows.focusedWindow.close();
			show_account_window();
		} else {
			$('error_password').update('Login error.');
			Windows.focusedWindow.updateHeight();
			Windows.focusedWindow.showCenter();
		}
	}

	return false;
}

function submit_logout()
{
	show_loader();
	pars = 'cmd=logout';
	var myAjax = new Ajax.Request(
			'actions/user.action.php',
			{
				method: 'get',
				parameters: pars,
				onComplete: showResponse
			}
		);

	function showResponse(originalRequest)
	{
		hide_loader();
		$('account_link').hide();
		$('logout_link').hide();
		$('login_link').show();
	}

	return false;
}


// contacts
function show_contact_window()
{
	Dialog.info(
		{
			url: 'popover_contact.php',
			options: {method: 'get'}
		}, 
		{
			className: 'alphacube',
			width: 500,
			closable: true,
			title: ''
		}
	);
	return false;
}

function submit_contact()
{
	var no_error = 1;

	no_error = validate_input('name', 'required', 'Name is required') && no_error;
	no_error = validate_input('email', 'email', 'Please enter correct email') && no_error;
	no_error = validate_input('question', 'required', 'Please enter question') && no_error;

	Windows.focusedWindow.updateHeight();
	Windows.focusedWindow.showCenter();

	if (no_error == 1) {
		show_loader();
		pars = 'cmd=send';
		pars += '&name=' + encodeURIComponent($F('name'));
		pars += '&email=' + encodeURIComponent($F('email'));
		pars += '&question=' + encodeURIComponent($F('question'));
		var myAjax = new Ajax.Request(
				'actions/contact.action.php',
				{
					method: 'get',
					parameters: pars,
					onComplete: showResponse
				}
			);
	}

	function showResponse(originalRequest)
	{
		hide_loader();
		$('contact_block').update('Thank you for contacting us. We will reply shortly.');
	}

	return false;
}

// account
function show_account_window(page)
{
	if (!page)
		page = 0;
	var pars = 'page=' + page;
	Dialog.info(
		{
			url: 'popover_account.php',
			options:
			{
				method: 'get',
				parameters: pars
			}
		}, 
		{
			className: 'alphacube',
			width: 1050,
			closable: true,
			title: ''
		}
	);
	return false;
}

function refresh_account_window()
{
	if ($('account_block')) {
		show_loader();
		var pars = '';
		var myAjax = new Ajax.Request(
				'popover_account.php',
				{
					method: 'get',
					parameters: pars,
					onComplete: showResponse
				}
			);
	}

	function showResponse(originalRequest)
	{
		hide_loader();
		$('account_block').update(originalRequest.responseText);
		Windows.focusedWindow.updateHeight();
		Windows.focusedWindow.showCenter();
	}
	
	return false;
}

// forgot
function show_forgot_window()
{
	Dialog.info(
		{
			url: 'popover_forgot.php',
			options: {method: 'get'}
		}, 
		{
			className: 'alphacube',
			width: 500,
			closable: true,
			title: ''
		}
	);
	return false;
}

function submit_forgot()
{
	var no_error = 1;

	no_error = validate_input('forgot_email', 'email', 'Please enter your email') && no_error;

	Windows.focusedWindow.updateHeight();
	Windows.focusedWindow.showCenter();

	if (no_error == 1) {

		show_loader();

		pars = 'cmd=restore';
		pars += '&forgot_email=' + encodeURIComponent($F('forgot_email'));
		var myAjax = new Ajax.Request(
				'actions/user.action.php',
				{
					method: 'get',
					parameters: pars,
					onComplete: showResponse
				}
			);
	}

	function showResponse(originalRequest)
	{
		hide_loader();
		if (originalRequest.responseText == '[OK]') {
			$('forgot_block').update('New password have been sent to your email.');
		} else {
			$('error_forgot_email').update('Cant find account with such email.');
		}
		Windows.focusedWindow.updateHeight();
		Windows.focusedWindow.showCenter();
	}

	return false;
}


// view
function show_report_view_window(report_id)
{
	Dialog.info(
		{
			url: 'popover_report_view.php',
			options: {method: 'get', parameters: 'id=' + encodeURIComponent(report_id)}
		}, 
		{
			className: 'alphacube',
			width: 600,
			closable: true,
			title: ''
		}
	);
}

// message
function show_message(message)
{
	Dialog.info(
		'<div class="popover_content">' + message + '</div>', 
		{
			className: 'alphacube',
			width: 400,
			closable: true,
			title: ''
		}
	);
}


// loader
function show_loader()
{	
	Dialog.info(
		'<div style="padding:30px 100px"><img src="images/loadinfo.net.gif" border="0" /> Loading...</div>', 
		{
			id: 'loader',
			className: 'alphacube',
			width: 300,
			destroyOnClose: true
		}
	);
}

function hide_loader()
{
	Windows.close('loader');
}
