﻿
function doHighlight(bodyText, searchTerm, highlightStartTag, highlightEndTag) 
{
  // the highlightStartTag and highlightEndTag parameters are optional
  if ((!highlightStartTag) || (!highlightEndTag)) {
    highlightStartTag = "<font style='color:blue; background-color:yellow;'>";
    highlightEndTag = "</font>";
  }
  
  // find all occurences of the search term in the given text,
  // and add some "highlight" tags to them (we're not using a
  // regular expression search, because we want to filter out
  // matches that occur within HTML tags and script blocks, so
  // we have to do a little extra validation)
  var newText = "";
  var i = -1;
  var lcSearchTerm = searchTerm.toLowerCase();
  var lcBodyText = bodyText.toLowerCase();
    
  while (bodyText.length > 0) {
    i = lcBodyText.indexOf(lcSearchTerm, i+1);
    if (i < 0) {
      newText += bodyText;
      bodyText = "";
    } else {
      // skip anything inside an HTML tag
      if (bodyText.lastIndexOf(">", i) >= bodyText.lastIndexOf("<", i)) {
        // skip anything inside a <script> block
        if (lcBodyText.lastIndexOf("/script>", i) >= lcBodyText.lastIndexOf("<script", i)) {
          newText += bodyText.substring(0, i) + highlightStartTag + bodyText.substr(i, searchTerm.length) + highlightEndTag;
          bodyText = bodyText.substr(i + searchTerm.length);
          lcBodyText = bodyText.toLowerCase();
          i = -1;
        }
      }
    }
  }
  
  return newText;
}


function highlightSearchTerms(searchText, treatAsPhrase, warnOnFailure, highlightStartTag, highlightEndTag)
{
  // if the treatAsPhrase parameter is true, then we should search for 
  // the entire phrase that was entered; otherwise, we will split the
  // search string so that each word is searched for and highlighted
  // individually
  if (treatAsPhrase) {
    searchArray = [searchText];
  } else {
    searchArray = searchText.split(" ");
  }
  
  if (!document.body || typeof(document.body.innerHTML) == "undefined") {
    if (warnOnFailure) {
      alert("Sorry, for some reason the text of this page is unavailable. Searching will not work.");
    }
    return false;
  }
  
  var bodyText = document.body.innerHTML;
  for (var i = 0; i < searchArray.length; i++) {
    bodyText = doHighlight(bodyText, searchArray[i], highlightStartTag, highlightEndTag);
  }

  document.body.innerHTML = bodyText;
  return true;
}


