$(document).ready(function(){
	var elem = $(window);

	// auto scaling catalog
	resizeCatalog();

	// tags size modify
	$.each( $(".tags A"), function() {
		$(this).css('font-size', parseInt(12 + 5*Math.random()) + 'px');
	});

	// paragraph columns delete last border
	$(".left P:last").css({'border-bottom':'none','margin':0,'padding':0});
	$(".right P:last").css({'border-bottom':'none','margin':0,'padding':0});

	// auth popup
	$(".link-enter").click(function(){
		openFloatForm('login');
	});

	// collection scrolling
	$(".coll-cont").mousemove( function(e) {
		var Div = $("#coll-cont"); 
		var lastDiv = $("#coll-move").find("div.collection:last-child");
		var divWidth = Div.width();
		var ulPadding = 15;
		var ulWidth = lastDiv[0].offsetLeft + lastDiv.outerWidth() + ulPadding;
		var left = (e.pageX - Div.offset().left) * (ulWidth-divWidth) / divWidth;
		Div.scrollLeft(left);
	});
	
	// search
	var toUseSearch = 0;
	$("#searchfld").focus(function () {
		toUseSearch = "searchfld";
		$("#searchfld").val("");	
	});
	$("#searchfld").blur(function () {
		toUseSearch = 0;
		if( $("#searchfld").val() == "" ) $("#searchfld").val("поиск..");	
	});
	$("#searchfld1").focus(function () {
		toUseSearch = "searchfld1";
	});
	$("#searchfld1").blur(function () {
		toUseSearch = 0;
	});
	$("#sbutton1").click(function () {
		search("searchfld1");
	});
	$("#sbutton").click(function () {
		search("searchfld");
	});
	$(document).keyup(function(event) {
		if (event.keyCode == 13 && toUseSearch != 0 ) {
			search( toUseSearch );
		}
	});
});

/**
 *	прототип на масштабирование
 */
	$(window).resize(function() {
		resizeCatalog();
	});

/**
 *	функция автомасштабирования блоков каталога
 */
	function resizeCatalog() {
		var w = $(window);
		var widthCat = $(".cinner").width()-2; //parseInt( 0.9 * 0.7 * w.width() ); //  90% * 70%
		var numCat = Math.floor( widthCat / 140 ); // 140 + 10(margin)
		var widthPosition = parseInt( (widthCat - 10*(numCat-1) - 2 * numCat )/numCat );
		var lackWidth = widthCat - widthPosition * numCat - 10*(numCat-1) - 2 * numCat;
		var n = 1;
		$.each( $(".sub"), function(i,val) {
			if( n == numCat ) {
				$(this).css( {'width' : (widthPosition + lackWidth - 20) + 'px', 'margin-right' : 0} );
				n = 0;
			} else {
				$(this).css( {'width' : (widthPosition - 20) + 'px', 'margin-right' : '10px'} );
			}
			n ++;
		});
	}

function openFloatForm(id) {
    var xy = getScrollXY();
    var elem = $(window);
    var floatX = (id == 'post') ? (( elem.width() - 800 ) / 2) : (( elem.width() - 260 ) / 2);
    var floatY =  (elem.height() / 2 - 100) + xy[1] ;
    $("#form-"+id).css( {'position':'absolute','top': floatY,'left':floatX,'z-index':'200' } );
    $("#form-shadow").dropShadow({left: 10, top: 10, opacity: 0.2, blur: 0});
    $("#form-"+id).css({'visibility':'visible'});
}

function closeFloatForm( id ) {
    $("#form-progress").html(" ");
    $("#form-" + id ).css({'visibility':'hidden'});
}
function getScrollXY() {
       var scrOfX = 0, scrOfY = 0;
       if( typeof( window.pageYOffset ) == 'number' ) {
               //Netscape compliant
               scrOfY = window.pageYOffset;
               scrOfX = window.pageXOffset;
       } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
               //DOM compliant
               scrOfY = document.body.scrollTop;
               scrOfX = document.body.scrollLeft;
       } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
               //IE6 Strict
               scrOfY = document.documentElement.scrollTop;
               scrOfX = document.documentElement.scrollLeft;
       }
       return [ scrOfX, scrOfY ];
}

function ShowHide(idname) {
	if (document.getElementById(idname).style.display == "none") {
		document.getElementById(idname).style.display = "block";
	} else {
		document.getElementById(idname).style.display = "none";
	}
}

function ModTree( image, id, area, selected, type ) {
	var ajax = new sack();
      	ajax.requestFile = "/project/phpfiles/_ajax.php";
	ajax.setVar("action", "modtree");	
	ajax.setVar("id", id);
	ajax.setVar("type", type);
	ajax.setVar("selected", selected);
       ajax.method = "GET"; 
      	ajax.element = "tree" + area + "" + id; 
	document.getElementById("tree" + area + "" + id).innerHTML = '<img src="/project/img/load.gif">';
       ajax.runAJAX();
	ShowHide("tree" + area +""+ id);
	//var imgsrc = document.getElementById("image"+id).src.toString();
	if( imagesrc = document.getElementById("image"+id) ) {
		imagesrc.src = (imagesrc.src.indexOf('close')==-1) ? "/_img/catalog/tree_close.gif" : "/_img/catalog/tree_open.gif";
		document.getElementById("min"+id).src = (image.src.indexOf('plus')==-1) ? "/_img/catalog/plus.gif " : "/_img/catalog/min.gif ";
	}
}


