$(function(){
    attach_events();

    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 attach_events(){
	$('#check_links #select_all').click(function(){
        $('#check_links input[@type=checkbox]').attr('checked', true);
    });
    $('#check_links .select_none').click(function(){
        $('#check_links input[@type=checkbox]').attr('checked', false);
    });
    $('#check_links #but_check_links').click(function(){
        test_next_link();
    });

	send_email_events();
	

	$('.accept').click(function() {
		id_val = $(this).parent().attr('id').substr(7);
		
		$.post('?accept_review', {review_id: id_val}, function(response) {
			if (response.indexOf('1') >= 0) {
				alert('Review accepted!');
				$('#review_'+id_val).remove();
				if ($('.review_container').length == 0) $('.contentwrap_admin').append('<p>No reviews left!</p>');
			} else alert('There was an error communicating with the server!');
		})
	});
	$('.reject').click(function() {
		id_val = $(this).parent().attr('id').substr(7);

		$.post('?reject_review', {review_id: id_val}, function(response) {
			if (response.indexOf('1') >= 0) {
				alert('Review rejected!');
				$('#review_'+id_val).remove();
				if ($('.review_container').length == 0) $('.contentwrap_admin').append('<p>No reviews left!</p>');
			} else alert('There was an error communicating with the server!');
		})
	});
}

function test_next_link(){

    parent_obj = $('#check_links input[@type=checkbox]:checked:first').parent();
	if (parent_obj.length == 0) 
        return;

    //link_val = parent_obj.find('a').attr('href');
    campsite_id = parent_obj.find('input[@type=hidden]').attr('value');
    parent_obj.find('input[@type=checkbox]').after('<span style="color: green" class="currently_checking">CHECKING.. </span>');
    $.get('?check_link=' + campsite_id, {}, function(response){
        parent_obj.find('.check_valid').html(response);
        parent_obj.find('.currently_checking').remove();
        parent_obj.find('.check_date').remove();
        parent_obj.find('input[@type=checkbox]').attr('checked', false);
        test_next_link();
    });
}

function send_email_events()
{
	 // send emails
    $('#send_emails #but_send_emails').click(function(){
    	if ($('#the_subject').length == 0 || $('#the_subject').attr('value') == '') {
    		alert('The subject is empty');
    		return;
    	}	
    	if ($('#the_message').length == 0 || $('#the_message').attr('value') == '') {
    		alert('The message is empty');
    		return;
    	}

    	parent = $('#send_emails input[@type=checkbox]:checked:first').parent();
    	if (parent.length > 0)  
        	send_next_email();
        else 	
        	alert('No emails have been selected!');	
    });
    $('#send_emails #select_all').click(function(){
        $('#send_emails input[@type=checkbox]').attr('checked', true);
    });
    $('#send_emails .select_none').click(function(){
        $('#send_emails input[@type=checkbox]').attr('checked', false);
    });
}

function send_next_email()
{
	parent = $('#send_emails input[@type=checkbox]:checked:first').parent();
 	if (parent.length == 0)  
    {
    	alert('All emails have been sent!');
    	$('del').each(function() {
    		$(this).after($(this).html());
    		$(this).remove();
    	});
    	$('.currently_sending').remove();

    	$('#message_wrapper').after('<textarea id="the_message" style="width: 500px; height: 100px;"></textarea>');
    	$('#message_wrapper').remove();
    	$('#subject_wrapper').after('<input id="the_subject" style="width: 500px;" type="text" />');
    	$('#subject_wrapper').remove();
        return;
    }

	email_address = parent.find('.the_email').text();
	if ($('#the_message').length > 0)
			post_message = $('#the_message').attr('value').replace(/(<([^>]+)>)/ig,"");
	else 	post_message = '';
	$('#the_message').after('<div id="message_wrapper">' + post_message + '</div>');
	$('#the_message').remove();

	if ($('#the_subject').length > 0)
			post_subject = $('#the_subject').attr('value').replace(/(<([^>]+)>)/ig,"");
	else 	post_subject = '';
	$('#the_subject').after('<div id="subject_wrapper">' + post_subject + '</div>');
	$('#the_subject').remove();

	if (parent.find('.currently_sending').length == 0)
		parent.find('input[@type=checkbox]').after('<span style="color: green" class="currently_sending">SENDING.. </span>');
	$.post('?send_email=' + email_address, {message: post_message, subject: post_subject}, function(response){
		parent.find('.currently_sending').text('SENT!');
		parent.find('.currently_sending').css('color', 'red');
		parent.html('<del>'+parent.html()+'</del>');
		parent.find('input[@type=checkbox]').attr('checked', false);

		send_next_email();
	});
}

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;
