var oDisplay      = new Object();
var aOverture     = new Array();
var sLanguageCode = new String( window.location ).substr( 7, 2 ) || 'fr';
var sOrigin = (document.URL.match( /(network_thumb|network|popup_exit_1|popup_exit|popup_1|popup|search|stats)/))[1] || '';
var nCategoryID = 0;
var sSearch_key = '';
var over_block = 5;

var sCountryCode = 'fr';                                         // DEBUG


function initPromo( sPromo, sParam_1, sParam_2, sParam_3, sParam_4 ) {
   
   a_displayableCountries = new Array( 'fr', 'es', 'us', 'de', 'be' );
   display_overture = false;

   if(sOrigin != 'popup') { 
	if(sOrigin != 'network_thumb') {  
		for(x in a_displayableCountries){
		   if(a_displayableCountries[x] == sCountryCode) { display_overture = true }
		}
		}
	}
   
   if (sPromo == 'overture' || sPromo == 'search_key') {

      var nStar       = 0;
      var sFilter     = '';
      var sKeyword    = '';
      var sQuery      = '';

	   if (sParam_1 != null && sParam_2 != null && sParam_3 != null && sParam_4 != null) {

         nStar    = sParam_1;
         sFilter  = sParam_2;
         sKeyword = sParam_3;
         sQuery   = sParam_4;
			
			if(sFilter == 'over') { over_block = 10 }

      } else if (sParam_1 != null && sParam_2 != null) {

         nCategoryID = sParam_1;
         nStar       = sParam_2;

			if (sParam_3 != null) { sQuery = sParam_3 }
      }
		
		var sURL_Key = 'http://' + sLanguageCode + '.wedoo.com/cgi-bin/surfer/surfer_search_key.cgi?categoryid=' + nCategoryID + '&language=' + sLanguageCode;
		document.write( '<script language="javascript" src="' + sURL_Key + '"></scr' + 'ipt>' );

      if (display_overture) {
			
			if (sOrigin == 'stats') { sOrigin = 'network_stats' }
			
         var sSRC = 'http://overture.wedoo.com/cgi-bin/overture.cgi?' + ((sCountryCode)   ? 'country='    + sCountryCode  + '&' : '')
                                                                      + ((nCategoryID)    ? 'categoryid=' + nCategoryID   + '&' : '')
                                                                      + ((nStar)          ? 'star='       + nStar         + '&' : '')
                                                                      + ((sLanguageCode)  ? 'language='   + sLanguageCode + '&' : '')
                                                                      + ((sOrigin)        ? 'origin='     + sOrigin       + '&' : '')
                                                                      + ((sFilter  != '') ? 'filter='     + sFilter       + '&' : '')
                                                                      + ((sKeyword != '') ? 'keyword='    + sKeyword      + '&' : '')
                                                                      + ((sQuery   != '') ?                 sQuery        + '&' : '')
                                                                      + Date.parse( new Date() ) + Math.floor( Math.random() * 1000000000 );
		
			document.write( '<script language="javascript" src="' + sSRC + '"></scr' + 'ipt>' );
      }
   }
}

function displayPromo( sPromo, sPlaceHolder, sParam ) {

  if (oDisplay[sPlaceHolder] == null) {

   if (sPromo == 'adcycle') {

      var nWidth  = parseInt( (sParam.split( 'x' ))[0] );
      var nHeight = parseInt( (sParam.split( 'x' ))[1] );
      var nGID    = parseInt( (sParam.split( 'x' ))[2] );
       
      var oDate   = new Date();
      var sSecond = oDate.getSeconds();
      var sMinute = oDate.getMinutes();
      var sID     = sSecond * sMinute + Math.floor( (Math.random() + 1) * 999 );

      var sURL = 'http://ad.wedoo.com/cgi-bin/adcycle4/adcycle.cgi';
      var sHTML = '<iframe src="' + sURL + '?gid=' + nGID + '&id=' + sID + '&layout=multi&t=_blank&type=iframe&keywords=' + '" width="' + nWidth + '" height="' + nHeight + '" border="0" marginwidth="0" marginheight="0" hspace="0" vspace="0" frameborder="0" scrolling="no">'
                +   '<img src="'  + sURL + '?gid=' + nGID + '&id=' + sID + '&layout=multi&keywords=' + '" width="' + nWidth + '" height="' + nHeight + '" border="1" onclick="window.open(\'' + sURL + '?gid=' + nGID + '&id=' + sID + '&layout=multi\')">'
                + '</iframe>';

      document.write( sHTML );

   } else if (sPromo == 'overture') {

      if (aOverture != null && aOverture.length) {

        if (sParam != null) {

          var sHTML = '';
			 
			if(sParam == 'search' && over_block == 3) {
				over_block = 5;
			} else if(sParam == 'network_stats') {
				over_block = 2;
				//aOverture.sort( function(a, b) { return a.length - b.length } );
			}
			 
          for (var i = 0; i < over_block; i++) { if (aOverture.length) { sHTML += aOverture.shift(); } }

          document.write( sHTML );

        } else { if (aOverture.length) { document.write( aOverture.shift() ); } }
      }
   } 

    if (sPlaceHolder != null && sPlaceHolder != '') { oDisplay[sPlaceHolder] = true; }
  }
}  

if (!Array.prototype.shift) { 

	Array.prototype.shift = function() {

		firstElement = this[0];
		this.reverse();
		this.length = Math.max( this.length - 1, 0 );
		this.reverse();
		return firstElement;
	}
}
