$(function(){

    if (window.GBrowserIsCompatible && GBrowserIsCompatible()) {
		if ($('#position_lat').length > 0 && $('#position_long').length) 
        	initialize_gmap();
    }
    else
    	$('#map_canvas').remove();

    //attach_map_events();
})

function attach_map_events(){
    $('map area').click(function(){
        id_val = $(this).parent().attr('id').substr(6);
        if ($('#url_' + id_val).length == 0) 
            return;
        document.location = $('#url_' + id_val).attr('value');
    });
}
function initialize_gmap(){
 	if (!window.GBrowserIsCompatible || !GBrowserIsCompatible()) return this;
     
	position_lat_val = $('#position_lat').attr('value');
	position_long_val = $('#position_long').attr('value');
    if (position_lat_val == '' || position_long_val == '') {
        //$('#map_canvas').remove();
        return;
    }

    arr_lat = position_lat_val.split(',');
    arr_long = position_long_val.split(',');

    // find the middle
    middle_lat = 0;
    middle_long = 0;
    count = arr_lat.length;
    for (i = 0; i < count; i++) {
        middle_lat += parseFloat(arr_lat[i]);
        middle_long += parseFloat(arr_long[i]);
    }
    middle_lat /= count;
    middle_long /= count;

    // set the center
    zoom_factor = 13;
    if (count > 1) 
        zoom_factor = 8;
    var map = new GMap2(document.getElementById("map_canvas"));
    map.setCenter(new GLatLng(middle_lat, middle_long), zoom_factor);
    map.addControl(new GSmallMapControl());

    var blueIcon = new GIcon(G_DEFAULT_ICON);
    blueIcon.image = "http://labs.google.com/ridefinder/images/mm_20_red.png";
	blueIcon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
    blueIcon.iconSize = new GSize(12, 20);
	blueIcon.shadowSize = new GSize(22, 20);
	blueIcon.iconAnchor = new GPoint(6, 20);
    // add the points on the map
    for (i = 0; i < count; i++) {
        title_val = $('#title_' + i).attr('value');

        markerOptions = {
            icon: blueIcon,
            title: title_val
			
        };
        marker = new GMarker(new GLatLng(arr_lat[i], arr_long[i]), markerOptions);
		marker.id = i;
		GEvent.addListener(marker, "click", function() {
			url = $('#url_' + this.id).attr('value');
			if (typeof(url) != 'undefined')
				document.location = $('#url_' + this.id).attr('value');
		});

        map.addOverlay(marker);
    }
}
	 	var pcode = "";
		$(document).ready(function(){
		if (pcode!=null && pcode!=""){
			document.mapControl.postcode.value = pcode;
			//alert ("submiting postcode "+pcode);
			submitForm(document.mapControl);
			}
		});
		function findPostCode() {
			var theValue = document.thePostcodeForm.searchTerm.value;
			if (theValue != null && theValue != "") {
				document.mapControl.postcode.value = theValue;
				document.mapControl.placecode.value = "";
				submitForm(document.mapControl);
			}
		}
		function submitForm(form) {		
			if ( (form.onsubmit == null) || (form.onsubmit()) ) {
			   form.submit();
			}
		}
		
		//BROWSER DETECTION
addthis_url    = location.href;
addthis_title  = document.title;
addthis_pub    = 'network15';

var ns4 = document.layers;
var ns6 = document.getElementById && !document.all;
var ie4 = document.all;
$(function() {
	http_val = '';
	if ($('input#http').length > 0)
		http_val = $('input#http').attr('value');

	$('#quick_search input[@type=text]').focus(function() {
		if ($(this).attr('value') == $(this).attr('title'))
			$(this).attr('value', '');
	});

	$('#quick_search input[@type=text]').blur(function() {
		if ($(this).attr('value') == '')
			$(this).attr('value', $(this).attr('title'));
	});

	if ($('#frm').length > 0)
		$('#frm_search').remove();

	$('#but_post_review').click(function() {
		if ($('#post_review').css('display') != 'none')
		{
			rating_val = $('#post_review .star_on').length;
			$('#frm_post_review input[@name=rating]').attr('value', rating_val);
			text_review_val = $('#text_review').attr('value');
			if (text_review_val == '') {
				alert('Please enter the review text!');
				return;
			}
			$('#frm_post_review').submit();
			return;
		}

		if ($('#authenticated').length > 0 && $('#authenticated').css('display') != 'none')
			$('#post_review').show('fast');
		else
			$('#login_review').show('fast');
	});

	$('#link_logout').click(function() {
		$.get(http_val+'?review_logout', {}, function(response) {
			if (response.indexOf(1) == -1) return;
			$('#authenticated').hide();
			$('#login_review').hide();
			$('#post_review').hide();
			alert('You were logged out!');	
		})
	});

	$('#frm_login').submit(function() {
		user_val = $('#btn_user').attr('value'); 
		pass_val = $('#btn_password').attr('value');

		$.post(http_val+'?review_login', {user: user_val, pass: pass_val}, function(response) {
			if (response && response.trim() != '0') {
				$('#login_review').hide();
				$('#post_review').show();
				$('#authenticated .login_user').text(response);
				$('#authenticated').show();
			}
			else alert('Invalid Login!');
		});
		return false;
	});

	$('#text_review').keyup(limit_text);
	$('#text_review').keydown(limit_text);
})

String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}

function limit_text() 
{
	limitNum = 1000;
	val = $(this).attr('value');
	if (val.length > limitNum)
		$(this).attr('value', val.substring(0, limitNum))
}
  function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}
