

var _CiaoMedia_JS_OBJ = {  

	_keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
 
  counter : 0,

	encode : function (input) { 
		var output = "";
		var chr1, chr2, chr3, enc1, enc2, enc3, enc4; 
		var i = 0;  

		input = _CiaoMedia_JS_OBJ._utf8_encode(input); 

		while (i < input.length) { 

			chr1 = input.charCodeAt(i++); 
			chr2 = input.charCodeAt(i++); 
			chr3 = input.charCodeAt(i++); 
			enc1 = chr1 >> 2; 
			enc2 = ((chr1 & 3) << 4) | (chr2 >> 4); 
			enc3 = ((chr2 & 15) << 2) | (chr3 >> 6); 
			enc4 = chr3 & 63;  

			if (isNaN(chr2)) {
				enc3 = enc4 = 64; 
			} else if (isNaN(chr3)) { 
				enc4 = 64;  
			}              

			output = output + 
			this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) + 
			this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4); 
		}  
		return output;  
	},   
	_utf8_encode : function (string) {  
		string = string.replace(/\r\n/g,"\n");   
		var utftext = "";  
		for (var n = 0; n < string.length; n++) { 
			var c = string.charCodeAt(n);   
			if (c < 128) {  
				utftext += String.fromCharCode(c);  
			}  
      else if((c > 127) && (c < 2048)) {  
				utftext += String.fromCharCode((c >> 6) | 192); 
				utftext += String.fromCharCode((c & 63) | 128); 
			} 
			else {  
				utftext += String.fromCharCode((c >> 12) | 224);   
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128); 
			} 
		}
		return utftext; 
	},  
  insertJS : function (path) { 
      document.write('<scr'+'ipt src="'+path+'" type="text/javascript"><\/scr'+'ipt>');
  },
  addCSS : function addStyle(){
    	var s = document.createElement('link');
    	s.href = 'http://www.ciaomedia.com/version2/css/ciao_stylesheet.css?cb='+Math.floor(Math.random()*10000);
    	s.type = 'text/css';
    	s.media = 'all';
    	s.rel = 'stylesheet';
    	document.getElementsByTagName('head')[0].appendChild(s);
  },
  load : function(){
    this.preloadImage('http://www.ciaomedia.com/version2/images/ciao_bar4.gif',320,19);
    this.preloadImage('http://www.ciaomedia.com/version2/images/tooltip_shadow.gif',300,14);  
  },
  init : function(data){
        var ciao_outer_wrapper = document.createElement('div');
        ciao_outer_wrapper.style.textAlign = 'center';
        ciao_outer_wrapper.style.display = 'block';
		ciao_outer_wrapper.style.marginTop = '0.2em';
		ciao_outer_wrapper.style.marginBottom = '0.2em';
        var ciao = document.createElement('div');
        ciao.className = 'ciao_main_wrapper';
        ciao.appendChild(this.createTitleWrapper(this.createCiaoLogo(),this.createTitle(data.title,data.imp)))
        ciao.appendChild(this.createToolTip(data.tooltip,data.clickurl));
        ciao_outer_wrapper.appendChild(ciao);
        return ciao_outer_wrapper;
  },
  createTitleWrapper : function(logo,title){
    var w = document.createElement('div');
    w.className = 'ciao_title_wrapper';
    w.appendChild(logo);
    w.appendChild(title);
    w.appendChild(this.createClearBoth());
    return w;
  },
  createClearBoth : function(){
    var d = document.createElement('br');
    d.style.clear = 'both';
    return d;
  },
  createTitle : function(title,imp){
     var titleWrap = document.createElement('span');
     titleWrap.className = 'ciao_title';
     var titleA = document.createElement('a');
     titleA.innerHTML = title;
     titleWrap.appendChild(titleA);
     var t;
     var i = imp; 
      $(titleWrap).hover(function(){
            var that = this;
            t = setTimeout(function(){
                      $(that.parentNode.nextSibling).fadeIn('fast');
                      if(that.parentNode.className == 'ciao_title_wrapper'){
                          that.parentNode.className = 'ciao_title_wrapper_down';
                      }                                      
                    },400);
            _CiaoMedia_JS_OBJ.recordImg(i);          
          },
          function(){
            clearTimeout(t);
          }
        );      
     return titleWrap;
  },
  createCiaoLogo : function(){
     var ping = document.createElement('a');
     ping.className = 'ciao_logo';
     ping.href = 'http://www.ciaomedia.com/?utm_source=publishers&utm_medium=ads&utm_campaign=demo';
     ping.innerHTML = 'Ads by CiaoMedia:';
     ping.target = '_blank';
     return ping;
  },
  createToolTip : function(tooltip,clickurl){
     var ciao_tooltip_outer_wrapper = document.createElement('div');
     ciao_tooltip_outer_wrapper.style.width = 'auto';
     ciao_tooltip_outer_wrapper.style.height = 'auto';
     ciao_tooltip_outer_wrapper.style.display = 'none';
     ciao_tooltip_outer_wrapper.style.textAlign = 'left';

     ciao_tooltip_outer_wrapper.id = 'ciaoid' + this.counter;
     var titleWrap = document.createElement('div');
     titleWrap.className = 'ciao_tooltip_wrapper';
     titleWrap.id = 'ciaoid' + (this.counter + 1);
     titleWrap.innerHTML = this.createCloseButton() + tooltip;
     this.counter ++;
     ciao_tooltip_outer_wrapper.appendChild(titleWrap);
     return ciao_tooltip_outer_wrapper;
  },
  createCloseButton : function(){
    var c = '<div style="text-align:right;height:14px;margin-top:2px;margin-bottom:0.3em;">'
    + '<img class="ciao_close_button" src="http://www.ciaomedia.com/version2/images/close.gif" />'
    + '</div>';
    return c;
  },
 recordImg : function(url) {
    window.focus();
    var bug = new Image();  
    bug.src = url;   
 },
 preloadImage : function(img,width,height){
    window.focus();
    var i = new Image(width,height);
    i.src = img;
 }
}

