
// append system data 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
	{
		jQuery("a[liskZoom=true]").fancybox({hideOnContentClick:true});	
	}
	catch (ex) {  }
	

	//fix pngs for ie6
	jQuery.ifixpng.pixel = 'img/0.gif';
	
	$('a[confirm=true]').click(function(){
		var elt = this;
		var href = this.href;
		document.nextHref = href;
		document.msg = this.title;
		confirm(document.msg,
			function(){
				document.location = document.nextHref;
			}); 
		return false;
	});
	if ($.browser.msie && $.browser.version==7)
		$('select').css('height','20px');
	
	try{
	if ($.browser.msie && $.browser.version<7 && $('.container3')[0].clientHeight < 400) 
	{
		$('.container3')[0].style.height = '400px';
	}
	}
	catch(e){}
	
});

function confirm(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;
}

function alert(message) 
{
	ShowAlert(message);
}

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%"],
		minHeight: 30,
		onShow: function (dialog) {
			dialog.container.css('height', 'auto');
			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 dialog.data.find('#'+itemsId).html("<li>"+message+"</li>"); //single message
		}
	});
	$('#simplemodal-container').draggable();
	
	//pngfix still not 100% working
	try
	{
		jQuery("#idAlertBox img").ifixpng(); 
		jQuery("a.modalCloseImg").ifixpng(); 
	}
	catch (ex) { }
}

function popupWindow(url,width,height,scroll) {
	var popUpWin = 0;
	
	if (scroll==null) scroll=false;
	
	if(popUpWin) {
		if(!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, 'popUpWin', '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();
}

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);
}


function insertAtCursor(myField, myValue) {
	//IE support
	if (document.selection) {
		myField.focus();
		sel = document.selection.createRange();
		sel.text = myValue;
	}
	//MOZILLA/NETSCAPE support
	else if (myField.selectionStart || myField.selectionStart == '0') {
		var startPos = myField.selectionStart;
		var endPos = myField.selectionEnd;
		myField.value = myField.value.substring(0, startPos)
		+ myValue + myField.value.substring(endPos, myField.value.length);
	} else {
		myField.value += myValue;
	}
	return false;
}
