function OsoboFeedback()
{
	this.visible = false;

	this.show = function ()
	{
		if (this.visible)
			return;
		else
		{
			$('form#feedbackForm input').attr('value', '');
			$('form#feedbackForm textarea')[0].value = '';
			$('div#feedback2').hide();
			$('div#feedback1').show();
			$('div#feedback').css('visibility', 'visible');
			$('div.feedback2').removeClass('feedbackError');
			this.visible = true;
			osoboMenu.changeRolloversStatus(3, 3, false);
		}
	}

	this.hide = function ()
	{
		if (this.visible)
		{
			$('div#feedback').css('visibility', 'hidden');
			this.visible = false;
			osoboMenu.changeRolloversStatus(3, 3, true);
		}
	}

	this.toggle = function ()
	{
		if (this.visible)
			this.hide();
		else
			this.show();
	}

	this.send = function ()
	{
		$('div.feedback2').removeClass('feedbackError');
		$('img#feedbackPreloader').show();
		$('div#feedback1').hide();
		$.post(	'/feedback/send',
				$('form#feedbackForm').serializeArray(),
				function (response)
				{
					if (response.result.errorFields)
					{
						var f = response.result.errorFields;
						for (var i = 0; i < f.length; i++)
							$('div#feedbackField' + f[i]).addClass('feedbackError');

						$('div#feedback1').show();
					}
					else
					{
						$('div#feedback2').show();
						setTimeout("osoboFeedback.hide()", 3000);
					}
					$('img#feedbackPreloader').hide();
				},
				'json'
				);
	}
}

function strCount(str, what)
{
	var count = 0;
	var index = 0;
	while ((ret = str.indexOf(what, index)) > -1)
	{
		index = ret + what.length;
		count++;
	}
	return count;
}


// main
google.load('maps', '2.x');  

var osoboFeedback = new OsoboFeedback();

function initializeOsoboMap()
{
   var map = new google.maps.Map2($('div#osoboMap')[0]);
   var osoboIcon = new GIcon(G_DEFAULT_ICON, 'images/mapicon.png');
   osoboIcon.iconSize = new GSize(48, 49);

   var marker = new GMarker(new GLatLng(35.363436, 138.730888), { 'icon': osoboIcon, 'title': '«OSOBO Communications» Headquarter' });
   map.setMapType(G_SATELLITE_MAP);
   GEvent.addListener(marker, 'click', function () { osoboFeedback.toggle(); } );
   map.addOverlay(marker);
   map.addControl(new GSmallMapControl());
   map.setCenter(new google.maps.LatLng(35.363436, 138.730888), 13);
}

$(document).ready( function () {
	initializeOsoboMap();
	$('a#feedbackLink').hover( function(e) {
		e.preventDefault()
		osoboFeedback.show();
	}).click( function (e) {
		e.preventDefault();
		osoboFeedback.toggle();
	});
	osoboMenu.changeRolloversStatus(1, 3, false);
	$('a#feedbackSend').click( function (e) {
		e.preventDefault();
		osoboFeedback.send();
	});
});