jQuery.cookie=function(name,value,options){if(typeof value!='undefined'){options=options||{};if(value===null){value='';options.expires=-1}var expires='';if(options.expires&&(typeof options.expires=='number'||options.expires.toUTCString)){var date;if(typeof options.expires=='number'){date=new Date();date.setTime(date.getTime()+(options.expires*24*60*60*1000))}else{date=options.expires}expires='; expires='+date.toUTCString()}var path=options.path?'; path='+options.path:'';var domain=options.domain?'; domain='+options.domain:'';var secure=options.secure?'; secure':'';document.cookie=[name,'=',encodeURIComponent(value),expires,path,domain,secure].join('')}else{var cookieValue=null;if(document.cookie&&document.cookie!=''){var cookies=document.cookie.split(';');for(var i=0;i<cookies.length;i++){var cookie=jQuery.trim(cookies[i]);if(cookie.substring(0,name.length+1)==(name+'=')){cookieValue=decodeURIComponent(cookie.substring(name.length+1));break}}}return cookieValue}};
jQuery.fn.ContextTeaserSearch = function(pat, config) {
 jQuery.fn.ContextTeaserCount = 0;
 jQuery.fn.ContextTeaserUserWords = Array();
 
 function innerHighlight(node, pat, config) {
  var skip = 0;
  
  if (node.nodeType == 3) {
  	$.each(pat, function(pat_id) {
  	   
  	   if (config['unique'] == 0 || jQuery.inArray(pat_id, jQuery.fn.ContextTeaserUserWords) == '-1') {
		   var pos = node.data.toUpperCase().indexOf(this.toUpperCase());
		   if (pos >= 0) {
			  var spannode = document.createElement('span');
			  spannode.className = 'ContextTeaserAll';
			  spannode.id = 'ContextTeaser_'+jQuery.fn.ContextTeaserCount;
			  var middlebit = node.splitText(pos);
			  var endbit = middlebit.splitText(this.length);
			  var middleclone = middlebit.cloneNode(true);
			  spannode.appendChild(middleclone);
			  middlebit.parentNode.replaceChild(spannode, middlebit);
			  skip = 1;
			  jQuery.fn.ContextTeaserUserWords[jQuery.fn.ContextTeaserUserWords.length] = pat_id;
			  jQuery.fn.ContextTeaserCount++;
		   }
  	   }
  	});
  }
  else if (node.nodeType == 1 && node.childNodes && !/(script|style)/i.test(node.tagName)) {
   for (var i = 0; i < node.childNodes.length; ++i) {
    i += innerHighlight(node.childNodes[i], pat, config);
   }
  }
  return skip;
 }
 return this.each(function() {
    innerHighlight(this, pat, config);
 });
};

