/* 
 * itis.js:
 * JavaScript for rounded corners, on screen keyboard, and the rest.
 *
 * Copyright (c) 2008 UK Citizens Online Democracy. All rights reserved.
 * Email: matthew@mysociety.org. WWW: http://www.mysociety.org/
 */

$(window).load(function() {
    Nifty('div.rounded h2', 'top');
    if (document.getElementById('home')) {
        Nifty('div.row span');
	Nifty('#selection_box');
	// Nifty('#list_order');
	// Nifty('#list_order li');
	// Nifty('#pick_types li');
    } else if (document.getElementById('const') || document.getElementById('la')) {
        Nifty('div.roundedbox', 'transparent');
	// Nifty('div#minor-stats ul');
        // Nifty('ul#map_tabs a', 'transparent top');
	// Nifty('#subnav li');
	// Nifty('a.button');
    }
});

function kbdClick(e) {
	e.preventDefault();
	// $('#search').this.innerHTML;
	var t = $(this).text();
	if (t=='Delete') {
		var v = $('#search').val();
		$('#search').val(v.substring(0, v.length-1));
	} else if (t=='Clear') {
		$('#search').val('');
	} else if (t=='Space') {
		$('#search').val($('#search').val() + ' ');
	} else {
		$('#search').val($('#search').val() + t);
	}
	$('#search').change();
}

$(document).ready(function() {
    $('div.row span').click(kbdClick);
    $('#search').change(textChange);
    // if ($('#search').val())
    //     $('#search').change();

    $('#list_congested').bind('click', 'congested', orderChange);
    $('#list_incidents').bind('click', 'incidents', orderChange);
    $('#list_alphabet').bind('click', '', orderChange);
    $('#type_la').bind('click', 'la', typeChange);
    $('#type_const').bind('click', 'const', typeChange);
});

function typeChange(e) {
	var new_type = e.data;
	var type = $('#type').val();
	if (type != new_type) {
		$('#type').val(new_type);
		if (new_type == 'la') {
			$('.type_selected').text('local authority');
    			$('#img_type_const').attr('src', 'images/buttons/constituencies-inactive.jpg');
    			$('#img_type_la').attr('src', 'images/buttons/local-auths-active.jpg');
		} else {
			$('.type_selected').text('constituency');
    			$('#img_type_la').attr('src', 'images/buttons/local-auths-inactive.jpg');
    			$('#img_type_const').attr('src', 'images/buttons/constituencies-active.jpg');
		}
		$('#search').change();
	}
	return false;
}

function orderChange(e) {
	var new_order = e.data;
	var order = $('#order').val();
	if (order != new_order) {
		$('#order').val(new_order);
		if (new_order == 'congested') {
			$('#img_list_congested').attr('src', 'images/buttons/most-congested-active.jpg');
			$('#img_list_incidents').attr('src', 'images/buttons/most-accidents-inactive.jpg');
			$('#img_list_alphabet').attr('src', 'images/buttons/alphabetically-inactive.jpg');
		} else if (new_order == 'incidents') {
			$('#img_list_congested').attr('src', 'images/buttons/most-congested-inactive.jpg');
			$('#img_list_incidents').attr('src', 'images/buttons/most-accidents-active.jpg');
			$('#img_list_alphabet').attr('src', 'images/buttons/alphabetically-inactive.jpg');
		} else if (new_order == '') {
			$('#img_list_congested').attr('src', 'images/buttons/most-congested-inactive.jpg');
			$('#img_list_incidents').attr('src', 'images/buttons/most-accidents-inactive.jpg');
			$('#img_list_alphabet').attr('src', 'images/buttons/alphabetically-active.jpg');
		}
		$('#search').change();
	}
	return false;
}

function textChange(e) {
	$('#results_box').css('opacity', 0.5);
	$('#loading').html('<img src="images/loading.gif" alt="Loading..." width="24" height="24">').show();

	var search = $('#search').val();
	var order = $('#order').val();
	var type = $('#type').val();
	$('#results_list').load('ajax.php?t=' + type + '&l=' + order + '&s=' + encodeURIComponent(search), function(){
		$('#loading').hide();
		$('#results_box').css('opacity', 1);
	});
}