//----------------------------------------inlineAds
var inlineAdsRootPath="http://ads.linksaz.com";
function inlineads_showAds(inlineads_keyword,inlineads_keyIndex,evt)
{
    //---    
    if(MouseOverWordCounter[inlineads_keyIndex]==inlineads_AdsCount)        
    {
        MouseOverWordCounter[inlineads_keyIndex]=0;
    }    
    if(inlineads_ADSarray[MouseOverWordCounter[inlineads_keyIndex]][7].indexOf(","+inlineads_keyword+",",0)>=0)
    {
        inlineads_AdsIndex=MouseOverWordCounter[inlineads_keyIndex];
        MouseOverWordCounter[inlineads_keyIndex]+=1;
    }else
    {        
        MouseOverWordCounter[inlineads_keyIndex]+=1;       
        inlineads_showAds(inlineads_keyword,inlineads_keyIndex,evt);
    }
    //---
    var e = new MouseEvent(evt);
     var y=e.y;
     var x=e.x;
 
     var yOffset;
     var xOffset;     
     if(typeof pageYOffset!= 'undefined'){
        //most browsers
        yOffset= pageYOffset;
        xOffset=pageXOffset;
     }
     else{
        var B= document.body; //IE 'quirks'
        var D= document.documentElement; //IE with doctype
        D= (D.clientHeight)? D: B;
        yOffset= D.scrollTop;
        xOffset=D.scrollLeft;
     }
     
     var docWidth=document.body.clientWidth;
     var docHeight=document.body.clientHeight;
     var scWidth=screen.width;
     var scHeight=screen.height;
     var OffsetAtHeight;
     var OffsetAtwidth;
     if((e.y+250)>(scHeight-200))
     {
        y=e.y-250;
        OffsetAtHeight=true;
     }
     if((e.x+230)>scWidth)
     {
        x=e.x-215;
        OffsetAtwidth=true;
     }
     
     //------
     var inlineads_AdsHtmlContent="";
     if(inlineads_ADSarray[inlineads_AdsIndex][4]=="1")//text ads
       inlineads_AdsHtmlContent="<br/>";
     else if(inlineads_ADSarray[inlineads_AdsIndex][4]=="2")//image ads
       inlineads_AdsHtmlContent="<br><center><a style=\"text-decoration:none;font-family: Tahoma;font-size:9pt\" href=\""+inlineAdsRootPath+"/ads/adclick.aspx?id="+inlineads_ADSarray[inlineads_AdsIndex][0]+"&site="+siteID+"&referrer="+escape(document.location)+"\" target=_blank><img border=0 width=150 height=110 src="+inlineAdsRootPath+"/ads/adfiles/"+inlineads_ADSarray[inlineads_AdsIndex][6]+"></a></center>";
     else if(inlineads_ADSarray[inlineads_AdsIndex][4]=="3")//flash ads
       inlineads_AdsHtmlContent="<br><center><object classid=\"CLSID:D27CDB6E-AE6D-11CF-96B8-444553540000\" id=ShockwaveFlash1 width=192 height=150>"
                                 +"<param name=\"_cx\" value=5080>"
                                 +"<param name=\"_cy\" value=5080>"
                                 +"<param name=FlashVars value=\"\">"
                                 +"<param name=Movie value=\"\">"
                                 +"<param name=Src value=\""+inlineAdsRootPath+"/ads/adfiles/"+inlineads_ADSarray[inlineads_AdsIndex][6]+"\">"
                                 +"<param name=WMode value=Window>"
                                 +"<param name=Play value=-1>"
                                 +"<param name=Loop value=-1>"
                                 +"<param name=Quality value=High>"
                                 +"<param name=SAlign value=\"\">"
                                 +"<param name=Menu value=-1>"
                                 +"<param name=Base value=\"\">"
                                 +"<param name=AllowScriptAccess value=always>"
                                 +"<param name=Scale value=ShowAll>"
                                 +"<param name=DeviceFont value=0>"
                                 +"<param name=EmbedMovie value=0>"
                                 +"<param name=BGColor value=\"\">"
                                 +"<param name=SWRemote value=\"\">"
                                +"</object></center><br>";              
     inlineads_AdsHtml="<center><a style=\"text-decoration:none;font-family: Tahoma;font-size:9pt;color:blue\" href=\""+inlineAdsRootPath+"/ads/adclick.aspx?id="+inlineads_ADSarray[inlineads_AdsIndex][0]+"&site="+siteID+"&referrer="+escape(document.location)+"\" target=_blank><strong>"+inlineads_ADSarray[inlineads_AdsIndex][3]+
			    "</strong></a></center>"+inlineads_AdsHtmlContent +"<span style=\"text-decoration:none;font-family: Tahoma;font-size:9pt;color:black\" dir=\"rtl\">"+ inlineads_ADSarray[inlineads_AdsIndex][2]+"</span>";
     document.getElementById('inlineads_content').innerHTML=inlineads_AdsHtml;
     document.getElementById('inlineads_div').style.display='block';
     document.getElementById('inlineads_div').style.left=eval(x+8)+'px';
     document.getElementById('inlineads_div').style.top=eval(y+16+yOffset)+'px';
     //---show pointer
     document.getElementById('pointer').innerHTML="";
     document.getElementById('pointer').style.display='block';
     var y2=y+yOffset;
     if(OffsetAtHeight==true)
       y2=y+yOffset+230;

     for(var x3=e.x;x3<=e.x+16;x3+=1)
     {
      if(OffsetAtHeight==true)
         y2=y2-2;
      else
         y2=y2+2;
      document.getElementById('pointer').innerHTML+='<div style="background-color:#a99c89;width:1px;height:9px;position:absolute;left:'+x3+'px;top:'+y2+'px"></div>';
     }
     //---
}

