
function strrchr (haystack, needle) {
	// http://kevin.vanzonneveld.net
	// +   original by: Brett Zamir (http://brett-zamir.me)
	// +   input by: Jason Wong (http://carrot.org/)
	// +   bugfixed by: Brett Zamir (http://brett-zamir.me)
	// *     example 1: strrchr("Line 1\nLine 2\nLine 3", 10).substr(1)
	// *     returns 1: 'Line 3'

	var pos = 0;

	if (typeof needle !== 'string') {
		needle = String.fromCharCode(parseInt(needle, 10));
	}
	needle = needle.charAt(0);
	pos = haystack.lastIndexOf(needle);
	if (pos === -1) {
		return false;
	}

	return haystack.substr(pos);
}

function formatTing(lenCSS, num, minVal, maxVal){

	var stepCSS = Math.round(lenCSS/num);
	var stepView = Math.round((maxVal - minVal)/num);
	var resHTML = '';

	for (i=1; i<num;i++){
		resHTML+= '<div class="formty" style="left: ' + stepCSS*i + 'px"><span>' + (minVal + stepView*i) +  '</span></div>';
	}

	$("#slider-range .formatt").append(resHTML);
	$("#slider-range .formatt").each(function(){
		$("span", this).css("left", "-" + $("span", this).width()/2 + "px");
	});
}

