/**
 *	PineTree CMS *tm
 *
 */
$(document).ready(function(){
		$.ajaxSetup({
		    beforeSend: function (request) {
		      request.setRequestHeader('Accept', 'application/html+ajax');
		    }
		  })
		$.fn.alignCenter = function() {
	      //get margin left
	      var marginLeft =  ($(window).width()/2)-50 + 'px';
	      //get margin top
	      var marginTop =  $(window).height()-200 + 'px';
	      //return updated element
	      return $(this).css({'left':marginLeft, 'top':marginTop});
		};

		$.fn.center = function(w, h) {
			//if(w==0) w = 200;
			if(h==0) h = 300;
			page_size = getPageSize();
			this.css("top",		getBodyScrollTop() + (h/2) +  "px");
			this.css("left",	Math.round(page_size['windowWidth']/2) +  "px");
			return this;
		}

	// POPUP VIA AJAX (faster than any plugin)
	// todo: more complex defining of the screen center
	// used: fancy plugin css
	$("#ajax-popup").click(function(){
		var top = $(window).scrollTop()+200;
		//$(".popup").css("top",top+'px');
		var ref = $(this).attr("href");
		var title = $(this).attr("title");
		if(ref!=''&&ref!=undefined)
		{
			if(title!='') title = '<div class="popup-title corner-top">'+title+'</div>';
			$(".popup").html('<div class="popup-close"></div>'+title+'<div class="popup-content"></div><div id="fancy_bg"><div class="fancy_bg" id="fancy_bg_n"></div><div class="fancy_bg" id="fancy_bg_ne"></div><div class="fancy_bg" id="fancy_bg_e"></div><div class="fancy_bg" id="fancy_bg_se"></div><div class="fancy_bg" id="fancy_bg_s"></div><div class="fancy_bg" id="fancy_bg_sw"></div><div class="fancy_bg" id="fancy_bg_w"></div><div class="fancy_bg" id="fancy_bg_nw"></div></div>');
			$(".popup-content").load(ref);
			//$(".popup").animate({height:$(".popup").height+"px"}, 500,function(){ });
			$(".popup").show();
			$(".popup-close").bind("click", function(){$(".popup").hide();});
		}
		return false;
	});

	$("div.sub").parent().append("<span></span>");
    $("ul.menu li").mouseover(function() {
		if($(this).find("div.sub").attr("id")=='selected-mnu') {			return false;
		}
        $(this).find("div.sub").slideDown('fast').show();

        $(this).hover(function() {
        }, function(){
            $(this).find("div.sub").slideUp('fast');
        });

        }).hover(function() {
            $(this).addClass("subhover");
        }, function(){
            $(this).removeClass("subhover");
    });
});

// FOO function
function afterAjax(){return true;}

function tb_parseQuery ( query ) {
   var Params = {};
   if ( ! query ) {return Params;}// return empty object
   var Pairs = query.split(/[;&]/);
   for ( var i = 0; i < Pairs.length; i++ ) {
      var KeyVal = Pairs[i].split('=');
      if ( ! KeyVal || KeyVal.length != 2 ) {continue;}
      var key = unescape( KeyVal[0] );
      var val = unescape( KeyVal[1] );
      val = val.replace(/\+/g, ' ');
      Params[key] = val;
   }
   return Params;
}


function detectIE()
{
 var agent = navigator.userAgent.toLowerCase();
 var i = agent.indexOf('msie');

 if (i != -1) return parseFloat(agent.slice(i+5))
 return false;
}
ie = detectIE();

function show_picture(gid, id, f, c, w, h, color, title)
{ var size="width="+w+",height="+h; picture = window.open("/includes/misc/popup_img.php?gid="+gid+"&id="+id+'&f='+f+"&c="+c+"&col="+color+"&tit="+title, "picture", "dependent=yes,status=yes,directories=no,menubar=no,scrollbars=yes,resizable=yes,"+size);
}

function PopUp(path,W,H)
	{
	W+=0;
	H+=0;
	var size="width="+W+",height="+H;
	window.open("/includes/popup.php?path="+path,"","dependent=yes,status=no,scrollbars=no,resizable=yes,"+size);
	}

function  getPageSize(){
	var xScroll, yScroll;

	if (window.innerHeight && window.scrollMaxY) {
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else if (document.documentElement && document.documentElement.scrollHeight > document.documentElement.offsetHeight){ // Explorer 6 strict mode
		xScroll = document.documentElement.scrollWidth;
		yScroll = document.documentElement.scrollHeight;
	} else { // Explorer Mac...would also work in Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}

	var windowWidth, windowHeight;
	if (self.innerHeight) { // all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}

	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else {
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}

	var out;
	out = new Array();
	out['windowWidth'] = windowWidth;
	out['windowHeight'] = yScroll;

	return out;
}
function getBodyScrollTop()
{
  return self.pageYOffset || (document.documentElement && document.documentElement.scrollTop) || (document.body && document.body.scrollTop);
}
/* form */
function checkLength(o,n,min,max) {
        		if ( o.val().length > max || o.val().length < min ) {
				o.addClass('state-error');
				o.parent().append('<span class="state-error-text">Поле ' + n + ' должно содержать  от '+min+' до  '+max+' символов.</span>');
				return false;
					} else {
						return true;
					}
				}
				function checkRegexp(o,regexp,n) {
				if ( !( regexp.test( o.val() ) ) ) {
					o.addClass('state-error');
					o.parent().append('<span class="state-error-text">'+n+'</span>');
					return false;
				} else {
					return true;
				}
				}
				function checkEqual(a,b,n)
				{
					if(a.val()!=b.val())
					{
						a.addClass('state-error');
						a.parent().append('<span class="state-error-text">'+n+'</span>');
						return false;
					} else {
					return true;
					}
					return true;
				}