function ciao_record(url)
{
  _CiaoMedia_JS_OBJ.recordImg(url);
}

_CiaoMedia_JS_OBJ.load();
var ciao_url = _CiaoMedia_JS_OBJ.encode(ciao_targetURL);
_CiaoMedia_JS_OBJ.insertJS('http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.js'); 
_CiaoMedia_JS_OBJ.insertJS('http://www.ciaomedia.com/version2/example.php?url='+ciao_url+'&type='+ ciao_type + '&zone='+ciao_zone+'&P8259745='+ciao_4702358+'&K895264='+ciao_1903586+'&callback=processData');


  function processData(jsonObj){    
      
      _CiaoMedia_JS_OBJ.addCSS();
      if(typeof(ciao_separator) != 'undefined')
      {
        var separator = ciao_separator;
      }
      else
      {
        var separator = 'p';
      }
      var ciaoMedia_p = document.getElementById('ciaomedia').getElementsByTagName(separator);
      
      var spacePara = 1;
      var countAds = 0;
      for(var t=0;t<ciaoMedia_p.length;t++)
      {
        if(t == ciao_ciaoStartPara || spacePara == ciao_linkParaSpace && countAds <= jsonObj.length)
        {
            var ciaoAd = _CiaoMedia_JS_OBJ.init(jsonObj[countAds]);
            ciaoMedia_p[t].parentNode.insertBefore(ciaoAd, ciaoMedia_p[t].nextSibling);
            $(ciaoMedia_p[t]).css('margin-bottom','0em');
            $(ciaoMedia_p[(t+1)]).css('margin-top','0em');
            spacePara = 1;
            countAds ++;
        }
        else
        {
          spacePara ++;  
        }
      }

      $('.ciao_close_button').click(function(){
          var that = this;               
           $(this.parentNode.parentNode.parentNode).fadeOut(function(){
               if(that.parentNode.parentNode.parentNode.parentNode.firstChild.className == 'ciao_title_wrapper_down'){
                  that.parentNode.parentNode.parentNode.parentNode.firstChild.className = 'ciao_title_wrapper';
               }
               else{
                  that.parentNode.parentNode.parentNode.parentNode.firstChild.className = 'ciao_title_wrapper_down';
               }
           });                                     
      });
 
      // SET CUSTOM COLOR FOR LINK 
      if(typeof(ciao_ciaoLinkColor) != 'undefined')
      {
        $('.ciao_title').css('color',ciao_ciaoLinkColor);
      }
      
      // SET CUSTOM COLOR FOR LOGO
      if(typeof(ciao_logo_color) != 'undefined')
      {
        $('.ciao_logo_link').css('color', ciao_logo_color);
      }    
  }