$(document).ready(function(){

	if ( $.browser.msie ) {
		if ( parseInt($.browser.version, 10) == 6){

			$( "#dialog:ui-dialog" ).dialog( "destroy" );
		
			$( "#dialog-message" ).dialog({
				modal: true,
				buttons: {
					Ok: function() {
						$( this ).dialog( "close" );
					}
				}
			});

		}
	}

// menu start

	//$('.nav').droppy();
	//$('#nav2').droppy2();

	var options = {
		speed: 150
		//delay: 150
	};

	var submenuOptions = {
		interval: 10,
		sensitivity: 5,
		over: showSubmenu,
		timeout: 100,
		out: hideSubmenu
	}; 

	var submenuOptions2 = {
		interval: 10,
		sensitivity: 5,
		over: showSubmenu2,
		timeout: 100,
		out: hideSubmenu2
	}; 

	function showSubmenu(){

		$('> .nav ul', this).slideDown(options.speed).css('cursor', 'default');
	}

	function hideSubmenu(){

		$('> .nav ul', this).slideUp(options.speed);
	}

	function showSubmenu2(){

		$('.regbl', this).slideDown(options.speed).css('cursor', 'default');
	}

	function hideSubmenu2(){

		$('.regbl', this).slideUp(options.speed);
	}

	$('.menu .mtab .m1').hoverIntent(submenuOptions);

	$('#cccy').hoverIntent(submenuOptions2);

	$('.menu .mtab .m1').hover(
		function(){
			$(this).css({
				'background': '#fff500 url(/i/menu/menu_top_over_bg.png) repeat-x 0 -2px',
				'cursor': 'pointer'
			});

			$('> ul li a', this).css({
				'color': '#008000'
			});

		},function(){
			$(this).css({
				'background': 'none'
			});

			$('> ul li a', this).css({
				'color': '#fff'
			});
		}
	);

/*
		$('.menu .mtab .m1').hover(
			function(){
				$(this).css({
					'background': '#fff500 url(/i/menu/menu_top_over_bg.png) repeat-x 0 -2px',
					'cursor': 'pointer'
				});

				$('> ul li a', this).css({
					'color': '#008000'
				});

				var elem = this;
				setTimeout(function(){
					$('> .nav ul', elem).slideDown(options.speed);
				}, options.delay);

			},function(){
				$(this).css({
					'background': 'none'
				});

				$('> ul li a', this).css({
					'color': '#fff'
				});

				var elem = this;
				setTimeout(function(){
					$('> .nav ul', elem).slideUp(options.speed);
				}, options.delay);
			}
		);
*/

	$('.nav ul ul li').mouseover(function(){
		$('> a', this).css('color', '#fff');
	});

	$('.nav ul ul li').mouseout(function(){
		$('> a', this).css('color', '#616161');
	});

	$('.menu .mtab .m1').mouseover(function(){

		var tab = $('.menu').offset();
		var elem = $(this).offset();

		var tabRight = Math.round(tab.left) + 920;
		var elemLi = Math.round(elem.left);
		var elemUl, sumWi;

		if ($('.onecol', this).size()) elemUl = $('.onecol', this).width();
		if ($('.twocol', this).size()) elemUl = $('.twocol', this).width();

		if (elemUl){
			sumWi = elemLi + elemUl;
			if (sumWi && sumWi > tabRight){
				$('> ul li ul', this).css({
					//'right': '-10px'
					'right': '-5px'
				});
			}else{
				$('> ul li ul', this).css({
					//'left': '-15px'
					'left': '-5px'
				});
			}
		}
		//alert(tabRight + ' --> ' + sumWi);
	});

// menu end

	$("a[rel=gapict-group]").fancybox({
		'padding': 2,
		'transitionIn': 'none',
		'transitionOut': 'none',
		'titlePosition': 'over',
		'overlayOpacity': 0.8,
		'overlayColor': '#fff',
		/*'titleFormat': function(title, currentArray, currentIndex, currentOpts) {
			return '<span id="fancybox-title-over">Image ' + (currentIndex + 1) + ' / ' + currentArray.length + (title.length ? ' &nbsp; ' + title : '') + '</span>';*/
		'titleFormat': function(title){
			return '<span id="fancybox-title-over">' + (title.length ? ' &nbsp; ' + title : '&nbsp;') + '</span>';
		}
	});

	$('div.clcl').toggle(
		function(){

			$(this).removeClass('blsss');
			$(this).next().next().hide(500);
			return false;

		},function(){

			$(this).addClass('blsss');
			$(this).next().next().show(500);
			return false;
		}
	);

	$('div.clffii').toggle(
		function(){

			$(this).addClass('arr2');
			$('div.ffii').hide(500);
			$('a', this).text('Развернуть фильтр');
			$('#debug1').load('/filter_close.php?close=1');
			return false;

		},function(){

			$(this).removeClass('arr2');
			$('div.ffii').show(500);
			$('a', this).text('Свернуть фильтр');
			$('#debug1').load('/filter_close.php?close=0');
			return false;
		}
	);

	$('div.nbcnav div.clcl2').click(function(){

		if ($('a', this).hasClass("tn")) return false;
		else{
			$('div.nbcnav div.clcl2 a').removeClass("tn");
			$('a', this).addClass("tn");

			var clbl = "cglist" + $(this).attr("id").substr(3);

			$("div.nbcnav div").each(function(){
				if ($(this).hasClass('clcl2')){
					$(this).removeClass('blsub').addClass('blsub2');
				}
			});
			$(this).addClass('blsub').removeClass('blsub2');
			if (!$(this).is(':first-child')) $(this).parent().next().addClass('blinfonc');
			if ($(this).is(':first-child')) $(this).parent().next().removeClass('blinfonc');

			//if (!$(this).is(':first-child')) $('.dinfo').addClass('blinfonc');
			//if ($(this).is(':first-child')) $('.dinfo').removeClass('blinfonc');

			$("div.blinfo div").each(function(){
				if ($(this).attr("id").substr(0,6) == 'cglist'){
					$("#" + $(this).attr("id")).css("display","none");
				}
			});
			$("#" + clbl).css("display","block");
			return false;
		}
	});

/*
	Array.max = function( array ){
		return Math.max.apply( Math, array );
	};

	Array.min = function( array ){
		return Math.min.apply( Math, array );
	};
*/

	function homepageBlocks(){

		var elem = $(".xxss"), size = $("div.[class^='xxssbl']", elem).size(), obj = {}, arr = [], i = j = 0, key = 1, id, max, hi;
/*
		$(".xxssbl", elem).each(function(){
			if (i < 3){
				arr[i] = $(this).height();
			}else{
				obj[key] = arr;

				arr = [];
				i = 0;
				key++;

				arr[i] = $(this).height();
			}
			i++;
			j++;

			if (size == j) obj[++key] = arr;
		});
*/

		$("div[class^='xxssbl']", elem).each(function(){

			// determine max height
			/*if (j == 0){
				max = $(this).height();
			}else{
				if (max < $(this).height()) max = $(this).height();
			}*/

			// form ojbect with (class: value) rowwise
			if (i < 3){
				//arr[i] = $(this).height();
				id = $(this).attr("class").split(" ")[0];
				arr[i] = { id : id, val : $(this).height() - 10 };
			}else{
				obj[key] = arr;

				arr = [];
				i = 0;
				key++;

				//arr[i] = $(this).height();
				id = $(this).attr("class").split(" ")[0];
				arr[i] = { id : id, val : $(this).height() - 10 };
			}
			i++;
			j++;

			if (size == j) obj[++key] = arr;
		});

		//console.log(obj);
		//console.log(max);

		// set max height for blocks in every line
		if (!$.isEmptyObject(obj)){
			for (var k in obj){

				// determine min/max height
				max = 0;
				for (var k2 in obj[k]){
					if (obj[k][k2]["val"] > max) max = obj[k][k2]["val"];
				}

				// max height set for the block in the line
				hi = max - 175;
				for (var k2 in obj[k]){

					id = obj[k][k2]["id"];
					$("div." + id + " div.gin", elem).css({
						"height": max + "px",
						"min-height": max + "px"
					});

					$("div." + id + " div.info", elem).css("height", hi + "px");
				}
			}
		}
	}

	function homepageBlocks2(){

		var /*elem = $(".xxee"),*/ size = $("table.ctab5").size(), obj = {}, arr = [], i = j = 0, key = 1, id, max;

		$("table.ctab5 .cc2 div[class^='xxeebl']").each(function(){

			// form ojbect with (class: value) rowwise
			if (i < 3){
				//arr[i] = $(this).height();
				id = $(this).attr("class").split(" ")[0];
				arr[i] = { id : id, val : $(this).height() };
			}else{
				obj[key] = arr;

				arr = [];
				i = 0;
				key++;

				//arr[i] = $(this).height();
				id = $(this).attr("class").split(" ")[0];
				arr[i] = { id : id, val : $(this).height() };
			}
			i++;
			j++;

			if (size == j) obj[++key] = arr;
		});

		//console.log(obj);

		// set max height for blocks in every line
		if (!$.isEmptyObject(obj)){
			for (var k in obj){

				// determine min/max height
				max = 0;
				for (var k2 in obj[k]){
					if (obj[k][k2]["val"] > max) max = obj[k][k2]["val"];
				}

				// max height set for the block in the line
				for (var k2 in obj[k]){

					id = obj[k][k2]["id"];
					$("table.ctab5 .cc2 div." + id).css({
						"height": max + "px"
					});
				}
			}
		}
		//console.log(max);

	}

	homepageBlocks();
	homepageBlocks2();

/*
		$("a.prw").live("click", function(){
			var hrf = $(this).attr("href");
			var idx = $(this).attr("id").substr(5);
			var fnm = strrchr(hrf, "/");
			var nfnm = "/m" + fnm.substr(2);

			hrf = hrf.replace(fnm, nfnm);
			$("#opuppict img").attr("src", hrf);
			$("#opuppict").attr("rel", "gapict-group_" + idx);

			return false;
		});
*/
/*
		$("a.prw").live("click", function(){
		//$("a.prw").click(function(){
			var hrf = $(this).attr("href");
			var idx = $(this).attr("id").substr(5);
			var ext = strrchr(hrf, ".");
			//var fnm = strrchr(hrf, "/");
			var hrf = hrf.replace(ext, "") + "_thumb" + ext;

			$("#opuppict img").attr("src", hrf);
			$("#opuppict").attr("rel", "gapict-group_" + idx);

			return false;
		});

		$("#opuppict").live("click", function(){
		//$("#opuppict").click(function(){

			var str = $(this).attr("rel");
			inx = str.substr(13);

			$("#_oppt_" + inx).trigger('click');

			return false;
		});

		$("#ankt").click(function(){
			$("#anktbl").toggle("slow");
			return false;
		});

		$(".slider td").mouseover(function(){
			if (!$(this).attr("class") && $("a", this).size() > 0){
				$(this).css({
					backgroundImage: "url(/i/slider_bg2.png)",
					cursor: "pointer"
				});
			}
		});

		$(".slider td").mouseout(function(){
			if (!$(this).attr("class") && $("a", this).size() > 0){
				$(this).css({
					backgroundImage: "url(/i/slider_bg.png)"
				});
			}
		});

		$(".tdata2 .dd3 .goo").mouseover(function(){
			$(this).css({
				border: "1px solid #eb193c",
				cursor: "pointer"
			});
			$(".go", this).css({
				backgroundColor: "#fff",
				color: "#eb193c"
			});
		});

		$(".tdata2 .dd3 .goo").mouseout(function(){
			$(this).css({
				border: "1px solid #ccc",
				cursor: "pointer"
			});
			$(".go", this).css({
				backgroundColor: "#eb193c",
				color: "#fff"
			});
		});

*/

/*
		$("#toffred").click(function(){
			$("#tonred").attr("checked", "");
			$("#red input:checkbox").each(function(){
				$(this).attr("checked", "");
			});
			return false;
		});


    $("li").toggle(
      function () {
        $(this).css("list-style-type", "disc")
               .css("color", "blue");
      },
      function () {
        $(this).css({"list-style-type":"", "color":""});
      }
    );
*/

/*
		$(".dlist li ul").slideUp();
		$(".dlist li a").click(function(){

			var cElem = $(this).next("ul");
			console.log($(this).next("ul"));

			if(!$(this).data('flag')){
				$(cElem).slideDown();
				$(this).data('flag', 1)
			}else{
				$(cElem).slideUp();
				$(this).removeData('flag');
			}
			return false;
		});
*/
/*
		var flag = false;
		$(".dlist li a").click(function(){

			var cElem = $(this).next("ul");

			if(!flag){
				$(cElem).slideDown();
				flag = true;
			}else{
				$(cElem).slideUp();
				flag = false;
			}
			return false;
		});*/

});


function number_format( number, decimals, dec_point, thousands_sep ) {  // Format a number with grouped thousands
    var i, j, kw, kd, km;
    if( isNaN(decimals = Math.abs(decimals)) ){
	decimals = 2;
    }
    if( dec_point == undefined ){
	dec_point = ",";
    }
    if( thousands_sep == undefined ){
	thousands_sep = ".";
    }
    i = parseInt(number = (+number || 0).toFixed(decimals)) + "";
    if( (j = i.length) > 3 ){
	j = j % 3;
    } else{
	j = 0;
    }
    km = (j ? i.substr(0, j) + thousands_sep : "");
    kw = i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + thousands_sep);
    kd = (decimals ? dec_point + Math.abs(number - i).toFixed(decimals).replace(/-/, 0).slice(2) : "");
    return km + kw + kd;
}


/*
function ankshow(){
	var elem = document.getElementById("anketa");

	if (elem.style.display == "block"){
		elem.style.display = "none";
		return false;
	}
	elem.style.display = "block";
	return false;
}

*/