var MouseOverWordCounter=new Array();
function inlineads_markKeyword()
{
     var inlineads_newHtml;
 	    var inlineads_adsIDhtmlcontent=document.body.innerHTML; 
	    inlineads_newHtml=inlineads_adsIDhtmlcontent.toLowerCase();
    var wordsMarkedArray = new Array();
    var markedIndex=0;
     for(var c=0;c<inlineads_AdsCount;c++)
     {
	    var inlineads_adsID=inlineads_ADSarray[c][0];
	    var inlineads_adsKeywords=inlineads_ADSarray[c][7];		  
	    //--mark the keyword as AdsKeyword
	    var inlineads_keysSplit = inlineads_adsKeywords.split(",");
	    for(var i=0;i<inlineads_keysSplit.length;i++)
	    {		
		    if(inlineads_keysSplit[i]!="")
		    {	
		        var isKeyMarked=false;
		        for(var k=0;k<markedIndex;k++)
		        {
		          if(inlineads_keysSplit[i]==wordsMarkedArray[k])
		             isKeyMarked=true;
		        }
		        if(isKeyMarked==false)
		        {
		         highlightSearchTerms(inlineads_keysSplit[i],true,false,"<span style='border-bottom-style: double' onmouseover='inlineads_showAds(\""+inlineads_keysSplit[i]+"\","+markedIndex.toString()+",event);'>","</span>");				        
		         wordsMarkedArray[markedIndex]=inlineads_keysSplit[i];
		         MouseOverWordCounter[markedIndex]=0;
		         markedIndex++;		         
		        } 
		    }		
	    }	
     }
}

function inlineads_AdsContainer()
{	
    //alert("containter");
	document.body.innerHTML+='<span id="pointer"></span><div id="inlineads_div"  style="height:200px;z-index:1000;font-family: Tahoma;font-size:9pt;position:absolute;left:0px;top:400px;display:none;text-align:right;background-color:#f9f8f4;width:220px;border:#fff7e8 thick ridge;"><table width="100%"><tr><td onmousedown="beginDrag(event)" onmouseup="endDrag()" style="background-image:url(\''+inlineAdsRootPath+'/ads/img/headbg.jpg\');direction:rtl"><span  style="float:right"><a href="http://inlineads.com" target="_blank" style="text-decoration:none;font-family: Tahoma;font-size:8pt;color:blue">تبليغات درون متني</a></span><img src="'+inlineAdsRootPath+'/ads/img/close.jpg" style="float:left;cursor:pointer" onclick="hideAdsDiv()" /></td></tr><tr><td><div id="inlineads_content" dir="rtl" style="text-align:right" ></div></td></tr></table></div>';
}

function  hideAdsDiv()
{
    document.getElementById('inlineads_div').style.display='none';
    document.getElementById('pointer').style.display='none';
}

function MouseEvent(e)  {
      if (e == null) e = window.event; 
//    if(typeof( e.pageX ) == 'number') {
//      this.x = e.pageX;       
//    } else {
      this.x = e.clientX; 
//    }
//    if(typeof( e.pageY ) == 'number') {
//      this.y = e.pageY; 
//    } else {
      this.y = e.clientY; 
//    }
    if(e.target) {
      this.target = e.target; 
    } else {
      this.target = e.srcElement;
    } 
}

var Dragging=false;
var ClickOffsetX;
var ClickOffsetY;
function beginDrag(evt)
{
    Dragging=true;
     var e = new MouseEvent(evt);
     ClickOffsetX =e.x- eval(document.getElementById('inlineads_div').style.left.replace("px",""));
     ClickOffsetY =e.y- eval(document.getElementById('inlineads_div').style.top.replace("px",""));
     document.getElementById('pointer').style.display='none';
     document.getElementById('inlineads_div').style.cursor='move';
    document.body.onmousemove=doDrag;
}
function doDrag(evt)
{
    if(Dragging==true)
    {
     var e = new MouseEvent(evt);
     document.getElementById('inlineads_div').style.left=eval(e.x-ClickOffsetX)+'px';
     document.getElementById('inlineads_div').style.top=eval(e.y-ClickOffsetY)+'px';
    }
}
function endDrag()
{
    Dragging=false;
    document.getElementById('inlineads_div').style.cursor='default';
}