// <--
var imagedir = '/media/icons';
var scriptdir = '/plugins/ajax';

// Show hide text ------------------------------------------------------------------------------------------------------- //
function displayBusy(me) {
	me.html('<img src="'+ imagedir +'/dots.gif" alt="Loading" title="Loading" class="absmiddle" border="0" />');	
}

// Show hide text ------------------------------------------------------------------------------------------------------- //
function toggleText() {
    $me = $(this);
    var $obj_to_find  = $(this).attr('rel');
    var $obj = $('.' + $obj_to_find);
    
    if($obj)
    {
    	// close all other 'closable' classes.
		$('.closable').not($obj).hide();
        
        $obj.slideToggle("fast", function(){
            var blocks = $('.closable:visible');
            if(blocks.size() == 0 || $me.hasClass('cancelable'))
            {
               $('.showable').show('fast');            
            } 
        });
        
        return true;
    }
    return false;
}


// Display AJAX item ---------------------------------------------------------------------------------------------------- //
function displayItem() {
	var $type  = $(this).attr('rel');
	var $parent = $(this).parents($type);
	var $url = $(this).attr('rev');
	
	$parent.hide(1);

    var $child = $($type + '_form');
    $child.html('');
	// Load the item
	$($child).load($url, {}, 
		function(){ 
			$child.show();
		}
	);

	return false;
}

function postItem() {
    var url = $me.attr('rel');
    var item_block = $me.attr('rev');
	//var parent = $(item_block).parent();
    var $txt_to_change = $(item_block+ ' span');
  
    // ajax post the URL $("input#name").val
    //$.post(url, { value: $(item_block + '_form input:first').val() },
	$.post(url, $(item_block + '_form form').serialize(),
      function(data){
		$txt_to_change.load(data);
        $(item_block + '_form .revertIt').trigger('click');    
   	});
}

function revertItem() {
    var obj_to_find = $(this).attr('rel');
    
    var $show_obj = $(obj_to_find);    
    var $hide_obj = $(obj_to_find + '_form');
    
    $hide_obj.hide(1, function(){
        $show_obj.show();
    });
}

// Delete item --------------------------------------------------------------------------------------------------------- //
function deleteItem() {
	var $me = $(this);
	var $original = $(this).html();
	
	// Get parent to delete
	var $type  = $(this).attr('rel');
	var $parent = $(this).parents($type);
	var $url = $(this).attr('rev');
	var $actions = $(this).parents('ul');
	var id = $parent.attr('id');
		
	//alert($(this).html());	
	//alert($parent.html());
	//alert("Type:"+ $type +" Url:"+ $url +" Id:"+ id);	
	
	if( confirm("Are you sure you want to delete this item?") ) {
		displayBusy($(this));
		
		$.post($url, { item_id: id, type: $type, deleteItem: "1" },
			function (data) {
				if (data == 'ok') {
					// deleted
					$parent.fadeOut("slow");
				} else {
					// not deleted
					alert("Sorry we could not delete this item. Please try again later.");
					$me.html($original);
				}											
			}
		);		
	}
	
	return false;
}

// Check item --------------------------------------------------------------------------------------------------------- //
function checkItem() {
	var $parent = $(this).parents('div.checkItem');

	var $span = $('span', $parent).html($span);
	var $checked  = $(this).is(':checked')-0; //attr('checked');
	var $url = $(this).attr('rev');
	//var $value = $(this).attr('value');
	//alert($span);	
	//alert("Checked:"+ $checked +" Url:"+ $url);	
	
	// Show loading image
	displayBusy($('span',$parent));
	
	// Post data	
	$.post($url, { checked: $checked },
		function (data) {
			if (data == 'ok') {
				// updated
				// Show the text again and remove loader
				$('span', $parent).html($span);
			} else {
				// not updated
				alert('Server: ' + data);
				$('span', $parent).html($span);
			}											
		}
	);		
	
	return true;
}

function checkItemRemove() {
	var $parent = $(this).parents('div.checkItem');
    var $type  = $(this).attr('rel');
	var $div = $(this).parents($type);
	
	var $span = $('span', $parent).html($span);
	var $checked  = $(this).is(':checked')-0; //attr('checked');
	var $url = $(this).attr('rev');
	
	// Show loading image
	displayBusy($('span', $parent));
	
	// Post data	
	$.post($url, { checked: $checked },
		function (data) {
			if (data == 'ok') {
				// updated
				// Show the text again and remove loader
				$div.fadeOut("slow");
			} else {
				// not updated
				alert('Server: ' + data);
				$('span', $parent).html($span);
			}											
		}
	);		
	
	return true;
}

// Toggle item --------------------------------------------------------------------------------------------------------- //
function updateFlag() {
	var $original = $(this).html();
	
	// Get parent to delete
	var $class  = $(this).attr('rel');
	var $parent = $(this).parents($class);
	var $url = $(this).attr('rev');
	
	var id = $parent.attr('id');
	var status = $('img',$(this)).attr('alt');
	
	//alert($me.html());	
	//alert("Class:"+ $class +" Url:"+ $url +" Id:"+ id +" Status:"+ status);		
	
	$.post($url, { item_id: id, status_id: status, toggleItem: "1" },
					function (data) {
						if (data != '') {
							// change image
							$(this).html(data);	
						} else {
							// not deleted
							alert("Sorry we could not edit this item. Please try again later.");
							$(this).html($original);
						}											
					}
	);	
		
	return false;
}

// -------------------------------- Show Progress of Upload ---------------------------------- //
function showProgress() {
	document.getElementById('uploadprogress').style.display = 'block';    
   	return true;
}

// Manage table row mouseovers ----------------------------------------------------------------------------------------- //
function tableRollovers(rollover, highlight) {
	// Change the background colour for the table row when viewing a list of results
	rollover.mouseover( 
		function() {
			$(highlight, $(this)).addClass("over");
		}
	).mouseout(
		function() {
			$(highlight, $(this)).removeClass("over");				
		}
	);	
}

// Form Submit ---------------------------------------------------------------------------------------------------------- //
function sendForm() {
	// Get submit button text
	var $original = $(this).attr("value");
	
	// Disable button
	$(this).attr("disabled","disabled");

	// Change text
	$(this).attr("value","Submitting form...");
	
	$(this).parents("form").trigger('submit');

	/*
	// Enable button
	$(this).attr("disabled","");

	// Change text
	$(this).attr("value", $original);
	*/
}

function resetSubmitButton(form) {
    //var $me = $(this);
    
    //submit = 'Post again'
    console.log(form);
    
    //$($me + ' input').attr("disabled","");
    
    //$(this).attr("value", 'Post again');
    
    //return false;
}

function submitForm()
{
	$(this).parents("form").trigger('submit');
}

// Fade in messages ----------------------------------------------------------------------------------------------------- //
function setMessageFade()
{
	//$('#errorMessage').Highlight(2000, '#FF0000');
	//$('#successMessage').Highlight(2000, '#008000');	
	
	$("#errorMessage").fadeIn(3000);
	$("#successMessage").fadeIn(3000);
}

// Form functions ------------------------------------------------------------------------------------------------------- //
function formInit()
{
	$('.toggleSwap').click(toggleText);
	$('.submit').click(sendForm);
}
// -->