jQuery.fn.ContextTeaserStart = function(config, teaser, search_id) {
  function get_random() {
  	 var irandom = Math.round(Math.random() * jQuery.fn.ContextTeaserCount);
  	 if (jQuery.inArray(irandom, enable_words) != '-1') {
  	 	irandom = get_random();
  	 }
     return irandom;
  }


  function open_window (eid) {
  	 
  	 var offset = $("#"+eid).offset();
		 offset_x_1 = offset.left;
		 offset_y_1 = offset.top;
	 
     $('<div id="'+eid+'_bg" class="ContextTeaserActiveWindow"></div>').appendTo("body").css({
	       'position' : 'absolute',
		   'left' : offset_x_1,
		   'top' : offset_y_1-100,
		   'background-color' : config['bg_color'],
		   'opacity' : config['bg_alpha']/100,
		   'border' : '1px solid #000',
		   'display' : 'none',
		   'height' : '102px',
		   'width'  : '240px',
		   'z-index'  : '900'
     }).fadeIn();
     $('<div id="'+eid+'_cnt" class="ContextTeaserActiveWindow"></div>').appendTo("body").css({
	       'position' : 'absolute',
		   'left' : offset_x_1,
		   'top' : offset_y_1-100,
		   'border' : '1px solid #000',
		   'display' : 'none',
		   'height' : '102px',
		   'width'  : '240px',
		   'z-index'  : '900',
		   'overflow' : 'hidden'
     }).fadeIn().bind('mouseleave', function() {
     	close_window(eid);
     });
     if (config['ref_link'] == 1) {
	   $("<div id=\""+eid+"_adv\" class=\"ContextTeaserActiveWindow\"><a href=\"http://advertpro.net/to/"+config['ref_id']+"\" style=\"font: 10px "+config['font_type']+"; color: "+config['font_color']+";\">Реклама на сайте</a></div>").appendTo("body");
	   $("#"+eid+"_adv").css({
	      'width' : 100, 
		  'height' : 15, 
		  'display' : 'none',
		  'position' : 'absolute',
		  'background-color' : config['bg_color'],
		  'border' : '1px solid #000',
		  'border-top' : '1px solid '+config['bg_color'],
		  'opacity' : config['bg_alpha']/100,
		  'cursor' : 'hand',
		  'cursor' : 'pointer',
		  'text-align' : 'center',
		  'left' : offset_x_1+140,
		  'top' : offset_y_1+3,
		  'z-index' : '900'
	   }).fadeIn();
     }
     // 'bid' : '455','font_color' : '#FFFFFF','font_type' : 'Arial','font_size' : '12','img_direct' : 'left',}
     //('<div style="padding: 5px"><a style="text-decoration: none;" href="http://192.168.0.13:81/advertpro/" onmouseover="this.href=\'http://192.168.0.13:81/advertpro/\'" title="Описание тизер блока" target="_blank"><img id="teb_img_src_0_0" src="http://192.168.0.13:81/advertpro/upload/test.jpg" title="Описание тизер блока" alt="Здесь выводится заголовок тизерного блока, описание при наведении." style="margin-right: 8px;" align="left" border="0" height="90"></a><a href="http://192.168.0.13:81/advertpro/" onmouseover="this.href=\'http://192.168.0.13:81/advertpro/\'" title="Описание тизер блока" target="_blank" style="font: 12px Arial">Здесь выводится заголовок тизерного блока, описание при наведении.</a></div>');
     current_id = jQuery.fn.ContextTeaserCurrent;
     if (config['img_direct'] == 'left') {
					html_teaser = '<div style="padding: 5px">'+
					               '<a style="text-decoration: none" href="http://'+location.host+'" onmouseover="this.href=\''+teaser[current_id]['link']+'\'" title="'+teaser[current_id]['desc']+'" target="_blank" >'+
					               '<img  src="'+teaser[current_id]['img']+'" title="'+teaser[current_id]['desc']+'" alt="'+teaser[current_id]['title']+'" border="0" align="left" height="90" style="margin-right: 8px;"/></a>'+
					               '<a href="http://'+location.host+'" onmouseover="this.href=\''+teaser[current_id]['link']+'\'" title="'+teaser[current_id]['desc']+'" target="_blank" style="font: '+config['font_size']+'px '+config['font_type']+'; color: '+config['font_color']+';">'+teaser[current_id]['title']+
					               '</a></div>';
	            } else {
					html_teaser = '<div style="padding: 5px">'+
					               '<a style="text-decoration: none" href="http://'+location.host+'" onmouseover="this.href=\''+teaser[current_id]['link']+'\'" title="'+teaser[current_id]['desc']+'" target="_blank" >'+
					               '<img src="'+teaser[current_id]['img']+'" title="'+teaser[current_id]['desc']+'" alt="'+teaser[current_id]['title']+'" border="0" align="right" height="90" style="margin-left: 8px;"/></a>'+
					               '<a href="http://'+location.host+'" onmouseover="this.href=\''+teaser[current_id]['link']+'\'" title="'+teaser[current_id]['desc']+'" target="_blank" style="font: '+config['font_size']+'px '+config['font_type']+'; color: '+config['font_color']+';">'+teaser[current_id]['title']+
					               '</a></div>';
	            }

	      		$("#"+eid+"_cnt").html(html_teaser);
     if (jQuery.fn.ContextTeaserCurrent == jQuery.fn.ContextTeaserMax) {
		jQuery.fn.ContextTeaserCurrent = 0;
	 } else {
	    jQuery.fn.ContextTeaserCurrent++;
	 }
     
     $("#"+eid).unbind('mouseover');
  }
  
  function close_window(eid, current_id) {
     $("#"+eid+"_bg").fadeOut(function() {
     	$(this).remove();
     });
     $("#"+eid+"_adv").fadeOut(function() {
     	$(this).remove();
     });
     $("#"+eid+"_cnt").fadeOut(function() {
     	$(this).remove();
	    $("#"+eid).bind('mouseover', function() {
	       open_window(eid);
	    });
     });
  }
  
  if (search_id && search_id.length > 0) {
      $.each(search_id, function() {
         $('#'+this).bind('blur', function() {
             $.cookie('TE_teaser_cookie_search', this.value);
         });
      });
  }

  if (jQuery.fn.ContextTeaserCount > 0) {
	  i = 0;
	  enable_words = Array();
	 
	  if (config['count_type'] == 'procent') {
	  	enable_words_count = Math.round(jQuery.fn.ContextTeaserCount / 100 * config['count']);
	  } else {
	  	enable_words_count = config['count'];
	  }
	  if (enable_words_count == 0) {
	  	enable_words_count = 1;
	  }
	  while(i < enable_words_count) {
	     enable_words[i] = get_random(); 
	     i++;
	  }
	  jQuery.fn.ContextTeaserCurrent = 0;
	  jQuery.fn.ContextTeaserMax = teaser.length-1;
	  	
	  $.each(enable_words, function(id) {
	     $('#ContextTeaser_'+enable_words[id]).removeClass().addClass('ContextTeaserActive').css({
	       'cursor' : 'help', 
	       'border-bottom' : '1px dotted #000',
	       'font-weight' : config['text_type'],
	       'color' : config['text_color']
	     }).mouseover(function() {
	     	open_window(this.id);
	     });
	  });
  }
};

 jQuery.fn.ContextTeaserRemove = function() {
    jQuery.fn.ContextTeaserCurrent = 0;
    jQuery.fn.ContextTeaserCount = 0;
    jQuery.fn.ContextTeaserMax = 0;
    $("div.ContextTeaserActiveWindow").remove();
    return this.find("span").each(function() {
  	  this.parentNode.firstChild.nodeName;
	  with (this.parentNode) {
	     replaceChild(this.firstChild, this);
	     normalize();
	  }
     }).end();
  };

