function parse_json(json) {
	if(json == undefined || !json || json == '') return;
	/*json = json.replace('<pre>','');
	json = json.replace('</pre>','');*/
	var json = eval( "(" + json + ")");

	if(json == undefined || !json || !json.length) return;

	for(var i in json)
	{
		if(json[i] == null || typeof(json[i].type)=='undefined') continue;

		switch(json[i].type)
		{
			case 'html':
				var obj = $('#'+json[i].name); if(!obj.length) continue;
				if (json[i].value) {
					obj.html(json[i].value);
					obj.show('fast');
				}
				else {
					obj.hide('fast');
					obj.html('');
				}
								break;

			case 'inscr':
				apply_scripts(json[i].name);
				break;

			case 'code':
				eval(json[i].value);
				break;

			case 'alert':
				alert(json[i].value);
				continue;

			case 'go':
				window.location.href = json[i].value;
				continue;

			case 'remove':
				var obj = $('#'+json[i].name); if(!obj.length) continue;
				obj.remove();
				continue;

			case 'wnd':
					if (!wndids[json[i].wndid]) {
						obj = $("<div></div>");
						obj.html(json[i].value);
						ok = lang['line']['ok'];

						var buttons = {};
						if (json[i].form)
							buttons[lang['line']['ok']] = function() { $("form", obj).ajaxSubmit( function(result) {parse_json(result); } ); $(this).dialog("close");wndids[json[i].wndid] = null;}
						else
							buttons[lang['line']['ok']] = function() {}
						buttons[lang['line']['cancel']] = function() {$(this).dialog("close");wndids[json[i].wndid] = null;}

						$(obj).dialog({
							title: json[i].caption,
							width:json[i].width,
							height:json[i].height,
							resizable: false,
							draggable: true,
							close: function(ev, ui) { wndids[json[i].wndid] = null; },
							position: "center",
							buttons: buttons
						 });
						 wndids[json[i].wndid] = 1;
					}
				break;
			case 'upl':
				make_upload(json[i]);
				continue;
		}

		if(typeof(json[i].name) == 'undefined') continue;

		if(json[i].type == 'wnd')
		{
			setTimeout(function() { center(obj); }, 100);

			$('.title .close', obj).click(function() {
				$('.tinytextarea', obj).untinymce();
				$(obj).remove();
			});

			setTimeout(function(){ $("#fancy_loading").hide(); }, 1000);
		}
		set_form(obj);
	}
}

function msg (sMsg) {
	alert(sMsg)
}

function set_form(obj) {
	$('.ajaxform', obj).ajaxForm({ success: function(result) {parse_json(result);}}).
		bind('form-pre-serialize', function(event, $form, options) { if(typeof(tinyMCE) != 'undefined') tinyMCE.triggerSave(); });
}

function ltrim (s,what) {
	for (i = 0; i < what.length; i++) {
		while (s.substring(0,1) == what[i]) {
			s = s.substring(1,s.length);
		}
	}
	return s;
}

function trim(s,what) {
	for (i = 0; i < what.length; i++) {
		while (s.substring(0,1) == what[i]) {
			s = s.substring(1,s.length);
		}
		while (s.substring(s.length-1,s.length) == what[i]) {
			s = s.substring(0,s.length-1);
		}
	}
	return s;
}

function SetCookie(name, value) {
   var argv    = SetCookie.arguments;
   var argc    = SetCookie.arguments.length;
   var expires = (argc > 3) ? new Date(argv[3]) : null;
   //var path    = (argc > 4) ? argv[4] : null;
	var path    = '/';
   var domain  = (argc > 5) ? argv[5] : null;
   var secure  = (argc > 6) ? argv[6] : false;
   expires = 'Friday, 01-Jan-2399 00:00:00 GMT';

   document.cookie = name + "=" + escape(value)
      + ((expires == null) ? "" : ("; expires=" + expires))
      + ((path == null) ? "" : ("; path=" + path))
      + ((domain == null) ? "" : ("; domain=" + domain))
      + ((secure == true) ? "; secure" : "");
}

function GetCookie(name) {
   var arg  = name + "=";
   var alen = arg.length;
   var clen = document.cookie.length;
   var i    = 0;

   while(i < clen) {
      var offset = i + alen;

      if(document.cookie.substring(i, offset) == arg) {
         var iEnd  = document.cookie.indexOf(";", offset);

         if(iEnd == -1) {
            iEnd = document.cookie.length;
         }

         return unescape(document.cookie.substring(offset, iEnd));
      }

      i = document.cookie.indexOf(" ", i) + 1;

      if(i == 0) {
         break;
      }
   }

   return null;
}

function windowHeight() {
    var de = document.documentElement;
    return self.innerHeight || ( de && de.clientHeight ) || document.body.clientHeight;
}

function windowWidth() {
    var de = document.documentElement;
    return self.innerWidth || ( de && de.clientWidth ) || document.body.clientWidth;
}

function ajblock(url, name)
{
	$.post(url, {name:name}, parse_json);
	return false;
}

nWindowHeight = false;
cpanelHeight = false;
ucpanelHeight = false;

//$.browser.msie
function CenterVertical (sObject, height) {
    if (height <= nWindowHeight) {
        $(sObject).css('position', 'fixed');
    }
    else {
        $(sObject).css('position', 'static');
    }
}

$(function() {

	set_form();

	$("a.fotoimg, p.img a").fancybox({ 'hideOnContentClick': true, 'overlayShow': false });

	$(document).ajaxStart(function(){
		$.fn.fancybox.showLoading();
	}).ajaxStop(function(){
		$("#fancy_loading").hide();
	});

	$('form.validate').validate({
			submitHandler: function(form) {
		        $(form).ajaxSubmit({ dataType:'xjson', success: parse_json });
	        }
	});

});

