/**
 * 
 * @version $Id: ajax.js 1253 2009-07-02 21:46:46Z midget $
 * 
 * @license http://www.midworld-networks.com/licenses/outsoursing.html
 * @author Dario Minnucci <dario.minnucci@midworld-networks.com>
 * @copyright Copyright 2009, Dario Minnucci
 *            <dario.minnucci@midworld-networks.com>
 * 
 * @package midworld.net
 * @subpackage ajax
 * @access public
 * 
 */

/**
 * Show the given message using AJAX overlay
 * 
 * @param string
 *            msg
 */
function show_overlay(msg) {

	$(document)
			.ready(
					function() {

						$
								.blockUI( {
									css : {
										fontSize : 'large',
										fontWeight : 'bold',
										border : '2px #333 solid',
										padding : '15px',
										backgroundColor : '#FFF',
										'-webkit-border-radius' : '10px',
										'-moz-border-radius' : '10px',
										opacity : .95,
										color : '#666'
									},

									message : "<div style='text-align:center;'><img src='../images/icons/common/loading.gif' /><br>"
											+ msg + "</div>"

								});

						// setTimeout($.unblockUI, 2000);

					});
}
/**
 * Hide AJAX overlay
 */
function hide_overlay() {

	$(document).ready(function() {
		// make fadeOut effect shorter
			$.unblockUI( {
				fadeOut : 3000
			});
		});

}

function load_form(cpid, url) {

	$(document).ready(function() {

		show_overlay('Loading...');

		$('#container').hide();
		$('#container').load(url, {
			'cpid' : cpid
		});

		$("#container").ajaxComplete(function() {
			$('#container').slideDown(500);
		});

		hide_overlay();

	});

}

/**
 * Display message overlay
 * 
 * @param string
 *            msg
 */
function display_message(msg) {

	$(document).ready(function() {

		$.blockUI( {
			css : {
				fontSize : 'large',
				fontWeight : 'bold',
				border : '2px green solid',
				padding : '15px',
				backgroundColor : '#EFF',
				'-webkit-border-radius' : '10px',
				'-moz-border-radius' : '10px',
				opacity : .95,
				color : 'green'
			},

			message : "<div style='text-align:center;'>" + msg + "</div>"

		});

		setTimeout(function() {
			$.unblockUI( {
				fadeOut : 1000
			});
		}, 3000);

	});

}

/**
 * Display error overlay
 * 
 * @param string
 *            msg
 */
function display_error(msg) {

	$(document).ready(function() {

		$.blockUI( {
			css : {
				fontSize : 'large',
				fontWeight : 'bold',
				border : '2px #CC0000 solid',
				padding : '15px',
				backgroundColor : '#FEA',
				'-webkit-border-radius' : '10px',
				'-moz-border-radius' : '10px',
				opacity : .95,
				color : '#CC0000'
			},

			message : "<div style='text-align:center;'>" + msg + "</div>"

		});

		setTimeout(function() {
			$.unblockUI( {
				fadeOut : 1000
			});
		}, 3000);

	});

}

function doPost(form) {

	var debug = "#debug";

	$('#error').html('');
	$('#error').hide();
	$('#message').html('');
	$('#message').hide();
	$('#debug').html('');
	$('#debug').hide();

	var button_initial_value = $('#button').attr('value');

	// prepare the form when the DOM is ready
	$(document).ready(function() {

		var options = {
			target : debug, // target element(s) to be updated with server
			// response
			type : "post",
			dataType : 'script',
			// beforeSubmit : showRequest, // pre-submit callback
			// success : showResponse
			// post-submit callback

			// other available options:
			// url: url // override for form's 'action' attribute
			// type: type // 'get' or 'post', override for form's
			// 'method' attribute
			// dataType: null // 'xml', 'script', or 'json'
			// (expected server response type)
			// clearForm: true // clear all form fields after
			// successful submit
			// resetForm: true // reset the form after successful
			// submit

			beforeSubmit : set_button_name('...'),

			success : set_button_name(button_initial_value)

		// $.ajax options can be used here too, for example:
		// timeout: 3000
		};

		// bind form using 'ajaxForm'
			$('#' + form).ajaxForm(options);
			$('#' + form).submit();

		});

}

function set_button_name(label) {
	// alert(label);
	$('#button').attr('value', label);
	// alert( $('#button').attr('value') );
}

