function f1(x)
{
 x.focus();
 x.select();
};

var progress='<img src="/images/loader.gif" alt="обработка">';


function error(id,message){
    $(id+"_error").html(message);
    $(id).addClass("reg_error");
}

function unError(id){
    $(id+"_error").html("");
    $(id).removeClass("reg_error");    
}

function checkField(blur,field){
    var v_tmp=$(field).val();
    if (blur){
        if (v_tmp.length==0){
            error(field,"Поле не должно быть пустым");
            return false;
       //     alert(v_password.length); 
        }else{
            unError(field);
            return true;
        }        
    }else{
        if (v_tmp.length>0){
            unError(field);
        }        
    }

}

var show_utils="";
function exampleAdd(){
    tmp=$("#example").html();
    $("#stepplace").append(tmp);
    
}

function utilsHide(){
    if (show_utils!=""){
        $(show_utils).slideUp("normal");
        $("td[@foo=utils_menu]").removeClass("color2");             
    }
    show_utils="";
}

function utilsShow(id,menu,url,close){
    if (show_utils!=""){
        if (id==show_utils){
            if (close){
                utilsHide();
                $("td[@foo=utils_menu]").removeClass("color2");
            }
        }else{
            $("div[@foo=utils]").hide();
            $(id).show();
            $("td[@foo=utils_menu]").removeClass("color2");
            $(menu).addClass("color2");
            show_utils=id;
            if ($(menu).attr("load")=="true"){
                $.post(url,
                  { ajax: "true"},
                  function(data){
                    $(id).html(data);
                  }
                );
                $(menu).attr("load","false");
            }                                       
        }
      
    }else{
         $(id).slideDown("normal");    
         show_utils=id;
         $(menu).addClass("color2");
        if ($(menu).attr("load")=="true"){
            $.post(url,
              { ajax: "true"},
              function(data){
                $(id).html(data);
              }
            );
            $(menu).attr("load","false");
        }               
    }


    return false;
}


function getClientWidth()
{
  return document.compatMode=='CSS1Compat' && !window.opera?document.documentElement.clientWidth:document.body.clientWidth;
}

function getClientHeight()
{
  return document.compatMode=='CSS1Compat' && !window.opera?document.documentElement.clientHeight:document.body.clientHeight;
}

function selectExchange(from,to){
      var selected_index= $(from).get(0).selectedIndex;
      //alert(selected_index);
      if (selected_index>-1){
          var selected = $(from).children("option[@selected]");
          selected.appendTo(to);
          $(to).get(0).selectedIndex = $(to).get(0).options.length-1;
          var selected_length= $(from).get(0).options.length;
          if (selected_length>0){
              if (selected_index==selected_length){
                  $(from).get(0).selectedIndex=selected_index-1;
              }else{
                 $(from).get(0).selectedIndex=selected_index; 
              }
              
          }
      }
};

	$.easing.easeOutQuart = function (x, t, b, c, d) {
		return -c * ((t=t/d-1)*t*t*t - 1) + b;
	};

function showLarge(foto,num){
    $("#full_screen" + num).html('<img src="/images/blog/photo/' + foto + '.jpg" />');
    return false;
};

function doRecipesVote (n, sign)
		{
//			var n=$(this).attr("cId");
			$("#divRecipes"+n).html("");
			$.post(site_url + "/recipes/voteRecipes/" + n + "/",
//			  { ajax: "jaja", event:$(this).attr("event")},
		      { ajax: "jaja", event: sign},
			  function(data){
			      if (data=="error354"){
			          document.location.href= site_url + "/recipes/barrier/" + n + "/";
			          data = 'это барьер';
			      }
			      if (data=="error353"){
			          document.location.href= site_url + "/recipes/barrier2/" + n + "/";
			          data = 'это барьер';
			      } 		      
			      $("#divRecipes"+n).hide();
			      $("#resultRecipes"+n).html(data);
			  }
			);
			return false;
		};
		
		var nx;
		var useridx;
		
	function doRecipesVoteMinus (n, userid)
		{
			nx = n;
			useridx = userid;

			$('#recpdislike').css('display', 'block');
			$('#recpvote').css('display', 'none');
		};
		
	function countwords(str){
		var y=str;
		var r = 0;
		a=y.replace(/\s/g,' ');
		a=a.split(' ');
		for (z=0; z<a.length; z++) {if (a[z].length > 0) r++;}
		return r;
	}
		
	function doRecipesVoteMinusSubmit()
	{
		//	var n=$(this).attr("cId");
		if (countwords($('#repdislikemes').val())  < 3)  {
			alert('Слишком короткая причина!');
			return false;
		}
		
	
	
			$("#divRecipes"+nx).html("");
			$.post(site_url + "/recipes/voteRecipes/" + nx + "/",
//			  { ajax: "jaja", event:$(this).attr("event")},
		      { ajax: "jaja", event: '-', userid:useridx, message:$('#repdislikemes').val()},
			  function(data){
			      if (data=="error354"){
			          document.location.href= site_url + "/recipes/barrier/" + nx + "/";
			          data = 'это барьер';
			      }
			      if (data=="error353"){
			          document.location.href= site_url + "/recipes/barrier2/" + nx + "/";
			          data = 'это барьер';
			      }
			      $("#divRecipes"+nx).hide();
			      $("#resultRecipes"+nx).html(data);
			  }
			);
			
		$('#recpdislike').css('display', 'none');
		$('#recpvote').css('display', 'block');
		
			return false;
	}
	
	function doRecipesVoteMinusCancel()
	{
		$('#recpdislike').css('display', 'none');
		$('#recpvote').css('display', 'block');
	}
	
	
