
// 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
	{
		$("a[liskZoom=true]").fancybox({hideOnContentClick:true});
	}
	catch (ex) {  }
	
	//fix pngs for ie6
	if (jQuery.ifixpng) jQuery.ifixpng.pixel = 'img/0.gif';
});

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


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 dialog.data.find('#'+itemsId).html("<li>"+message+"</li>"); //single message
		}
	});
	$('#simplemodal-container').draggable();
	
	//pngfix still not 100% working
	try
	{
		$("#idAlertBox img").ifixpng(); 
		$("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);
}


    jQuery.fn.equalize = function(){
        var height = 0,
            reset = $.browser.msie ? "1%" : "auto";
  
        return this
            .css("height", reset)
            .each(function() {
                height = Math.max(height, this.offsetHeight);
            })
            .css("height", height)
            .each(function() {
                var h = this.offsetHeight;
                if (h > height) {
                    $(this).css("height", height - (h - height));
                };
            });
            
    };
    
function ConvertRusToTranslit(str)
{
    var cyr = new Array(
        "а", "б", "в", "г", "д", "е", "ё", "ж", "з", "и", "й", "к", "л", "м", "н", "о", "п", "р", "с", "т", 
        "у", "ф", "х", "ц", "ч", "ш", "щ", "ъ", "ы", "ь", "э", "ю", "я"
    );

    var lat = new Array(
        "a", "b", "v", "g", "d", "e", "e","zh","z", "i", "j", "k", "l", "m", "n", "o", "p", "r", "s", "t", 
        "u", "f", "kh","c", "ch","sh","sch","", "y", "",  "e", "u", "ia"
    );

    for (var i=0; i<cyr.length; i++)
    {
        str = str.replace(new RegExp(cyr[i], 'ig'), lat[i]);
        str = str.replace(new RegExp(cyr[i].toUpperCase(), 'ig'), lat[i]);
    }

    return str;
}