	$(function(){
	
		pageLocation = null;
		
		$('.closed').each(function(){
			eleClosed = $(this).attr('id').replace('-cont', '-list');					
			$('#'+eleClosed).hide();		
		})
		
		$('.sub-title').click(function(){
			ele = $(this).parent().parent().attr('id').replace('-header', '-list');				
			openItems = $('.open').length;
			
		//	if (($(this).parent().parent().parent().hasClass('closed')) && (openItems >= 1)) {
                // We need to hide everything and then show this one
                $('div[id$="list"]:visible').slideUp().removeClass('open').addClass('closed');
                $('#' + ele).slideDown('slow', function() { $(this).parent().removeClass('closed').addClass('open'); });
		/*	} else {
			
				$('#'+ele).slideToggle('slow', function(){
					if ($(this).is(':visible')) {
						$(this).parent().removeClass('closed')
						$(this).parent().addClass('open');
					} else {
						$(this).parent().removeClass('open');
						$(this).parent().addClass('closed');
					}
				});
			}*/
		})
		
		$('.col-wide ul li').live('click', function(){
			
            // Get the URL for our page
			page_url = $(this).find('span').attr('id');

            // Get the Slug for our page (Used to find sub pages)
            page_slug = $(this).find('span').attr('rel');
            
            // Title of our Location
			listVal = $(this).text();				
            
			eleHeader = $(this).parent().parent().parent().parent().attr('id').replace('-list', '-header');				
			$('#'+eleHeader).find('.select-value').text(listVal);				
			headerVal = $('#'+eleHeader).find('.select-value').text();	
			
            // Ajax Request - Check our parent div class and make the appropriate request
            ajax_request = $(this).parent().parent().attr('class');
            
            switch(ajax_request) {
                case 'ajax-continent-list':
                    // Set our ajax target content to loading (Incase it displays before we have finished)
                    $('#'+eleHeader).parent().next().find('.ajax-country-list').html('<p>Loading...</p>');
                
                    // We are choosing a continent, so we need to find our countries
                    $.get("/destinations/advancedsearch_ajax/", { ajax: 'True', action: 'get_countries', continent: page_slug }, function(data){
                        var return_arr = [];
                        $.each(data.result, function(i, response) {
                            return_arr.push('<li><span id="' + response.url + '" title="' + response.name + '" rel="' + response.slug + '">' + response.name + '</span></li>');
                        });
                        
                        // Get our target
						$('#'+eleHeader).parent().next().find('.ajax-country-list').html('<ul>' + return_arr.join('') + '</ul>');
                    }, "json");
                break;
                case 'ajax-country-list':
                    // Set our ajax target content to loading (Incase it displays before we have finished)
                    $('#'+eleHeader).parent().next().find('.ajax-city-list').html('<p>Loading...</p>');
                
                    // We are choosing a continent, so we need to find our countries
                    $.get("/destinations/advancedsearch_ajax/", { ajax: 'True', action: 'get_cities', country: page_slug }, function(data){
                        var return_arr = [];
                        $.each(data.result, function(i, response) {
                            return_arr.push('<li><span id="' + response.url + '" title="' + response.name + '" rel="' + response.slug + '">' + response.name + '</span></li>');
                        });
                        
                        // If there are no cities, let's just go straight to the country
                        if (return_arr.length == 0) {
                            window.location = '/destinations/' + page_slug + '/';
                            return;
                        }

                        // Get our target
						$('#'+eleHeader).parent().next().find('.ajax-city-list').html('<ul>' + return_arr.join('') + '</ul>');
                    }, "json");
                break;
                case 'ajax-city-list':
                    // We have chosen a city, take us to that page
                    if (page_url) {
                        window.location = page_url;
                    }
                break;
            } 
            
            if (listVal == headerVal) {		
                if ($('#'+eleHeader).parent().next().length == 1) {					
                    containterIdPrev = $('#'+eleHeader).attr('id').replace('-header', '-list');
                    $('#'+containterIdPrev).slideUp('slow', function() {
                        $('#'+eleHeader).parent().removeClass('open').addClass('closed');
                    });

                    containerIdNext = $('#'+eleHeader).parent().next().attr('id').replace('-cont', '-list');
                    $('#'+containerIdNext).slideDown('slow', function() {
                        nextCont = $('#'+containerIdNext).attr('id').replace('-list', '-cont')
                        $('#'+nextCont).removeClass('closed').addClass('open');
                    });
                }
                $('#'+eleHeader).find('.button').fadeIn('slow');
			}
            		
			if (page_url) {
				pageLocation = page_url;
			}
		})
	
		$('.button').click(function(){
			window.location = pageLocation;
		})
        
		$('#destination-search').css('top', '390px');
        
/* This button should no longer do anything
        $('.toggle-search').click(function(){
			heightAnim = 0;				
			searchHeight = $('#destination-search').css('top').replace('px', '');			
			if (searchHeight == 0) {
				heightAnim = 390;
				$(this).removeClass('active');
			} else {
				$(this).addClass('active');
			}			

			$('#destination-search').animate({
				top: heightAnim+'px'
			},500 ,function() {
			
			});		
		}); */
		
        // Populate our Continent list
        $.get("/destinations/advancedsearch_ajax/", { ajax: 'True', action: 'get_continents' }, function(data){
            var return_arr = [];
            $.each(data.result, function(i, response) {
                return_arr.push('<li><span id="' + response.url + '" title="' + response.name + '" rel="' + response.slug + '">' + response.name + '</span></li>');
            });
            
            $('.ajax-continent-list').html('<ul>' + return_arr.join('') + '</ul>');                        
        }, "json");
	});

