var indexs = new Array('destination', 'week', 'departure', 'package');
var default_display = new Array("Where To?", "When?", "From Where?", "Package?");

var set_active = function(index)
{
    $('input[@name=' + indexs[index] +'_display]').attr('disabled', '');
};

var close_section = function(id)
{
    $('#' + id).css({'display': 'none'});
};

var open_check = function()
{
    for(var x = 1; x < 4; x++)
    {
        if($('#' + indexs[x] + '_list').css('display') != 'none')
        {
            return true;
        }
    }

    return false;
};

var set_inactive = function(index)
{
    $('input[@name=' + indexs[index] +'_display]').attr('disabled', 'disabled');
};

var reset_data = function(index)
{
    for(var x = index; x < 4; x++)
    {
        if(x !== 0)
        {
            $('#' + indexs[x] + '_list').empty();
        }
        $('input[@name=' + indexs[x] +'_display]').val(default_display[x]);
        $('input[@name=' + indexs[x] +']').val('');
        set_inactive(x);
    }
};

var update_input = function(index, data, callback_function)
{
    if(index === 0)
    {
        $('input[@name=tour_season]').val(data.split('_')[2]);
    }

    var display_value = data.split('_')[0];
    var real_value = data.split('_')[1];

    $('input[@name=' + indexs[index] +'_display]').val(display_value);
    $('input[@name=' + indexs[index] +']').val(real_value);
    $('#' + indexs[index] + '_list').css({'display': 'none'});

    switch(indexs[index])
    {
		case 'destination':
			reset_data(1);
			set_active(1);
			$('input[@name=week_display]').addClass('ajax_loading').addClass('loading');
			$('#week_list').load('/trip_builder.php',
			{
				'get': 'week',
				'destination': $('input[@name=destination]').val(),
				'tour_season': $('input[@name=tour_season]').val()
			},
			function()
			{
				if($('#week_list ul li').length == 1 && $('#departure_list p').length == 0)
				{
					update_input(1, $('#week_list ul li a').attr('rel'), callback_function);
				}
				$('input[@name=week_display]').removeClass('ajax_loading').removeClass('loading');
			});
			break;

		case 'week':
			reset_data(2);
			set_active(2);
			$('input[@name=departure_display]').addClass('ajax_loading').addClass('loading');
			$('#departure_list').load('/trip_builder.php',
			{
				'get': 'depart_city',
				'destination': $('input[@name=destination]').val(),
				'tour_season': $('input[@name=tour_season]').val(),
				'week': $('input[@name=week]').val()
			},
			function()
			{
				if($('#departure_list ul li').length == 1 && $('#departure_list p').length == 0)
				{
					//alert('auto selected');
					update_input(2, $('#departure_list ul li a').attr('rel'), callback_function);
				}
				$('input[@name=departure_display]').removeClass('ajax_loading').removeClass('loading');
			});
			break;

		case 'departure':
			reset_data(3);
			set_active(3);
			$('input[@name=package_display]').addClass('ajax_loading').addClass('loading');
			$('#package_list').load('/trip_builder.php',
			{
				'get': 'package_option',
				'destination': $('input[@name=destination]').val(),
				'week': $('input[@name=week]').val(),
				'departure_code': $('input[@name=departure]').val()
			},
			function()
			{
				$('input[@name=package_display]').removeClass('ajax_loading').removeClass('loading');

				if($('input[@name=departure_display]').val() === 'Hotel Only')
				{
					$('input[@name=package_display]').val('Hotel Only');
					update_input(3, 'Hotel Only_Hotel Only', callback_function);
				}
				else if($('input[@name=departure_display]').val() === 'City Passes Only')
				{
					$('input[@name=package_display]').val('City Passes Only');
					update_input(3, 'City Passes Only_City Passes Only', callback_function);
				}

				if($('#package_list ul li').length == 1 && $('#departure_list p').length == 0)
				{
					//alert('auto selected');
					update_input(3, $('#package_list ul li a').attr('rel'), callback_function);
				}
			});
			break;

        case 'package':
            //alert('You Are Done');
            tb_show(null, '#?TB_inline&height=80&width=500&inlineId=building_trip&modal=true', false);
            if($('input[@name=package_display]').val() === 'Hotel Only')
            {
            	callback_function();
                location.href = '/trip_finder/choose_hotel.php?page_function=choose_hotel_timeframe&from=home&hotel_included=1' +
                                '&destination=' + $('input[@name=destination]').val() +
                                '&departure=' + $('input[@name=week]').val() +
                                '&departure_code=' + $('input[@name=departure]').val();
            }
            else if($('input[@name=package_display]').val() === 'City Passes Only')
            {
            	callback_function();
            	location.href = '/trip_finder/choose_hotel.php?page_function=choose_hotel_timeframe&from=home&hotel_included=1' +
                                '&destination=' + $('input[@name=destination]').val() +
                                '&departure=' + $('input[@name=week]').val() +
                                '&departure_code=' + $('input[@name=departure]').val();
			}
            else
            {
                var hotel_included = 0;
                var search_string = $('input[@name=package_display]').val();
                if(search_string.search('Hotel') !== -1)
                {
                    hotel_included = 1;
                }

                if(search_string.search('Bus') !== -1)
                {
                    var transport_type = 'bus';
                }
                else if(search_string.search('Cruise') !== -1)
                {
                    var transport_type = 'cruise';
                }
                else
                {
                    var transport_type = 'flight';
                }

                callback_function();
                location.href = '/trip_finder/choose_transportation.php?page_function=choose_transportation&from=home' +
                                '&trans_type=' + transport_type +
                                '&hotel_included=' + hotel_included +
                                '&destination=' + $('input[@name=destination]').val() +
                                '&departure=' + $('input[@name=week]').val() +
                                '&departure_code=' + $('input[@name=departure]').val();
            }
            break;

        default:
            alert('Invalid Case in update_input');
            break;
    }
};

