﻿
 $(function() {
 
     var currentpage = $.jqURL.strip();
     var topword = TopWords();
     var keyword = $.jqURL.get('key');
     var advertid = $.jqURL.get('AdvertID');
     
     // if its the search results set keyword with the search term inputted
     if(currentpage.indexOf("search")>0)
     {
       keyword= $.jqURL.get('q');
     }
     //for taxonomy based keywords
     else if($.jqURL.get('topic')!= undefined)
     {
        keyword = $.jqURL.get('topic');
     }
     else if(keyword == undefined)
     {
        keyword='';
     }
     
     //if keyword consists of more than one word remove "+" and replace with " "
     
     if(keyword.indexOf('+')>0)
     {
        keyword = FormatKeyword(keyword);
     }
     
     
     //get the adslots for the page
     var adSlotRhs= $("#rightcoladvert").attr("id");      
     var adFooterBody = $("#FooterAd").attr("id") ;
     
     
     //this is used for the preview page 
     if(advertid != undefined )
     {
	
         //call preview for advert   
         ajaxGetPreviewAdvert(adSlotRhs, advertid);
         ajaxGetPreviewAdvert(adFooterBody, advertid);
     }
     else
     {
        //ajax call to get advert
        ajaxGetAdvert(currentpage,topword,keyword,adSlotRhs);
        ajaxGetAdvert(currentpage,topword,keyword,adFooterBody);
     }
      
 });
 
 
 
 
 
 function FormatKeyword(keyword)
 {
    kword = new String(keyword);   
    strcount = kword.length;
    for(i=0;i<strcount;i++)
    {
        kword = kword.replace('+',' ');
    }    
    return kword;
 }
 
 
 //funuction to get a comma delimited list of keyword terms based on the page.
 function TopWords()
 {
     var count = 10;
     var topword='';

    $.extend($.wordStats.stopWords, {'retrieved': true, '2007': true});

     $.wordStats.computeTopWords(count);

        for(var i = 0, j = $.wordStats.topWords.length; i < j && i <= count; i++) 
        {
            topword += $.wordStats.topWords[i].substring(1) + ',';
        }

     $.wordStats.clear();
     
    
    
    return topword;
 
 }

function ajaxGetAdvert(url,topword,keyword,adslot)
{
      
      if(adslot != undefined) //if the adslot exists get advert
      {
      var data = "url="+url+"&topword="+topword+"&keyword="+keyword+"&adslot="+adslot;
      
      $.ajax({
                   type: "GET",
                   url: "/adserver.aspx",
                   datatype:'html',
                   data: data,
                   async: false,
                   success: function(msg){
                        
                      
                      if (msg.length > 11 )
                      {
                       $('#'+adslot).html(msg);
                      }
                                 

                   },
                   error: function(msg){
                       
                        return false;   
                   }
                   
              });
      }

}

function ajaxGetPreviewAdvert(positionid, advertid)
{
    if(positionid != undefined) //if the adslot exists get advert
      {
      var data = "AdvertID="+advertid+"&adslot="+positionid;
      
	//alert(data);	

      $.ajax({
                   type: "GET",
                   url: "/adserver.aspx",
                   datatype:'html',
                   data: data,
                   async: false,
                   success: function(msg){
                        
                      
                      if (msg.length > 11 )
                      {
                       $('#'+positionid).html(msg);
                      }
                                 

                   },
                   error: function(msg){
                        $('#'+positionid).html(msg);
                   }
                   
              });
      }

}

 function recordclickthrough(advertid,destination)
 {
    if(advertid != undefined) //if the ad id exists
      {
      var referer  = $.jqURL.strip();
      var data = "AdID="+advertid+"&referer="+referer+"&clickthrough=true";
      
	//alert(data);	

      $.ajax({
                   type: "GET",
                   url: "/adserver.aspx",
                   datatype:'html',
                   data: data,
                   async: false,
                   success: function(msg){
                  
                       $.jqURL.loc(destination,{w:screen.width-100,h:screen.height-100,wintype:'_blank'}); 
                       return false;   

                   },
                   error: function(msg){
                        alert('unable to open the request page in a new window');
                        
                         return false;   
                   }
                   
              });
      }
 }