//
// Upload
//
/*
 * function u( id, action_url ) {
 * 
 * var url = action_url;
 * 
 * $("#loading") .ajaxStart(function(){ $(this).show(500); })
 * .ajaxComplete(function(){ $(this).hide(500); });
 * 
 * 
 * $.ajaxFileUpload ( { url: url, secureuri:false, fileElementId: id, dataType:
 * 'json', success: function (data, status) { if(typeof(data.error) !=
 * 'undefined') { if(data.error != '') { alert(data.error); }else {
 * alert(data.msg); } } }, error: function (data, status, e) { alert(e); } } );
 * 
 * return false; }
 */
function upload(file) {

	// prepare the form when the DOM is ready
	$(document).ready(function() {
		var options = {
			// target : div_debug, // target element(s) to be updated with
			// server response
			beforeSubmit : showRequest, // pre-submit callback
			success : showResponse,
			// post-submit callback

			// other available options:
			// url: url // override for form's 'action' attribute
			// type: type // 'get' or 'post', override for form's
			// 'method' attribute
			// dataType: null // 'xml', 'script', or 'json'
			dataType : 'script'
		// (expected server response type)
		// clearForm: true // clear all form fields after
		// successful submit
		// resetForm: true // reset the form after successful
		// submit

		// $.ajax options can be used here too, for example:
		// timeout: 3000
		};

		// bind form using 'ajaxForm'
			$('#frm_images').ajaxForm(options);
			$('#frm_images').submit();

		});

}

// pre-submit callback
function showRequest(formData, jqForm, options) {

	alert('showRequest();');

	// formData is an array; here we use $.param to convert it to a string to
	// display it
	// but the form plugin does this for you automatically when it submits the
	// data
	var queryString = $.param(formData);

	// jqForm is a jQuery object encapsulating the form element. To access the
	// DOM element for the form do this:
	// var formElement = jqForm[0];

	alert('About to submit: \n\n' + queryString);

	// here we could return false to prevent the form from being submitted;
	// returning anything other than false will allow the form submit to
	// continue
	return true;
}

// post-submit callback
function showResponse(responseText, statusText) {
	// for normal html responses, the first argument to the success callback
	// is the XMLHttpRequest object's responseText property

	// if the ajaxForm method was passed an Options Object with the dataType
	// property set to 'xml' then the first argument to the success callback
	// is the XMLHttpRequest object's responseXML property

	// if the ajaxForm method was passed an Options Object with the dataType
	// property set to 'json' then the first argument to the success callback
	// is the json data object returned by the server

	alert('status: '
			+ statusText
			+ '\n\nresponseText: \n'
			+ responseText
			+ '\n\nThe output div should have already been updated with the responseText.');
}

function show_dialog(d) {

	$(document).ready(function() {

		$.ajax( {
			type : "POST",
			url : "../dialog_builder.php",
			data : "type=error",
			async : false,
			cache : false,
			success : function(html) {
				// Append code to the dialog box
			$('#' + d).html(html);

		}
		});
	});

	// alert(d);
	// alert('calling display_dialog(' + d + ')');
	display_dialog(d);
}

function display_dialog(d) {
	// Dialog
	$(d).dialog( {
		autoOpen : false,
		width : 600,
		buttons : {
			"Ok" : function() {
				$(this).dialog("close");
			},
			"Cancel" : function() {
				$(this).dialog("close");
			}
		}
	});
	// alert('displayed');
}

function delete_image(uid, cpid, piid, filename) {

	// alert( "uid : "+uid+ " | cpid :"+cpid+" | ppid : "+piid+" | filename:
	// "+filename );

	$(document).ready(
			function() {

				var qs = "uid=" + uid + "&cpid=" + cpid + "&piid=" + piid+ "&filename=" + filename;

				$.ajax( {
					type : "POST",
					url : "../ajax/action.delete_image.php",
					data : qs,
					async : false,
					cache : false,
					success : function(html) {
						alert(html);
					}

				});

				//load_form(cpid, '/forms/property.images.form.php');

				
				include( 'establishment_images', '/forms/property.images.form.php', cpid );
				
				
			});

}



function include( container, url, uniqueid ) {
	
	//alert(container+"|"+url+"|"+uniqueid);
	
	$(document).ready(function() {

		//show_overlay('Including...');
		
		$('#'+container).hide();
		$('#'+container).load(url, {
			'cpid' : uniqueid
		});

		$('#'+container).ajaxComplete(function() {
			$('#'+container).slideDown(500);
		});

		//hide_overlay();

	});

}