var initialize = function(index, style, callback_function)
{
    $('.input:eq(' + index + ')').bind('click', function()
    {
        if($('#' + indexs[index] + '_list').css('display') === 'none' && $('#' + indexs[index] + '_list').text() != '')
        {
            if(style === 'left')
            {
                var my_element = $('input[@name=' + indexs[index] +']').parent('li');
                //alert('input[@name=' + indexs[index] +']');
                var offset = my_element.offset();
                var height = my_element.height();

                var top = offset.top + height;
                var left = offset.left;

                if($('#sta_page_wrap').length == 1)
                {
                	top -= 128;
				}

                if(open_check() === false)
                {
                	var my_elem_node = $('#' + indexs[index] + '_list');
                    my_elem_node.css({'position': 'absolute',
                    				  'display': 'block',
                                      'top': top,
                                	  'left': left,
                                      'z-index': 1000,
                                      'margin-left': 4,
                                      'border': '2px solid #848484'});
                    if(jQuery.browser.msie && jQuery.browser.version < 7) {
            			ie_hover_fix('#' + indexs[index] + '_list');
						my_elem_node.width(0);
						my_elem_node.children("ul").each(function() {
							var my_width = $(this).width();
							var pa_width = $(this).parent().width();
							$(this).parent().width(my_width + pa_width);
						});
						my_elem_node.width(my_elem_node.width() - $('#' + indexs[index] + '_list > ul:last').width());

					}
                    if(my_elem_node.width() < 175) {
						my_elem_node.width(175);
					}
					if(jQuery.browser.msie) {
						var input_close_div = my_elem_node.children('.close_link');
						if(input_close_div.width() < my_elem_node.width()) {
							input_close_div.width(my_elem_node.width() + 208);
						}
					}
                }
            }
            else if(style === 'center')
            {
                var my_element = $('input[@name=' + indexs[index] +']').parent('div');
                var offset = my_element.offset();
                var height = my_element.height();
				var window_width = $(window).width();
                var top = offset.top + height;
                var left = (window_width / 2);

                if(open_check() === false)
                {
                	var my_elem_node = $('#' + indexs[index] + '_list');

                    if(jQuery.browser.msie && jQuery.browser.version < 7) {
            			ie_hover_fix('#' + indexs[index] + '_list');
						my_elem_node.width(0);
						my_elem_node.children("ul").each(function() {
							var my_width = $(this).width();
							var pa_width = $(this).parent().width();
							$(this).parent().width(my_width + pa_width);
						});
						my_elem_node.width(my_elem_node.width() - $('#' + indexs[index] + '_list > ul:last').width());

					}
                    if(my_elem_node.width() < 175) {
						my_elem_node.width(175);
					}
					my_elem_node.css({'display': 'block',
                                      'top': top,
									  'left': left,
                                      'z-index': 1000,
                                      'margin-left': ((my_elem_node.width() / 4) * -1),
                                      'border': '2px solid #848484'});
					if(jQuery.browser.msie) {
						var input_close_div = my_elem_node.children('.close_link');
						if(input_close_div.width() < my_elem_node.width()) {
							input_close_div.width(my_elem_node.width() + 208);
						}
					}
                }
			}
        }
        else
        {
            $('#' + indexs[index] + '_list').css({'display': 'none'});
        }
    });

    $('#' + indexs[index] + '_list li a').livequery('click', function()
    {
        update_input(index, $(this).attr('rel'), callback_function);
    });

    $('#' + indexs[index] + '_list p a').livequery('click', function()
    {
        update_input(index, $(this).attr('rel'), callback_function);
    });

    $('#' + indexs[index] + '_list .close_link a').livequery('click', function()
    {
        close_section(indexs[index] + '_list');
    });
};

var trip_build = function(style, callback_function)
{
	if(callback_function == undefined)
	{
		callback_function = function(){};
	}

    initialize(0, style, callback_function);
    initialize(1, style, callback_function);
    initialize(2, style, callback_function);
    initialize(3, style, callback_function);

    reset_index = 0;

    //set up other boxes if they click on the back button or soft
    if($('input[@name=week]').val() != '')
    {
        update_input(1, $('input[@name=week_display]').val() + "_" + $('input[@name=week]').val(), callback_function);
        reset_index = 2;
    }
    else
    {
        if($('input[@name=destination]').val() != '')
        {
            update_input(0, $('input[@name=destination_display]').val() + "_" + $('input[@name=destination]').val(), callback_function);
            reset_index = 1;
        }
        else
        {
            reset_index = 0;
        }
    }

    set_active(0);
};


function ie_hover_fix(hover_fix_target) {
	if(jQuery.browser.msie && jQuery.browser.version < 7) {
		$(hover_fix_target + ' li').each(function() {
			$(this).mouseover(function(){
				$(this).addClass("trip_builder_hover");
			});
			$(this).mouseout(function(){
				$(this).removeClass("trip_builder_hover");
			});
		});
	}
}