/**
 * @author aprian
 */

/* ajax for get detail of a row in a table
 * getObj : id of the link placeholder
 * getLink : url of the server side code
 * class created: .xdetail, .xwrapper
 */

function getRowDetail(getObj,getLink){
	$("#" + getObj +" a").click(function(){
		var clickedRow = $(this).parents("tr");
		var ajaxRow = clickedRow.next('.xdetail');
		var link = getLink + $(this).attr('href');
		var newRow = '<tr class=\"xdetail\"><td colspan=\"3\"><div class=\"xwrapper\"></div></td></tr>';
		var ajaxLoader = '<div id=\"xloader\"><img src=\"/assets/images/ajax-loader.gif\" /></div>';
		
		/* show / delete xdetail */
		var ajaxRowExists = ajaxRow.length;
		if(ajaxRowExists === 0) {
			
			/* create row for ajax content */
			clickedRow.after(newRow);
			
			/* set */
			thisAjaxRow = clickedRow.next('.xdetail');
			thisAjaxDiv = thisAjaxRow.children().children();

			/* setup ajax start stop */
			thisAjaxDiv.ajaxSend(function(){
				$('#' + getObj + ' tr.xdetail').not(thisAjaxRow).children().children().slideUp('slow', function(){
					$(this).parent().parent().remove();
				});
				$(this).parent('td').append(ajaxLoader).show('slow');
			});
			
			/* load ajax content */ 
			thisAjaxDiv.load(link, function(){
				$this = $(this);
				$('#xloader').slideUp('slow',function(){
					$this.slideDown(700);
					$(this).remove();
				});
				
			});
			return false;
			
		} else {
			ajaxRow.children().children().slideUp('slow', function(){
				ajaxRow.remove();	
			});
			return false;
		}
	});
}
