// Render-time Events
$j(document).ready(function() {
	// Auto Hiders
	$j('.hidden').hide();
	$j('a.slidebox').click(function(){
		//alert('tests'+$j(this).attr('rel'));
		$j('.'+$j(this).attr('rel')+'.hidden').toggle(400);
		return false;
	});
	
	// Delete Buttons
	$j('a.delete').click(function(){
		result = confirm('Are you sure you want to delete this item?');
				
		// If this is an ajax call, then run it through ajax, and fade out the item
		if(result && $j(this).hasClass('ajax')){
			jQuery.get($j(this).attr('href'));
			id = '.'+$j(this).attr('rel');
			$j(id).hide('normal');
			return false;				
		}			
		return result;			
	});
	
	// Ajax Save
	$j('a.save.ajax').click(function(){
		loading();
		jQuery.ajax({
			type: 	'GET',
			url:	$j(this).attr('href'),
			success:	function(msg){				
				notify('success', 'Successfully Saved Data');
			},
			error:	function(msg){
				alert(msg);
			}		
		});		
		return false;
	});
	
	// Ajax stylings	
	
	// Link Styling
	/* These are cool, but I'm going to use CSS selectors to do them'
	$j("a[@href$j=pdf]").addClass("pdf");
	$j("a[@href$j=zip]").addClass("zip");
	$j("a[@href$j=psd]").addClass("psd");		
	*/
	
	// Popups
	$j('a.popup').click(function(){
		newwindow=window.open($j(this).attr('href'),'','height=200,width=150');
		if (window.focus) {newwindow.focus()}
		return false;
	});	
	
});

// -------------------
// Helper Functions
//

// Notification Section	
function alert(msg){notify('alert', msg);}
function notify(type, msg){
	$j('#notify_load').hide();
	$j('#notification').removeAttr('class').addClass(type).html(msg).animate({top: '0px'}, 800).wait(3000).animate({top: '-310px'}, 800);
}

function loading(msg){
	if(msg == undefined)
		msg = 'loading...';	
	$j('#notify_load').html(msg).show();
}

	
$j.fn.wait = function(time, type) {
       time = time || 1000;
       type = type || "fx";
       return this.queue(type, function() {
           var self = this;
           setTimeout(function() {
               $j(self).dequeue();
           }, time);
       });
};	