jQuery(document).ready(function(){
	jQuery('.ticket-row').each(function(){
		jQuery(this).click(function(e){
			e.preventDefault();
			var id = jQuery(this).attr('id').split('ticket-').join('');
			var url = document.location + '';
			if (url.indexOf('#') > 0) url = url.substr(0, url.indexOf('#'));
			// load the contact form using ajax
			jQuery.post(url, {'ticket-id':id, 'ftype':'get'}, function(data){
				// create a modal dialog with the data
				jQuery(data).modal({
					onOpen: function(){
						jQuery('#simplemodal-overlay').fadeIn(200);
						jQuery('#simplemodal-container').fadeIn(200);
						jQuery('.simplemodal-data').fadeIn(200);
						jQuery('#frm-reply').submit(function(){
							postJSON(url, {id:jQuery('#reply-id').val(),respid:jQuery('#reply-respid').val(),a:'postmessage',message:jQuery('#reply-message').val(),ftype:'reply'}, function(resp){
								if (resp.status != 'ok')
								{ jQuery('#frm-reply-messages').html(resp.html); }
								else
								{ jQuery('#frm-reply-messages').html(resp.html); jQuery('#reply-reset').click(); }
							});
							return false;
						});
					},
					onClose: function(){
						jQuery('#frm-reply').unbind();
						jQuery('#simplemodal-overlay').fadeOut(200);
						jQuery('#simplemodal-container').fadeOut(200);
						jQuery('.simplemodal-data').fadeOut(200).queue(function(){jQuery.modal.close(); jQuery(this).dequeue();});
					}
				});
			});
			
		});
	});
	var postJSON = function(url, post_data, callback) {
		jQuery.ajaxSetup({
			'global': false,
			'type': "POST",
			'error': function(XMLHttpRequest,textStatus, errorThrown) {
						  alert('Maaf, telah ada kesalahan pada respon server. Silahkan mencoba kembali.');
					 }
		});
		jQuery.post(url, post_data, function(response){
			if (typeof(callback) == 'function') callback(response);
		}, 'json');
	};						
});


