// JavaScript Document
var orderm;

$(document).ready(function() {
    // Go to charter page
    $(".charter .button input").click(function() {
        location.href = "http://"+document.location.host+"/charters";
    });
	
	// Go to yacht page
    $(".yacht .button input").click(function() {
        location.href = "http://"+document.location.host+"/yachts";
    });
	
	// Go to hotel page
    $(".hotel .button input").click(function() {
        location.href = "http://"+document.location.host+"/hotels";
    });
	
	
	// Go to hotel page
    $(".car .button input").click(function() {
        location.href = "http://"+document.location.host+"/cars";
    });


    //Emptyleg events
    $("#emptyleg tr")
    .click(
        function() {
            var txt = "<ul><li class=\"date\">"+$('td',this).eq(2).text()+"</li><li  class=\"direction\"> "+$('td',this).eq(0).text()+" &mdash; "+$('td',this).eq(1).text()+"</li><li  class=\"plane\">"+$('td',this).eq(3).text()+"</li><li  class=\"places\">"+$('#seat').text()+": "+$('td',this).eq(4).text()+"</li></ul>";
            $("#order > .summary").html(txt);
            orderm = $('td',this).eq(2).text()+" "+$('td',this).eq(0).text()+" &mdash; "+$('td',this).eq(1).text()+" "+$('td',this).eq(3).text()+" "+$('#seat').text()+": "+$('td',this).eq(4).text();
            $("#order form li:not(.buttons)").show();
            $("input[type='submit']").show();
            $('#message').remove();
        }
        );
	
    $("#emptyleg .order a")
    .click(
        function() {
            $("#emptyleg tr",this).parent.parent.click();
            return false;
        }
        );
        
        
        
      //Emptyleg events
    $("#available tr")
    .click(
        function() {
            var txt = "<ul><li class=\"date\">"+$('td',this).eq(2).text()+"</li><li  class=\"direction\"> "+$('td',this).eq(0).text()+", "+$('td',this).eq(1).text()+"</li><li  class=\"places\">"+$('#seat').text()+": "+$('td',this).eq(3).text()+"</li></ul>";
            $("#order > .summary").html(txt);
            orderm = $('td',this).eq(2).text()+" "+$('td',this).eq(0).text()+" "+$('td',this).eq(1).text()+" "+$('td',this).eq(2).text()+" "+$('#seat').text()+": "+$('td',this).eq(3).text();
            $("#order form li:not(.buttons)").show();
            $("input[type='submit']").show();
            $('#message').remove();
        }
        );
	
    $("#available .order a")
    .click(
        function() {
            $("#available tr",this).parent.parent.click();
            return false;
        }
        );  

    // Charter Order events

    $("#addleg").click(function() {

      /*  city = $(".leg:last select option:selected").eq(-2).val();
        city1 = $(".leg:first select option:selected").eq(0).val(); */
        
        //city = $(".leg:last .citys").val();
        city = $(".citys").eq(0).val();
        city1 = $(".leg:last .citye").val();

        pax = $(".leg:last input").eq(-1).val();
        data = $(".leg:last .datepicker").datepicker( "getDate" );
        // $clon = $(".leg:last").clone(true);
        $clon = $(".leg:last").clone(false);

       
        tmpDate = new Date();
        tmpDate.setDate(data.getDate()+1);
        month = tmpDate.getMonth()+1;
        month = (month>9)?month:("0"+""+month);

       
        now = new Date();

        $(".index",$clon).html(($(".leg").length+1)+".");
        $("input",$clon).val(tmpDate.getDate()+'.'+month+'.'+tmpDate.getFullYear());

        $("input:eq(0)",$clon).val();
        
       /* $("select:eq(0)",$clon).val(city);
        $("select",$clon).eq(-2).val(city1); */
        
        $(".citys",$clon).val(city1);
        $(".citye",$clon).val(city);
        
       // $(".acpl",$clon).removeClass('ac_input');
        $(".acpl",$clon).autocomplete(cities);
        
        $("input:last",$clon).val(pax);
        $(".leg:last").after($clon);
        
        $(".datepicker",$clon).removeClass('hasDatepicker')
        .removeData('datepicker')
        .attr("id","")
        .unbind()
        .datepicker({
            minDate: now //  new Date(year, month-1, day)
        });
        if($(".leg").length==2){
            $("#delleg").addClass( "visible" );
        }
        return false;
    });
    
    
    $("#delleg a").click(function() {
        if($(".leg").length>1){
            $(".leg:last").remove();
        }
        if($(".leg").length==1){
            $("#delleg").removeClass("visible");
        }
        return false;
    });
    
    
    	//Emptyleg events
	$("#emptyleg_edit tr")		.hover(
			function() {
				if( !$(this).hasClass("selected") ) {
					$(this).addClass("hover");
				}
			},
			function() {
				$(this).removeClass("hover");
			}
	)

       $("#emptyleg_edit tr td").click(
			function(e) {
				if($("a",this).size() == 0 ) {
				location.href = "http://"+location.host+"/"+$("a.nav",$(this).parent()).attr("href");
				}
			}
	);
	
	$("#pg>a").click(function() {
		act = $(this).attr("href"),
        $("#sch").attr("action",act);
        $("#sch").submit();
    });
	
	$("span.rm").each(function(){
		$(this).replaceWith('<a href="'+$(this).attr('title')+'">'+$(this).html()+'</a>');
	});
	
	
	$(".vcard .category").hide();
});

function sbm(){
    //alert($("#uname").val());

    $.post("ajaxpost/emptyleg", {
        "uname": $("#uname").val(),
        "tel": $("#tel").val(),
        "email": $("#email").val(),
        "comment": $("#comment").val(),
        "orderm": orderm
    },
    function(data){
        //    alert(data.result + data.err);
        if(data.err==''){
            $("#order form li:not(.buttons)").hide();
            $("input[type='submit']").hide();
            $('#order > .summary').after("<p id='message'>"+data.result+"</p>");
        }
        else{
            alert(data.result + data.err);
        }
   
    }, "json");
    
    return false;
    
}

function sbm_av(){
    //alert($("#uname").val());

    $.post("ajaxpost/available", {
        "uname": $("#uname").val(),
        "tel": $("#tel").val(),
        "email": $("#email").val(),
        "comment": $("#comment").val(),
        "orderm": orderm
    },
    function(data){
        //    alert(data.result + data.err);
        if(data.err==''){
            $("#order form li:not(.buttons)").hide();
            $("input[type='submit']").hide();
            $('#order > .summary').after("<p id='message'>"+data.result+"</p>");
        }
        else{
            alert(data.result + data.err);
        }
   
    }, "json");
    
    return false;
    
}
