// get substring count with limit
 ( function( $ ) 
 {
        $.dequeue = function( a , b )
        {
                return $(a).dequeue(b);
        };

 })( jQuery ); 


$.fn.substrCount = function(needle)
{
	var h = this.text();
	var times = 0;
	while((pos=h.indexOf(needle)) != -1)
	{
		h = h.substr(pos+needle.length);
		times++;
	}

	return times;
}


textareaResizer = function() 
{	
	$('textarea').each(function() {    
	var obj = $(this);
	
	cl = obj.attr("class");
	if(cl && -1 != cl.indexOf("noresize"))
	{
		return ;
	}
	
    var content = obj.text();
    var Height = 30;
    if(content.length)
    {
    	// IE - doesnt find \n I gave up I don't know why it is so ..
    	// Firefox works just as it must work as well as Opera
    	var times = obj.substrCount($.browser.msie ? "\r" : "\n");

    	if(times > 20)
    	{
    		Height = 200;
    	}
    	else
    	{
    		Height = 70+10*times;
    	}
    }


	obj.height(Height);

    var offset = null;
	
	
	
    $(this).wrap('<div class="resizable-textarea"></div>').parent().append($('<div class="resizable-textarea2" style="width: '+(obj.width()+10)+'px;"></div>').bind("mousedown", dragBegins));
	  
	$("textarea [@class=checkbox]").bind("mousedown", dragBegins);
   
    $('div.resizable-textarea2', $(this).parent()).bind("mousedown", dragBegins);    

	var image = $('div.resizable-textarea2', $(this).parent())[0];
    
    //$(image).width(obj.width());

    function dragBegins(e)
    {
      offset = obj.height() - getMousePosition(e).y;
      if($.browser.opera)
      {
      	offset -= 6;
      }
      $(document).bind("mousemove", doDrag).bind("mouseup", dragEnds);
      stopPropagation(e);
    }

    function doDrag(e)
    {
      obj.height(Math.max(15, offset + getMousePosition(e).y) + 'px');
      stopPropagation(e);
    }

    function dragEnds(e)
    {
		$(document).unbind();
    }
  });
}

function getMousePosition(e)
{
	return e.pageX ? {'x':e.pageX, 'y':e.pageY} : {'x':e.clientX + (document.documentElement ? document.documentElement.scrollLeft : document.body.scrollLeft), 'y':e.clientY + (document.documentElement ? document.documentElement.scrollTop : document.body.scrollTop)};
};
function stopPropagation(ev)
{
	ev = ev||event;/* get IE event ( not passed ) */
	ev.stopPropagation? ev.stopPropagation() : ev.cancelBubble = true;
}


$(document).ready(init_design);


function init_design()
{   
    
 	//$("table.tab tr:even").addClass("red");
  $("table.tab tr").hover(function() {
    $(this).addClass("hover");
  },function(){
    $(this).removeClass("hover");
  });

  $("table.statistics tr").hover(function() {
    $(this).css("background-color","#d9d9d9");
  },function(){
    $(this).css("background-color","");
  });

  
  $("input").not("[type=checkbox],[type=radio]").focus(function(e) {
    $(this).addClass("hover");
  });
  
  $("input").not("[type=checkbox],[type=radio]").blur(function() {
    $(this).removeClass("hover");
  });
  
  $("textarea").focus(function() {
    $(this).addClass("hover");
  });
  
  $("textarea").blur(function() {
    $(this).removeClass("hover");
  });

  //$('div.block_error').pause(6000).fadeTo(500,0);

  $('div.tooltip').css('opacity', 0.1);
  $('img.tip')
		.bind(
			'mouseover',
			function()
			{
				$('div.tooltip').stopAll().pause(300).fadeTo(400,1);
			}
		)
		.bind(
			'mouseout',
			function()
			{
				$('div.tooltip').stopAll().fadeTo(400,0, function(){$(this).hide();});
			}
		);

 


}




var first=true;
var prev=false;


function select_tab(top, height, id)
{

  if(first)
  {
    first=false;
    $("#test").css("top", top);
    $("#test").css("height", height);
    $("#test").css("display", "block");
    $("#cursor_"+id.substr(3)+"_1").css("visibility", "visible");
    var a=$("#cursor_"+id.substr(3)+"_1").parent().children()[1];
    $(a).addClass("over");
    prev="#cursor_"+id.substr(3)+"_1";
  }
  else
  {
  $('#center').block(); 
  $('#block_msg').css('opacity',0.5);
  
    $("#test").animate({top: top, height:height}, 500, "easein", 
		function()
		{			
			if(prev)
			{
				$(prev).css("visibility", "hidden");
				var a=$(prev).parent().children()[1];
				$(a).removeClass("over");
			}
			
			$("#cursor_"+id.substr(3)+"_1").css("visibility", "visible");
			var a=$("#cursor_"+id.substr(3)+"_1").parent().children()[1];
			$(a).addClass("over");
			prev="#cursor_"+id.substr(3)+"_1";
			
			
			
			$('#center').unblock();
		}
					);
  }
}