function init(){

    $('form[@foo=voteForm]').ajaxForm({
    beforeSubmit: function(formArray) {
    	vote_id = formArray[0].value;
    	if (formArray.length > 1){
    	    $('#voteForm'+vote_id).hide();
        	$('<div id="busy">' + progress + '</div')
            .ajaxStart(function() {$(this).show();})
            .ajaxStop(function() {$(this).hide();})
            .appendTo('#vote_main'+ vote_id);
        }else{
            $("#vote_error"+vote_id).html("Не выбран вариант ответа");
            return false;
        }
    },
        success: function(data) {
            $('#voteDiv'+vote_id).html(data);            
            $('#voteDiv'+vote_id).show('slow');
            vote_id=0;
            return false;

        }
    });
	$("a[@foo=blog_vote]").click(function()
	{
		var n=$(this).attr("cId");
		$("#divBlog"+n).html("");
		$.post(site_url + "/blog/voteBlog/" + n + "/",
		  { ajax: "jaja", event:$(this).attr("event")},
		  function(data){
		  $("#divBlog"+n).hide();
		    $("#resultBlog"+n).html(data);
		  }
		);
		return false;
	});

	$("div.s_screen").each(function(i){
    	$('#slideshow'+$(this).attr("jid")).serialScroll({
    		items:'li',
    		prev:'a.prev' + $(this).attr("jid") +'',
    		next:'a.next' + $(this).attr("jid") +'',
    		axis:'x',
    		offset:-130,
    		start:1,
    		duration:1000,
    		force:true,
    		stop:true,
    		lock:false,
    		cycle:false, 
    		easing:'easeOutQuart', 
    		jump: true 
    	});	    
 
	});

  $('a[rel*=facebox]').facebox({
    loading_image : site_url + '/facebox/loading.gif',
    close_image   : site_url + '/facebox/closelabel.gif'
  })
  $('a[rel*=lightbox]').facebox({
    loading_image : site_url + '/facebox/loading.gif',
    close_image   : site_url + '/facebox/closelabel.gif'
  })    
}

function getPage(url,place,anchor){
    if (anchor) {
      window.location.hash = '0';
      window.location.hash = anchor;
    }
    place.html(progress);
		$.post(url.href,
		  { ajax: "jaja"},
		  function(data){
		      place.html(data);
		      init();
		  }
		);
		return false;    
}

$(document).ready(function(){

	
    init();
    
    
    $('form[@foo=invite]').ajaxForm({
    beforeSubmit: function(formArray) {
    	$("#invite_submit").val("обработка").attr('disabled','disabled');
    },
        success: function(data) {
            $('#invite_div').html("Cпасибо, приглашение было отослано");

        }
    });

          
    
});

function checkFeed(f) {

	var bad_words = ['спасиб', 'благодар', 'праздник'];
	
	var sx = f.body.value.toLowerCase();

	if (sx.match(new RegExp(bad_words.join('|'), 'm'))) {
		alert('Слова благодарности или поздравления Вы можете оставить на личной стене поварёнка');
		$('#feed_tip').css({color: '#F00', fontSize: '14px', paddingTop: '10px'});
		return false;
	}
	
	if(f.body.value==''){alert('Напишите сам отзыв пожалуйста '); f.body.focus(); return false;}else if(!f.agreement.checked){alert('Вы обязаны согласиться с правилами добавления отзывов');return false;}
	
}

