var CKAPI = 
{
	
	_mode : 1,
	_referer : null,
	
	_mode_1_options :
	{ 
		categorie : '', 
		libelle : '', 
		descriptif : '',
		date : '',
		heure : '',
		lieu : '',
		lien : 'Chronokiffer cet evenement',
		lienSite : 'http://',
		libelleLienSite : ''
	},
	
	_mode_2_options :
	{
		idCK : 0,
		annee : 0,
		mois : 0,
		jour : 0,
		heure : 0,
		minute : 0,
		voirLien : 1,
		lien : 'Chronokiffer cet evenement',
		lienSite : 'http://',
		couleur: 'transparent',
		texteCouleur: 'FF7E00',
		texteCouleur2: 'FFFFFF',
		fondCouleur: '',
		jourLettre: 'J',
		taille : 1,
    ckGagne : 0,
    otlk : 0,
    lienPos: 0
    //charset : 'ISO-8859-1'
	},
	
	_url_data :
	{
		encodedData : null,
		final : null
	},
	
	_do_render : false,
	
	base64 : 
	{
	
		_keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",		// private property
	 
		encode : function (input) {		// public method for encoding
			var output = "";
			var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
			var i = 0;
			input = this._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) {	// private method for UTF-8 encoding
			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;
		},
    
  	_utf8_decode : function (utftext) { // public method for url decoding
  		var string = "";
  		var i = 0;
  		var c = c1 = c2 = 0;
   
  		while ( i < utftext.length ) {
   
  			c = utftext.charCodeAt(i);
   
  			if (c < 128) {
  				string += String.fromCharCode(c);
  				i++;
  			}
  			else if((c > 191) && (c < 224)) {
  				c2 = utftext.charCodeAt(i+1);
  				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
  				i += 2;
  			}
  			else {
  				c2 = utftext.charCodeAt(i+1);
  				c3 = utftext.charCodeAt(i+2);
  				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
  				i += 3;
  			}
   
  		}
   
  		return string;
  	}
	},
	
	init : function(APIOptions,onlyInit) 
	{
		_do_render = false;
		
		if(isNaN(APIOptions.mode) || APIOptions.mode > 2)
		{
			alert("API Chronokif : Vous devez spcifier un mode pour l'API");
			return false;
		}
		
		this._referer = window.location  + '';
		
		this._mode = APIOptions.mode;
		
		if(this._mode == 1)
		{
			this._mode_1_options.categorie = APIOptions.categorie != null ? APIOptions.categorie : '';
			this._mode_1_options.libelle = APIOptions.libelle != null ? APIOptions.libelle : '';
			this._mode_1_options.descriptif = APIOptions.descriptif != null ? APIOptions.descriptif : '';
			this._mode_1_options.date = APIOptions.date != null ? APIOptions.date : '';
			this._mode_1_options.heure = APIOptions.heure  != null ? APIOptions.heure : '';
			this._mode_1_options.lieu = APIOptions.lieu != null ? APIOptions.lieu : '';
			this._mode_1_options.lien = APIOptions.lien != null ? APIOptions.lien : '';
			this._mode_1_options.lienSite = APIOptions.lienSite != null ? APIOptions.lienSite : '';
			this._mode_1_options.libelleLienSite = APIOptions.libelleLienSite != null ? APIOptions.libelleLienSite : '';
			
			this._url_data.encodedData = this.base64.encode("cat=" + encodeURIComponent(this._mode_1_options.categorie) + "&lib=" + encodeURIComponent(this._mode_1_options.libelle) + "&des=" + encodeURIComponent(this._mode_1_options.descriptif) + "&dat=" + encodeURIComponent(this._mode_1_options.date) +"&heu=" + encodeURIComponent(this._mode_1_options.heure) + "&lie=" + encodeURIComponent(this._mode_1_options.lieu) + "&ref=" + encodeURIComponent(this._referer) + "&lis=" + encodeURIComponent(this._mode_1_options.lienSite) + "&lls=" + encodeURIComponent(this._mode_1_options.libelleLienSite));
			this._url_data.final = "http://www.chronokif.fr/index.php?module=api&cont=redirect&d=" + this._url_data.encodedData;
			
			document.write(this.renderMode1());
			
		}
		else if(this._mode == 2)
		{
			if(isNaN(APIOptions.CK))
			{
				alert("API Chronokif : Vous devez spécifier un identifiant CK");
				return false;
			}
			
			this._mode_2_options.idCK = APIOptions.CK;
			
			this._mode_2_options.annee = isNaN(APIOptions.annee) ? 0 : APIOptions.annee;
			this._mode_2_options.mois = isNaN(APIOptions.mois) ? 0 : APIOptions.mois;
			this._mode_2_options.jour = isNaN(APIOptions.jour) ? 0 : APIOptions.jour;
			this._mode_2_options.heure = isNaN(APIOptions.heure) ? 0 : APIOptions.heure;
			this._mode_2_options.minute = isNaN(APIOptions.minute) ? 0 : APIOptions.minute;
			
			this._mode_2_options.lien = APIOptions.lien != null ? APIOptions.lien : '';
			this._mode_2_options.lienSite = APIOptions.lienSite != null ? APIOptions.lienSite : '';
			this._mode_2_options.voirLien = isNaN(APIOptions.voirLien) && APIOptions.voirLien > 2 ? 1 : APIOptions.voirLien;
			this._mode_2_options.couleur = APIOptions.couleur != null ? APIOptions.couleur : 'transparent';
			this._mode_2_options.texteCouleur = APIOptions.texteCouleur != null ? APIOptions.texteCouleur : 'FF7E00';
			this._mode_2_options.texteCouleur2 = APIOptions.texteCouleur2 != null ? APIOptions.texteCouleur2 : 'FFFFFF';
			this._mode_2_options.taille = APIOptions.taille != null ? APIOptions.taille : '1';
			this._mode_2_options.forme = APIOptions.forme != null ? APIOptions.forme : '1';
			this._mode_2_options.fondCouleur = APIOptions.fondCouleur != null ? APIOptions.fondCouleur : '';
			this._mode_2_options.jourLettre = APIOptions.jourLettre != null ? APIOptions.jourLettre : 'J';
      this._mode_2_options.ckGagne = APIOptions.ckGagne != null ? APIOptions.ckGagne : 0;
      this._mode_2_options.otlk = APIOptions.otlk != null ? APIOptions.otlk : 0;
      this._mode_2_options.lienPos = APIOptions.lienPos != null ? APIOptions.lienPos : 0;
      //this._mode_2_options.charset = APIOptions.charset != null ? APIOptions.charset : 'ISO-8859-1';
			
			this._url_data.encodedData = this.base64.encode("idck=" + encodeURIComponent(this._mode_2_options.idCK) + "&ref=" + encodeURIComponent(this._referer) + "&ls=" + encodeURIComponent(this._mode_2_options.lienSite));
			this._url_data.final = "http://www.chronokif.fr/index.php?module=api&cont=mode2&d=" + this._url_data.encodedData;
			
      if(this._mode_2_options.otlk == 1)
      {
        this._url_data.final = "http://www.chronokif.fr/index.php?cont=chronokif&action=outlook&idc=" + this._mode_2_options.idCK;
      }
      
      if(onlyInit == null || onlyInit == false)
      {
        document.write(this.renderMode2());
      }
		}
	},
	
	renderMode1 : function()
	{
		var buffer = "<style type='text/css'>a.blocLienChronokif { color:#939865; text-align:center; }</style>";
		buffer += "<div>";
		buffer += "<img style='padding-top:3px' src='http://www.chronokif.fr/img/front/ajout_widget_logo_mini.gif' />";
		buffer += "<a href='" + this._url_data.final + "' class='blocLienChronokif'>" + this._mode_1_options.lien + "</a>";
		buffer += "</div>";
		return buffer;
	},
	
	renderMode2 : function()
	{
		var parameters = this.getAnimationParameters();
		var css = 'text-align:center;'
    
    if(parseInt(this._mode_2_options.lienPos) == 1)
    {
      css = 'margin-left: 55px; margin-top: -36px;';
    }
    
		var buffer = "<style type='text/css'>a.blocLienChronokif { color:#"+encodeURIComponent(this._mode_2_options.texteCouleur2)+"; display: block; " + css + "}</style>";
		buffer += "<div>";
		buffer += "<object height='" + parameters.height + "' width='" + parameters.width + "' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0' classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000'>";
		buffer += "<param value=\"" + parameters.url + "\" name='movie'/>";
		buffer += "<param value='high' name='quality'/>";
		buffer += "<param value='transparent' name='wmode'/>";
		buffer += "<param name='FlashVars' value=''>";
		buffer += "<embed height='" + parameters.height + "' width='" + parameters.width + "' type='application/x-shockwave-flash' pluginspage='http://www.macromedia.com/go/getflashplayer' wmode='transparent' quality='high' src=\"" + parameters.url + "\" />";
		buffer += "</object>";
		
    //var texteLien = this._mode_2_options.charset == 'ISO-8859-1' ? this.base64._utf8_encode(this._mode_2_options.lien) : this._mode_2_options.lien;
    var texteLien = this._mode_2_options.lien;
    
		if(this._mode_2_options.voirLien == 1)
		{
			buffer += "<a href='" + this._url_data.final + "' class='blocLienChronokif' target='_blank'>" + texteLien + "</a>";
		}
    
    if(this._mode_2_options.ckGagne == 1)
		{
			buffer += "<a href='" + this._url_data.final + "' class='blocLienChronokif'><img alt='Chronokife et Gagne!' border='0' src='http://www.chronokif.fr/img/front/macaron_gagne_mini.png'" + texteLien + " /></a>";
		}
		
		buffer += "</div>";
		
		return buffer;
	},
  
  getAnimationParameters : function()
  {
    var urlSite = 'http://www.chronokif.fr/img/front/swf/';
		var animParams = "annee=" + this._mode_2_options.annee;
    animParams += "&mois=" + this._mode_2_options.mois;
    animParams += "&jour=" + this._mode_2_options.jour;
    animParams += "&heure=" + this._mode_2_options.heure;
    animParams += "&minute=" + this._mode_2_options.minute;
    animParams += "&seconde=00";
    animParams += "&url=" + encodeURIComponent(this._url_data.final);
    animParams += "&couleur=" + encodeURIComponent(this._mode_2_options.couleur);
    animParams += "&txtCouleur=" + encodeURIComponent(this._mode_2_options.texteCouleur);
    animParams += "&txtCouleur2=" + encodeURIComponent(this._mode_2_options.texteCouleur2);
    animParams += "&fondCouleur=" + encodeURIComponent(this._mode_2_options.fondCouleur);
    animParams += "&jourLettre=" + this._mode_2_options.jourLettre;
        
		if(this._mode_2_options.taille == 2 && this._mode_2_options.forme == 1)
		{
			var animFile = "CK_API_Widget_180.swf";
			var widthAnim = 180;
			var heightAnim = 43;
		}
		else if(this._mode_2_options.taille == 1 && this._mode_2_options.forme == 1)
		{
			var animFile = "CK_API_Widget.swf";
			var widthAnim = 308;
			var heightAnim = 74;
		}
		else if(this._mode_2_options.taille == 3 && this._mode_2_options.forme == 1)
		{
			var animFile = "CK_API_Widget_244.swf";
			var widthAnim = 244;
			var heightAnim = 58;
		}
		else if(this._mode_2_options.taille == 2 && this._mode_2_options.forme == 2)
		{
			var animFile = "CK_API_Widget_116.swf";
			var widthAnim = 116;
			var heightAnim = 43;
		}
		else if(this._mode_2_options.taille == 1 && this._mode_2_options.forme == 2)
		{
			var animFile = "CK_API_Widget_189.swf";
			var widthAnim = 189;
			var heightAnim = 74;
		}
		else if(this._mode_2_options.taille == 3 && this._mode_2_options.forme == 2)
		{
			var animFile = "CK_API_Widget_142.swf";
			var widthAnim = 142;
			var heightAnim = 58;
		}
    
    return {'url' : urlSite + animFile + "?" + animParams, 'width' : widthAnim, 'height': heightAnim};
  } 
}
