// JavaScript Document

function ObjComptador(Propietats) {
	this.iframe_id = 'iframe_Comptador';								// Propietat id del element iframe del comptador
	this.iframe_Generat = false;												// Element iframe del comptador generat
	this.URL = "";																			// Url del fitxer php que inicia el comptador
	this.Parametres = new Array();											// Taula per contenir els paràmetres
	this.Parametres['url'] = document.URL;							// Url del document actual
	this.Parametres['referrer'] = document.referrer;		// Url del document que ens ha portat al document actual
	this.Parametres['incrementar'] = 'si';							// 'no' o qualsevol altra cosa. Defecte 'si'
	this.Parametres['text_previ'] = '';									// Text previ al número retornat. Defecte ''
	this.Parametres['text_posterior'] = '';							// Text posterior al número retornat. Defecte ''
	this.Parametres['text_titol'] = '';									// Text que es mostrarà en passar per sobre del número retornat. Defecte ''
	this.Parametres['idioma'] = 'ca';										// 'es', 'en' o qualsevol altra cosa. Defecte 'ca'
	this.Parametres['mostrar'] = 'text';								// 'no', 'text', 'img'. Defecte 'text'
	this.Parametres['referrer_params'] = 'si';					// 'si' o qualsevol altra cosa. Defecte 'si'
	this.Parametres['align'] = 'left';									// 'left', 'center', 'right'. Defecte 'left'
	this.Parametres['color_fons'] = 'FFFFFF';						// Format 'RRGGBB' en hexadecimal. Defecte 'FFFFFF'
	this.Parametres['color_text'] = '000000';						// Format 'RRGGBB' en hexadecimal. Defecte '000000'
	this.Parametres['mida_font'] = '10';								// Entre 9 i 20, ambdos inclossos. Defecte 10
	this.Parametres['font_bold'] = '';									// 'si' o qualsevol altra cosa. Defecte ''
	this.Parametres['font_italic'] = '';								// 'si' o qualsevol altra cosa. Defecte ''
	this.Parametres['format_angles'] = '';							// 'si' o qualsevol altra cosa. Defecte ''
	
	if (typeof(Propietats) != 'undefined') {
		if (typeof(Propietats.url) != 'undefined') this.Parametres['url'] = String(Propietats.url);
		if (typeof(Propietats.referrer) != 'undefined') this.Parametres['referrer'] = String(Propietats.referrer);
		if (typeof(Propietats.incrementar) != 'undefined') this.Parametres['incrementar'] = String(Propietats.incrementar);
		if (typeof(Propietats.text_previ) != 'undefined') this.Parametres['text_previ'] = String(Propietats.text_previ);
		if (typeof(Propietats.text_posterior) != 'undefined') this.Parametres['text_posterior'] = String(Propietats.text_posterior);
		if (typeof(Propietats.text_titol) != 'undefined') this.Parametres['text_titol'] = String(Propietats.text_titol);
		if (typeof(Propietats.idioma) != 'undefined') this.Parametres['idioma'] = String(Propietats.idioma);
		if (typeof(Propietats.mostrar) != 'undefined') this.Parametres['mostrar'] = String(Propietats.mostrar);
		if (typeof(Propietats.referrer_params) != 'undefined') this.Parametres['referrer_params'] = String(Propietats.referrer_params);
		if (typeof(Propietats.align) != 'undefined') this.Parametres['align'] = String(Propietats.align);
		if (typeof(Propietats.color_fons) != 'undefined') this.Parametres['color_fons'] = String(Propietats.color_fons);
		if (typeof(Propietats.color_text) != 'undefined') this.Parametres['color_text'] = String(Propietats.color_text);
		if (typeof(Propietats.mida_font) != 'undefined') this.Parametres['mida_font'] = String(Propietats.mida_font);
		if (typeof(Propietats.font_bold) != 'undefined') this.Parametres['font_bold'] = String(Propietats.font_bold);
		if (typeof(Propietats.font_italic) != 'undefined') this.Parametres['font_italic'] = String(Propietats.font_italic);
		if (typeof(Propietats.format_angles) != 'undefined') this.Parametres['format_angles'] = String(Propietats.format_angles);
	}

}

