$().ready(function() {

    if(jQuery().galleria) {
        $('#gallery').galleria();
    }

    if(jQuery().sortable) {
    	$(".thumbnails ul").sortable( { opacity: 0.5, cursor: 'move', update: function() {
    			var order = $(this).sortable("serialize") ;
                
                var loadUrl = $(this).attr('id');
                
                $.post( loadUrl, order, function(theResponse){
    				$(".result").html(theResponse);
                    $('.result').show(500).delay(3000).hide(1000);
    			});
       
                
    		}
    	});
    }
    
    if(jQuery().autocomplete) {
          
        // Categories/Businesses
        var category_name_url  = "/ajax/directoryAutoComplete";
        
    	$(".directory_category").click(function() {
    		$(this).val('');
    	});
    
        $(".directory_category").autocomplete(category_name_url, 
            {
                delay: 5, 
                width: 300, 
            }
        );

        // Town/Counties
        var location_url  = "/ajax/locationsAutoComplete";
    
    	$(".directory_location").click(function() {
    		$(this).val('');
    	});
    
        $(".directory_location").autocomplete(location_url, 
            {
                delay: 5, 
                width: 300, 
            }
        );

    }
    
	$(".back").click(function() {
		history.go(-1);
	});

    $("#entry input:visible:enabled:first").focus();
    $("#entry select:visible:enabled:first").focus();
    
    $("#upload_image").show();
    $(".image_uploading").hide();
	$("#upload_image").click(function() {
	    $(".image_uploading").show();
        $("#upload_image").hide();
	});
    
    $('#change_user_id').change(function() {
    
        alert('Importing advertisers default contact details.');
        window.location = $(this).attr('title')+$(this).val();
        return false;
    });
    
	$(".thumbs, .clicks").click(function() {
		window.location=$(this).find("a").attr("href");return false;
	});
    
	$(".newsletter_form").click(function() {
		window.location=$(this).find("a").attr("href");return false;
	});
    
    $('#edit_user_id').change(function() {
        
    });
    
    $('#search, .per_line, .per_line_admin').focus(function() {
      $(this).val('');
    });
    
   
    
    $('.per_line').focusout(function() {
      $(this).val('1 item per line');
    });
    
    $(".change_password").hide();
	$(".show_password").click(function() {
	    $("#password").val('');
	    $(".hide_password").hide();
		$(".change_password").show();
        $("#password").focus();
	});
    
	$(".password_cancel").click(function() {
        $(".change_password").hide();
        //Also make the password box empty
        $("#password").val('');
	    $(".hide_password").show();
	});
    
	$.ajaxSetup ({  
		cache: false  
	});
    
    $('.action_delete').click(function(event) {
        var answer = confirm('Are you sure you want to complete this task?');
        
        if (answer){
                
            var loadUrl = $(this).attr('id');
        
    		$.get(
    			loadUrl,
    			{language: "php", version: 5},
    			function(responseText){
                    alert(responseText);
                    location.reload();
    			},
    			"html"
    		);
            
            
        }else{
            
        }
        
        return false;
    }) 

    
    $('.action_status').click(function(event) {
        var answer = confirm('Are you sure you want to change this status?');
        
        if (answer){
        
            var loadUrl = $(this).attr('id');
        
    		$.get(
    			loadUrl,
    			{language: "php", version: 5},
    			function(responseText){
                    alert(responseText);
                    location.reload();
    			},
    			"html"
    		);
            
            
        }else{
            
        }
        
        return false;
    })
    
    $('.action_import').click(function(event) {

        var loadUrl = $(this).attr('id');
    
		$.get(
			loadUrl,
			{language: "php", version: 5},
			function(responseText){
                location.reload();
			},
			"html"
		);

        return false;
    })
    
    // Counties ----------------------------------
    
	$("#county").change(function(){
	   
       var id = $(this).val();
								  
		var loadUrl = '/ajax/searchTowns/' + id;
		var ajaxDiv = '#result_towns';
        
		$.get(
			loadUrl,
			{language: "php", version: 5},
			function(responseText){
				$(ajaxDiv).html(responseText);
			},
			"html"
		);

	});
    
    // Categories ----------------------------------
    
	$("#categories").change(function(){
	   
       // First make result_cat_id dissapear
       $("#result_cat_id").hide();
	   
       var id = $(this).val();
       					  
		var loadUrl = '/ajax/searchSubCategories/' + id;
		var ajaxDiv = '#result_cats';
        
		$.get(
			loadUrl,
			{language: "php", version: 5},
			function(responseText){
				$(ajaxDiv).html(responseText);
			},
			"html"
		);
        
        //Reset the other options
        $("#counties").val('0');
        
	});
    
	$("#free_categories").change(function(){
	   
       // First make result_cat_id dissapear
       $("#result_cat_id").hide();
	   
       var id = $(this).val();
       					  
		var loadUrl = '/ajax/searchFreeSubCategories/' + id;
		var ajaxDiv = '#result_cats';
        
		$.get(
			loadUrl,
			{language: "php", version: 5},
			function(responseText){
				$(ajaxDiv).html(responseText);
			},
			"html"
		);
        
        //Reset the other options
        $("#counties").val('0');
        
	});
    
    // Search ----------------------------------
    
    $(".sub_search").hide();
    
    $(".search").click(function(){			
	  $(".sub_search").show();
	});
  
    $(".sub_search").mouseover(function(){			
	  $(".sub_search").show();
	});

    $(".sub_search").mouseout(function(){			
	  $(".sub_search").hide();
	});
    
    // Models ----------------------------------
    
	$("#make").change(function(){

       var id = $(this).val();
								  
		var loadUrl = '/ajax/searchModels/' + id;
		var ajaxDiv = '#result_models';
        
		$.get(
			loadUrl,
			{language: "php", version: 5},
			function(responseText){
				$(ajaxDiv).html(responseText);
			},
			"html"
		);

	});
    
    
    // Forms  ----------------------------------
    
	$("#county_id").change(function(){
       
        var id = $(this).val();			  
		var loadUrl = '/ajax/formTowns/' + id;
		var ajaxDiv = '#result_town_id';
        
		$.get(
			loadUrl,
			{language: "php", version: 5},
			function(responseText){
				$(ajaxDiv).html(responseText);
			},
			"html"
		);

	});
    
	$("#free_cat_id").change(function(){
       
        var id = $(this).val();			  
		var loadUrl = '/ajax/formCarsFree/' + id;
		var ajaxDiv = '#result_cat_id';
        
		$.get(
			loadUrl,
			{language: "php", version: 5},
			function(responseText){
				$(ajaxDiv).html(responseText);
			},
			"html"
		);

	});
    
	$("#cat_id").change(function(){
       
        var id = $(this).val();			  
		var loadUrl = '/ajax/formCars/' + id;
		var ajaxDiv = '#result_cat_id';
        
		$.get(
			loadUrl,
			{language: "php", version: 5},
			function(responseText){
				$(ajaxDiv).html(responseText);
			},
			"html"
		);

	});
    	
});
