
/* Nano Templates (Tomasz Mazur, Jacek Becela) */

(function($){
  $.nano = function(template, data) {
    return template.replace(/\{([\w\.]*)\}/g, function (str, key) {
      var keys = key.split("."), value = data[keys.shift()];
      $.each(keys, function () { value = value[this]; });
      return (value === null || value === undefined) ? "" : value;
    });
  };
})(jQuery);

/**
 * append system blocks to the page
 */
$(document).ready(function(){

	//Template for Confirm message
	var comfirmTpl = '\
		<div id="idConfirmBox" style="display:none;"> \
			<a href="#" title="Close" class="modalCloseX simplemodal-close">x</a> \
			<div class="header"><span>Confirm</span></div> \
			<p class="message"></p> \
			<div class="buttons"> \
				<div class="no simplemodal-close">No</div><div class="yes">Yes</div> \
			</div> \
		</div>';
	$("body").append(comfirmTpl);
	
	//Template for Alert Message
	var alertTpl = '\
		<div id="idAlertBox" style="display:none;"> \
			<table> \
			<tr> \
				<td width="48"><img src="img/lisk/alert.png" /></td> \
				<td> \
					<ul style="padding:3px 3px 3px 12px;margin:0px;" id="idAlertItems"> \
						<li></li> \
					</ul> \
				</td> \
			</tr> \
			</table> \
		</div>';	
	$("body").append(alertTpl);

	//Template for Notify Message
	var notifyTpl = '\
		<div id="idNotifyBox" style="display:none;"> \
			<table> \
			<tr> \
				<td width="48"><img src="img/lisk/success.gif" /></td> \
				<td> \
					<ul style="padding:3px 3px 3px 14px;margin:0px;" id="idNotifyItems"> \
						<li></li> \
					</ul> \
				</td> \
			</tr> \
			</table> \
		</div>';	
	$("body").append(notifyTpl);
	
	//zoom, ie6 throws exception
	try
	{
		$("a[liskZoom=true]").fancybox({hideOnContentClick:true, overlayColor: '#000', overlayOpacity: 0.5, titlePosition: 'inside'});
	}
	catch (ex) {  }

});

/**
 * Show modal confirm box, if user clicks "yes" run the callback 
 * 
 * @param string message
 * @param callback
 * @return false
 */
function ShowConfirm(message, callback) 
{
	$('#idConfirmBox').modal({
		close:true, 
		position: ['25%'],
		overlayId:'confirmModalOverlay',
		containerId:'confirmModalContainer', 
		onShow: function (dialog) {
			dialog.data.find('.message').append(message);
			dialog.data.find('.yes').click(function () {
				$.modal.close();
				
				//check callback
				if (eval("(typeof(callback) != 'function') && (typeof(callback) != 'object')")) return false;

				//link
				if (callback.href)
				{
					location.href = callback.href;
					return true;
				}

				//form
				if(1==1)
				{

				}

				//function
				if ($.isFunction(callback)) 
				{
					callback.apply();
				}
			});
		}
	});
	$('#confirmModalContainer').draggable();

	return false;
}

/**
 * Show alert modal alert box,
 * set type to render as error or notification
 * 
 * @param string message
 * @param string type
 * @return
 */
function ShowAlert(message, type)
{
	if (!type) type='error';
	
	var blockId = 'idAlertBox';
	var itemsId = 'idAlertItems';
	if (type=='notify') 
	{
		blockId = 'idNotifyBox';
		itemsId = 'idNotifyItems';
	}
	
	$('#'+blockId).modal({
		position: ['25%'],
		onShow: function (dialog) {
			if (message.push) 
			{
				//array of messages
				
				var messages = '';
				for (var i=0; i<message.length; i++) messages += "<li>" + message[i] + "</li>";
				dialog.data.find('#'+itemsId).html(messages);
			}
			else 
			{
				//single message
				
				dialog.data.find('#'+itemsId).html("<li>" + message + "</li>"); 
			}
		}
	});
	$('#simplemodal-container').draggable();

}

/**
 * Open popup window
 * 
 * @param url
 * @param width
 * @param height
 * @param scroll
 * @return
 */
function popupWindow(url, width, height, scroll) 
{
	var popUpWin = 0; //modify this to open new popup in the same window
	//if (window.popUpWin && !popUpWin.closed) popUpWin.close();

	var left = (screen.width/2) - width/2;
  	var top = (screen.height/2) - height/2;
  	var scrolling = (scroll) ? 'yes' : 'no';

	popUpWin = open(url, 'popUpWinName', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=' + scrolling + ',resizable=no,copyhistory=yes,width=' + width + ',height=' + height + ',left=' + left + ', top=' + top + ',screenX=' + left + ',screenY=' + top);
	popUpWin.focus();
}

/**
 * Alert object's propertis,
 * set showValues to show properties' values
 * 
 * @param obj
 * @param showValues
 * @return
 */
function alertObj(obj, showValues)
{
	showValues = (showValues) ? true : false;
	var buf = '';
	for (var prop in obj)
	{
		if (showValues) buf += ' ' + prop + '=' + obj[prop] + ', ';
		else buf += ' ' + prop + ' ';
	}
	alert(buf);
}