ObjComptador.prototype.Generar = function() {
	
	var FontSize = parseInt(this.Parametres['mida_font']);				// Número entre 9 i 20, ambdos inclossos. Defecte 9
	if ((FontSize < 9) || (FontSize > 20)) {
		FontSize = 9;
		this.Parametres['mida_font'] = String(FontSize);
	}
	
	this.GenerarURL();

	var MidaFrame = 0;
	if (this.Parametres['mostrar'] != 'no') MidaFrame = (this.Parametres['mostrar'] == 'img') ? 22 : FontSize + 2;
	if (this.iframe_Generat) {
		var iframe_element = document.getElementById(this.iframe_id);
		if (iframe_element) {
			iframe_element.height = MidaFrame;
			iframe_element.align = this.Parametres['align'];
			iframe_element.src = this.URL;
		}
	} else {
		var HTML_text = '<iframe id="' + this.iframe_id + '" height="' + MidaFrame + '"';
		HTML_text += ' align="' + this.Parametres['align'] + '"';
		HTML_text += ' scrolling="no" name="Comptador" frameborder="0" title="Comptador"';
		HTML_text += ' src="' + this.URL + '"></iframe>';
		//window.alert(this.URL);
		document.writeln(HTML_text);
		this.iframe_Generat = true;
	}
}

ObjComptador.prototype.GenerarURL = function() {
	var Param = new Array();
	
	Param['url'] = encodeURIComponent(this.Parametres['url']);
	Param['referrer'] = encodeURIComponent(this.Parametres['referrer']);
	if (this.Parametres['incrementar'] != '') Param['incrementar'] = encodeURIComponent(this.Parametres['incrementar']);
	if (this.Parametres['text_previ'] != '') Param['text_previ'] = encodeURIComponent(this.Parametres['text_previ']);
	if (this.Parametres['text_posterior'] != '') Param['text_posterior'] = encodeURIComponent(this.Parametres['text_posterior']);
	if (this.Parametres['text_titol'] != '') Param['text_titol'] = encodeURIComponent(this.Parametres['text_titol']);
	if (this.Parametres['idioma'] != '') Param['idioma'] = encodeURIComponent(this.Parametres['idioma']);
	if (this.Parametres['mostrar'] != '') Param['mostrar'] = encodeURIComponent(Trim(this.Parametres['mostrar']));
	if (this.Parametres['referrer_params'] != '') Param['referrer_params'] = encodeURIComponent(Trim(this.Parametres['referrer_params']));
	if (this.Parametres['align'] != '') Param['align'] = encodeURIComponent(Trim(this.Parametres['align']));
	if (this.Parametres['color_fons'] != '') Param['color_fons'] = encodeURIComponent(Trim(this.Parametres['color_fons']));
	if (this.Parametres['color_text'] != '') Param['color_text'] = encodeURIComponent(Trim(this.Parametres['color_text']));
	if (this.Parametres['mida_font'] != '') Param['mida_font'] = Trim(this.Parametres['mida_font']);
	if (this.Parametres['font_bold'] != '') Param['font_bold'] = encodeURIComponent(Trim(this.Parametres['font_bold']));
	if (this.Parametres['font_italic'] != '') Param['font_italic'] = encodeURIComponent(Trim(this.Parametres['font_italic']));
	if (this.Parametres['format_angles'] != '') Param['format_angles'] = encodeURIComponent(Trim(this.Parametres['format_angles']));

	this.URL = "/sl/comptador/comptar_ini.php?";
	
	var n = 0;
	for (var NomParametre in Param) {
		if (Param[NomParametre] != "") {
			if (n > 0) this.URL += '&';
			this.URL += NomParametre + '=' + Param[NomParametre];
			n++;
		}
	}
}

function Trim(Cadena) {
	while (Cadena.substr(0, 1) == ' ') {Cadena = Cadena.substr(1);}
	while (Cadena.substr(Cadena.length - 1) == ' ') {Cadena = Cadena.substring(Cadena.length - 1, 0);}
	return Cadena;
}

var PropietatsComptador = {
//	url: document.URL,
//	referrer: document.referrer,
	incrementar: 'si',
	text_previ: '',
	text_posterior: 'visites',
	text_titol: 'Comptador',
	idioma: 'ca',
	mostrar: 'text',
	referrer_params: 'si',
	align: 'right',
	color_fons: 'FFFFFF',
	color_text: '999999',
	mida_font: '9',
	font_bold: '',
	font_italic: '',
	format_angles: ''
};

if (!Llengua) var Llengua = "ca";

switch(Llengua) {
	case "es":
		PropietatsComptador.text_posterior = 'visitas';
		PropietatsComptador.text_titol = 'Contador';
		PropietatsComptador.idioma = 'es';
		break;
	case "en":
		PropietatsComptador.text_posterior = 'visits';
		PropietatsComptador.text_titol = 'Counter';
		PropietatsComptador.idioma = 'en';
		PropietatsComptador.format_angles = 'si';
		break;
	case "ca":
		break;
	default:
	Llengua = "ca";
}