var CKAPI = 
{
	
	_mode : 1,
	_referer : null,
	
	_mode_1_options :
	{ 
		categorie : '', 
		libelle : '', 
		descriptif : '',
		date : '',
		heure : '',
		lieu : '',
		lien : 'Chronokiffer cet évenement',
		lienSite : 'http://',
		libelleLienSite : ''
	},
	
	_mode_2_options :
	{
		idCK : 0,
		annee : 0,
		mois : 0,
		jour : 0,
		heure : 0,
		minute : 0,
		voirLien : 1,
		lien : 'Chronokiffer cet évenement',
		lienSite : 'http://',
		libelleLienSite : '',
		couleur: 'transparent',
		texteCouleur: 'FF7E00',
		texteCouleur2: 'FFFFFF',
		fondCouleur: '',
		jourLettre: 'J',
		taille : 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;
		}
	},
	
	init : function(APIOptions) 
	{
		_do_render = false;
		
		if(isNaN(APIOptions.mode) || APIOptions.mode > 2)
		{
			alert("API Chronokif : Vous devez spécifier 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;
			
			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.libelleLienSite = APIOptions.libelleLienSite != null ? APIOptions.libelleLienSite : '';
			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.fondCouleur = APIOptions.fondCouleur != null ? APIOptions.fondCouleur : '';
			this._mode_2_options.jourLettre = APIOptions.jourLettre != null ? APIOptions.jourLettre : 'J';
			
			this._url_data.encodedData = this.base64.encode("idck=" + encodeURIComponent(this._mode_2_options.idCK) + "&ref=" + encodeURIComponent(this._referer) + "&lis=" + encodeURIComponent(this._mode_2_options.lienSite) + "&lls=" + encodeURIComponent(this._mode_2_options.libelleLienSite));
			this._url_data.final = "http://www.chronokif.fr/index.php?module=api&cont=mode2&d=" + this._url_data.encodedData;
			
			this.renderMode2();
		}
		
	},
	
	renderMode1 : function()
	{
		var buffer = "<style type='text/css'>a.blocLienChronokif { color:#939865; }</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>";
		document.write(buffer);
	},
	
	renderMode2 : function()
	{
		var urlSite = 'http://www.chronokif.fr/img/front/swf/';
		
		if(this._mode_2_options.taille == 2)
		{
			var paramsAnim = urlSite + "CK_API_Widget_180.swf?annee=" + this._mode_2_options.annee + "&mois=" + this._mode_2_options.mois + "&jour=" + this._mode_2_options.jour + "&heure=" + this._mode_2_options.heure + "&minute=" + this._mode_2_options.minute + "&seconde=00&url=" + encodeURIComponent(this._url_data.final) + "&couleur=" + encodeURIComponent(this._mode_2_options.couleur) + "&txtCouleur=" + encodeURIComponent(this._mode_2_options.texteCouleur) + "&txtCouleur2=" + encodeURIComponent(this._mode_2_options.texteCouleur2) + "&fondCouleur=" + encodeURIComponent(this._mode_2_options.fondCouleur)  + "&jourLettre=" + encodeURIComponent(this._mode_2_options.jourLettre);
			var widthAnim = 180;
			var heightAnim = 43;
		}
		else
		{
			var paramsAnim = urlSite + "CK_API_Widget.swf?annee=" + this._mode_2_options.annee + "&mois=" + this._mode_2_options.mois + "&jour=" + this._mode_2_options.jour + "&heure=" + this._mode_2_options.heure + "&minute=" + this._mode_2_options.minute + "&seconde=00&url=" + encodeURIComponent(this._url_data.final) + "&couleur=" + encodeURIComponent(this._mode_2_options.couleur) + "&txtCouleur=" + encodeURIComponent(this._mode_2_options.texteCouleur) + "&txtCouleur2=" + encodeURIComponent(this._mode_2_options.texteCouleur2) + + "&fondCouleur=" + encodeURIComponent(this._mode_2_options.fondCouleur)  + "&jourLettre=" + encodeURIComponent(this._mode_2_options.jourLettre);
			var widthAnim = 308;
			var heightAnim = 74;
		}
		
		var buffer = "<style type='text/css'>a.blocLienChronokif { color:#939865; }</style>";
		buffer += "<div>";
		buffer += "<object height='" + heightAnim + "' width='" + widthAnim + "' 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=\"" + paramsAnim + "\" name='movie'/>";
		buffer += "<param value='high' name='quality'/>";
		buffer += "<param value='transparent' name='wmode'/>";
		buffer += "<param name='FlashVars' value=''>";
		buffer += "<embed height='" + heightAnim + "' width='" + widthAnim + "' type='application/x-shockwave-flash' pluginspage='http://www.macromedia.com/go/getflashplayer' wmode='transparent' quality='high' src=\"" + paramsAnim + "\" />";
		buffer += "</object>";
		
		if(this._mode_2_options.voirLien == 1)
		{
			buffer += "<a href='" + this._url_data.final + "' class='blocLienChronokif'>" + this._mode_2_options.lien + "</a>";
		}
		
		buffer += "</div>";
		
		document.write(buffer);
	}
	
}