/**
 *	PineTree CMS *tm
 *	@author	Kirill I Sosnin kirill@sosnin.spb.ru
 *
 *	License: GPL
 *
 */
$(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;
		}

		$.fn.extend({
			reset: function() {
				$(this).find(":input").not(':button, :submit, :reset, :hidden').val('').removeAttr('checked').removeAttr('selected');
				//$(this).bind("submit");
				$(this).submit();
				return true;
			},
			submit: function() {
				return this.each(function() { $(this).is('form') && this.submit(); });
			}
		});

		$().ajaxStart(function(){
		    $(".load").show();
		}).ajaxStop(function() {
		    $(".load").hide();
		});

		// zoom of image
		$(".catalog-image").click(function(){
			//page_size = getPageSize();

			src = "/catalog_images/"+$(this).attr("id")+".jpg";
			shade();
			modal = document.getElementById('photo');

			Loaded = 0;
			oImage = new Image;
			oImage.src = src;

			//scrollTo(0,100);
			$("#photo").html('<div class="loading">Фотография загружается.</div>');
			$("#photo").center(oImage.width, oImage.height);

			checkLoad();

			modal.className = '';
		});
		$(".zoom-image").click(function(){
		var src;
		var tit;
		var iid;
		var that; var h; var w;
		that = $(this);
		tit = $(this).attr("alt");
		src = $(this).attr("src");
		iid = $(this).attr("id");
		oImage = new Image;
		oImage.src = src;

		if(oImage.complete){
			h = ((ie>0)?(oImage.height+40):(oImage.height+10));
			w = ((ie>0)?(oImage.width+30):(oImage.width+5));
			$('<div id="zoom-'+iid+'" class="modal"><div><img src="'+src+'" border="0" alt=""></div></div>').insertAfter(that);
        	tb_show(tit,'#TB_inline?ajax=1&amp;height='+h+'&amp;width='+w+'&amp;inlineId=zoom-'+iid);
		}
        return false;
    });

	$(".corner,.corner-top,.corner-tab-content").wrapInner('<div class="corner-content">');
	$(".corner, .corner-top").prepend('<b class="b1"></b><b class="b2"></b><b class="b3"></b><b class="b4"></b>');
  	$(".corner,.corner-tab-content").append('<b class="b4"></b><b class="b3"></b><b class="b2"></b><b class="b1"></b>');

	// 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;
	});


});

// 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 show_hide(form_id) {
if(document.getElementById(form_id))
{
	if(document.getElementById(form_id).style.display == "block")
	{
		document.getElementById(form_id).style.display = "none";
	}
	else
	{
		document.getElementById(form_id).style.display = "block";
	}
}
}
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);
}

