$(function(){

	/******** MENU SELECT *******/

	$('nav li').not('.vertical').hover(
		function(){ // mouseOver
			var block_id = 'menu_sel';

			// var new_width = parseInt($(this).width())+26+26; // - width menu
			var new_width = parseInt($(this).width())+16+16; // - short menu
			// var margin_left = -26; // - width menu
			var margin_left = -16; // - short menu

			var margin_top = -15;
			if ($(this).hasClass('left')){
				block_id = 'menu_sel_left';
				//new_width = new_width - 21; // - width menu
				new_width = new_width - 12; // - short menu
				margin_left = -5;
			}
			if ($(this).hasClass('selected') && $('.'+block_id,this).length > 0) return;
			if ($(this).hasClass('selected')) $(this).prepend($('.'+block_id).clone().addClass('notouch'));
			else $(this).prepend($('body .'+block_id+':not(.notouch):first'));
			$('.'+block_id,this).css({
				'display':'block',
				'width':new_width+'px',
				'margin-left':margin_left+'px',
				'margin-top':margin_top+'px'
			});
		},
		function(){ // mouseOut
			if ($(this).hasClass('selected')) return;
			var block_id = 'menu_sel';
			if ($(this).hasClass('left')){
				block_id = 'menu_sel_left';
			}
			$('.'+block_id,this).css({
				'display':'none'
			});
		}
	);

    $('nav li').not('.vertical').click(function(){
    	window.location = $('a',this).attr('href');
    });
    $('nav li.selected').mouseover();

	/********* CHECKBOXES **************/

    $('input[type=checkbox]').beautifulCheckbox();

	/********* Control Blocks ***********/

    $('.control').click(function(){
    	var num = this.id.substr(8);
    	var block_id = '#hBlock_'+num;
    	if($(block_id).is(':visible')){
    		$(block_id).hide('fast','swing');
    		$('.cText',this).text('Показать');
    	}else{
    		$(block_id).show('fast','swing');
    		$('.cText',this).text('Скрыть');
    	}
    });

	/*
	$('.control').mouseover(function(){
		$('.cText',this).animate({ "margin-top": "-=3"}, "fast" ).animate({ "margin-top": "+=3"}, "fast" )
	});
	*/


	/******** FancyBox *********/

	$(".fb").fancybox({
		'transitionIn'	:	'elastic',
		'transitionOut'	:	'elastic',
		'speedIn'		:	600,
		'speedOut'		:	200,
		'overlayShow'	:	true
	});

	// search form

	$('#search_inp').focus(function(){
		if ($(this).val()==' Поиск') $(this).val('');
	});

	$('#search_inp').focusout(function(){
		if ($(this).val()=='') $(this).val(' Поиск');
	});

	// medcat type select

	$('#mt').change(function(){
	    var loc = document.location.toString();
	    loc = loc.replace(/p\/\d+\//,'');
	    loc = loc.replace(/mt\/\d+\//,'');
	    if ($(this).val()){
		    loc = loc + 'mt/' + $(this).val() + '/';
		}
		document.location = loc;
	});

	// medInfo show

	$('.medItemLink').click(function(){
		var id = $(this).attr('href').replace(/.*?\/mi\/(.*)?\/.*/,'$1');
		if($('#medItemInfo_'+id).is(':visible')){
			$(this).css({'text-decoration':'none','font-weight':'normal'});
			$('#medItemInfo_'+id).hide('fast');
		}else{
			$(this).css({'text-decoration':'underline','font-weight':'bold'});
			$('#medItemInfo_'+id).show('fast');
		}
		//$('#medItemInfo_'+id).toggle('slow');
		return false;
	});

	// print version

	$('#printv').click(function(){
		window.print();
		return false;
	});

	// VOTE

	$("ul.setRate li").mouseenter(function() {
		$(this).css({'background':'#C5E77C'});
	}).mouseleave(function() {
		$(this).css({'background':'#fff'});
	});

	$("ul.setRate li").click(function(){
    	var rate = $(this).attr('class').substr(1);
    	var type_id = $(this).parent().attr('class').replace(/setRate /,'').split('_');
    	var type = type_id[0];
    	var id = type_id[1];
    	var ratePlace = type_id[2];

    	$.post('/ajax.php',{'event':'vote','rate':rate,'type':type,'id':id},function(result_){
    		if (ratePlace){
    			$('#'+ratePlace).html(result_);
    		}
    		$('#voteBlock_'+id).hide();
    		$('#voteBtn_'+id).html('Спасибо! Ваш голос очень важен для нас.');
    	});
	});

	// Send Comment

	$('#medcomment_form').submit(function(){
		var id = $('input[name="id"]',this).val();
		var name = $('input[name="name"]',this).val();
		var text = $('textarea[name="text"]',this).val();
		var code = $('input[name="code"]',this).val();
		var key = $('input[name="key"]',this).val();

		if (!id || !name || !text || !code){
			alert('Заполните все поля.');
		}else{
	    	$.post('/ajax.php',{'event':'med_comment','id':id,'name':name,'text':text,'code':code,'key':key},function(result_){
				var res = result_.res;
				var err_text = result_.err_text;
				if (res=='ok'){
     				$('#medcomment_form').hide('fast',function(){
	     				$('#medcomment_res').text('Спасибо за Ваш отзыв. После проверки Вашего отзыва модератором, он будет доступен для всех.');
     				});
				}else{
					alert(err_text);
				}
        	});
		}
		return false;
	});

	$('.add_medcomment').click(function(){
		$(this).parent().hide();
		$('.medcomment_block').show('slow');
		return false;
	});


});

function showMedCommentForm(tr){
	$(tr).parent().hide();
	$('.medCommentFormBlock').show('fast',function(){
		$('#control_comments').click();
		$('.add_medcomment').click();

	});
	return false;
}

$.fn.beautifulCheckbox = function(){

	$(this).each(function(){
	var cb = this;
	var elem = $("<div>").addClass('beautifulCheckbox').attr('id',$(cb).attr('id')+'_b').click(function(){
		if ($(cb).is(':checked')){
			$(cb).removeAttr('checked');
			$(this).removeClass('checked');
		}else{
			$(cb).attr('checked','checked');
			$(this).addClass('checked');
		}
	});
	if ($(cb).is(':checked')) $(elem).addClass('checked');
	if ($('label[for='+$(cb).attr('id')+']')){
		$('label[for='+$(cb).attr('id')+']').click(function(){
			$(elem).click();
			return false;
		});
	}
	$(cb).after(elem).css('display','none');
	});

}

function show_(text){
	$('#info').css('display','block');
	$('#info').html($('#info').html()+text+'<br>');
}

function clear_(){
	$('#info').html('');
}

var popUps = Array();
function ShowHide(handler,anc,dx,dy,absx,absy,func)
{
	//dx=dx-70;
	//dy=dy-130;
		var inArray;
		element = document.getElementById(handler).style;
		if (element.display == "none" || !element.display){

if(anc)
{
 var obj=document.getElementById(handler);
 var obj1=document.getElementById(anc);
if(obj1){
 var ox,oy;
 for(ox=oy=0;obj1;obj1=obj1.offsetParent) ox+=obj1.offsetLeft, oy+=obj1.offsetTop;
 if(dx)ox=ox+dx;
 if(dy)oy=oy+dy;
 if(absx)ox=absx;
 if(absy)oy=absy;
 obj.style.top=oy+"px"; obj.style.left=ox+"px";
}else{

 pTop = document.documentElement.clientHeight>dy?document.documentElement.clientHeight-dy:0;
 obj.style.top=document.documentElement.scrollTop+(pTop)/2+"px";
 pLeft = document.documentElement.clientWidth>dx?document.documentElement.clientWidth-dx:0;
 obj.style.left=document.documentElement.scrollLeft+(pLeft)/2+"px";
	}
}

			element.display = "block";
			for (var i = 0; i <popUps.length; i++) {
				if (popUps[i].hwnd == handler){
					inArray = true;
 				}
			}
			if (!inArray) {
				popUps.push({hwnd:handler,func:func,status:true});
			}
			click = true;
		}
		else {
			element.display = "none";
			for (var i = 0; i <popUps.length; i++) {
				if (popUps[i].hwnd == handler){
					popUps[i].status= false;
 				}
			}
			click = true;
		}
	return false;
}
