var $ = jQuery.noConflict();

//Cufon
jQuery(document).ready(function($){Cufon.replace('nav li, h2, h3, #bk a, #comp cite em',{ hover: true});});


//Make ampersands lush
$(document).ready(function() {
    $("*:contains('&')", document.body)
        .contents()
        .each(
            function() {
                if( this.nodeType == 3 ) {
                    $(this)
                        .replaceWith( this
                            .nodeValue
                            .replace( /&/g, "<cite class='amp'>&</cite>" )
                        );
                }
            }
        );
    $("#id_bookinglist").change(
        function() {
            if ($(this).val() == "")
            {
//                 $("#id_departure_scheduled_0").val("");
                $("#id_airport_arrival_time_1").val("");
                $("#id_departure_scheduled_1").val("");
                $("#id_departure_number").val("");
//                 $("#id_arrival_scheduled_0").val("");
                $("#id_arrival_scheduled_1").val("");
                $("#id_arrival_number").val("");
            } else {
                for ( var i=0; i<bookinglist.length; i++ )
                {
                    if (bookinglist[i].pk == $(this).val())
                    {
                        var booking = bookinglist[i].fields;
                        var airport_arrival_time = booking.airport_arrival_time.split(" ");
                        var departure_scheduled = booking.departure_scheduled.split(" ");
                        var arrival_scheduled = booking.arrival_scheduled.split(" ");
//                         $("#id_departure_scheduled_0").val(departure_scheduled[0]);
                        $("#id_airport_arrival_time_1").val(airport_arrival_time[1]);
                        $("#id_departure_scheduled_1").val(departure_scheduled[1]);
                        $("#id_departure_number").val(booking.departure_number);
//                         $("#id_arrival_scheduled_0").val(arrival_scheduled[0]);
                        $("#id_arrival_scheduled_1").val(arrival_scheduled[1]);
                        $("#id_arrival_number").val(booking.arrival_number);
                        break;
                    }
                }
            }
        }
    );
    $("#id_clientlist").change(
        function() {
            if ($(this).val() == "")
            {
                $("#id_title option:eq(0)").attr("selected", "selected");
                $("#id_first_name").val("");
                $("#id_last_name").val("");
                $("#id_email").val("");
                $("#id_telephone").val("");
                $("#id_company").val("");
                $("#id_address").val("");
                $("#id_postcode").val("");
//                 $("#id_fuel_type option:eq(0)").attr("selected", "selected");
            } else {
                for ( var i=0; i<clientlist.length; i++ )
                {
                    if (clientlist[i].pk == $(this).val())
                    {
                        var client = clientlist[i].fields
                        $("#id_title option[value='"+client.title+"']").attr("selected", "selected");
                        $("#id_telephone").val(client.telephone);
                        $("#id_company").val(client.company);
                        $("#id_address").val(client.address);
                        $("#id_postcode").val(client.postcode);

                        var client = clientlist[i+(clientlist.length/2)].fields;
                        $("#id_first_name").val(client.first_name);
                        $("#id_last_name").val(client.last_name);
                        $("#id_email").val(client.email);
                        break;
                    }
                }
            }
        }
    );
    $("#id_vehiclelist").change(
        function() {
            if ($(this).val() == "")
            {
                $("#id_make option:eq(0)").attr("selected", "selected");
                $("#id_model").val("");
                $("#id_registration").val("");
                $("#id_colour").val("");
                $("#id_fuel_type option:eq(0)").attr("selected", "selected");
            } else {
                for ( var i=0; i<vehiclelist.length; i++ )
                {
                    if (vehiclelist[i].pk == $(this).val())
                    {
                        var car = vehiclelist[i].fields;
                        $("#id_make option[value='"+car.make+"']").attr("selected", "selected");
                        $("#id_model").val(car.model);
                        $("#id_registration").val(car.registration);
                        $("#id_colour").val(car.colour);
                        $("#id_fuel_type option[value='"+car.fuel_type+"']").attr("selected", "selected");
                        break;
                    }
                }
            }
        }
    );
});


function showVisibleSlide(id,focus)
{
    var obj = $("#"+id);
    obj.animate( { height: "show", opacity: "show" }, 200, function() {
        if ( focus ) $("#"+focus).focus();
    } );
}


function hideVisibleSlide(id,focus)
{
    var obj = $("#"+id);
    obj.animate( { height: "hide", opacity: "hide" }, 200, function() {
        if ( focus ) $("#"+focus).focus();
    } );
}

function toggleVisibleSlide(id,focus)
{
    var obj = $("#"+id);
    if ( obj[0].style.display == "none" )
    {
        showVisibleSlide(id,focus);
    }
    else
    {
        hideVisibleSlide(id,focus);
    }
}

function revealRegister()
{
        $("#login-block").animate( { opacity: "hide", paddingTop: 0, paddingBottom: 0 }, 300, function() {
                $("#register-block").animate( { opacity: "show" }, 300 );
        });
}

/*Truncate
jQuery(document).ready(function($){ 
	 $('#pa .trunk').truncate({max_length: 357});
});
*/

  


/*

//Pre and ap ends
jQuery(document).ready(function($){
    $(".addclass")
        .prepend('add stuff');
});


//Add class
jQuery(document).ready(function($){
	$('').addClass("none");
});

//Fade in
jQuery(document).ready(function($){
	$('').fadeIn(2000);
});


//Open in external windows
function externalLinks() {
 if (!document.getElementsByTagName) return;
 var anchors = document.getElementsByTagName("a");
 for (var i=0; i<anchors.length; i++) {
   var anchor = anchors[i];
   if (anchor.getAttribute("href") &&
       anchor.getAttribute("rel") == "external")
     anchor.target = "_blank";
 }
}
window.onload = externalLinks;

//JQuery Plugin: "EqualHeights" by:	Scott Jehl, Todd Parker, Maggie Costello Wachs (http://www.filamentgroup.com) Copyright (c) 2008 Filament Group. Licensed under GPL (http://www.opensource.org/licenses/gpl-license.php)
jQuery(document).ready(function($){ 	
	function equalHeight(group) {
		tallest = 0;
		group.each(function() {
			thisHeight = $(this).height();
			if(thisHeight > tallest) {
				tallest = thisHeight;
			}
		});
		group.height(tallest);
	}

	equalHeight($ ( "add stuff" ) );
	
});

*/
