/**
@brief Questo file si occupa di gestire il flash che verificherà se lo user id
       salvato nel cookie è uguale a quello salvato nell'lso.

       Viene insesrito a fine pagina il tag del flash. Quando è caricato, gli
       viene inviata la richiesta di confronto tra l'id creato dal php
       (/lib/user_tracker.php) else l'lso di flash: se i due id sono diversi
       viene data precedenza abstract quello di flash che va a sovrascrivere il
       cookie precedentemete salvato

@require global.js, cookie.js
*/

var flashVars = new Array();
var mid;

if (0) {
    var swf_width = '600';
    var swf_height = '700';
} else {
    var swf_width = '1';
    var swf_height = '1';
}

/**
@public
@brief inserisce nella pagina il tag del flash
@todo ampliare la funzion per poter prendere come parametro il percorso dell'swf e il nome
*/
function WriteFlashTag() {
	var swfURL = 'http://' + document.domain + '/swf/cookie.swf';
	var tag = document.createElement('object');
	var par = document.createElement('param');
	par.setAttribute('name', 'Movie');
	par.setAttribute('value', swfURL);
	tag.appendChild(par);
	var par = document.createElement('param');
	par.setAttribute('name', 'FlashVars');
	par.setAttribute('value', '');
	tag.appendChild(par);

	if (navigator.mimeTypes && navigator.mimeTypes["application/x-shockwave-flash"]) {
		var embed = document.createElement('embed');
		embed.setAttribute('src', swfURL);
		embed.setAttribute('quality', 'high');
		embed.setAttribute('FlashVars', '');
        embed.setAttribute('width', swf_width);
        embed.setAttribute('height', swf_height);
		embed.setAttribute('name', 'cookie_flash');
		embed.setAttribute('type', 'application/x-shockwave-flash');
		tag.appendChild(embed);
	}

	tag.setAttribute('classid', 'clsid:D27CDB6E-AE6D-11cf-96B8-444553540000');
	tag.setAttribute('codebase', 'http://fpdownload.macromedia.com/flash/cabs/swflash.cab#version=7,0,0,0');
	tag.setAttribute('id', 'cookie_flash');
	tag.setAttribute('width', swf_width);
	tag.setAttribute('height', swf_height);

	document.body.appendChild(tag);
	if (navigator.appName == 'Opera') {
		document.body.innerHTML = document.body.innerHTML; // per browser Opera
    }
}


/**
@public
@brief test che verifica la possibilità di scrivere i cookie
@return @c true  @c false

ne viene scritto uno e se lo restituisce allora sono abilitati i cooki3
*/
function VerifyJsCookieEnabled() {
	SetCookie('testBS', '0', .001, '/', GetTopLevelDomain()); // vita di pochi minuti
	return GetCookie('testBS') ? true : false;
}


/**
@public
@brief verifica se può essere eseguito un flash
@return @c true o @c false
*/
function VerifyFlashPlugin() {
	if (("Microsoft Internet Explorer" == navigator.appName && -1 == navigator.appVersion.indexOf("Mac") && -1 == navigator.appVersion.indexOf("3.1")) ||
		(navigator.plugins && (navigator.plugins["Shockwave Flash"] || navigator.plugins["Shockwave Flash 2.0"]))) {
		return true; // flash enabled
	} else {
		return false; // flash disabled
	}
}

/**
@public
@brief verifica se è presente il cookie mid che contiene la machine id
@return @c true o @c false
*/
function VerifyJsCookieExists() {
	return GetCookie('mid').length > 0 ? true : false;
}


/**
@private

Verifica che i cookie e flash siano abilitati.
*/
function VerifyCookieAndFlash() {
	if (VerifyJsCookieEnabled()) {
		if (VerifyJsCookieExists()) {
            mid = GetCookie('mid');
		}
	} else {
        // cookie non abilitati
		return false;
	}
	if (! VerifyFlashPlugin()) {
		// impossibile scrivere il flash
		return false;
	}
}

/**
@public
@brief aggiunge una variabile al param e object del flash
@param mane nome della variabile
@param value valore della variabile
@idSwf id dell swf al quale passare la variabile
*/
function AddFlashVar(name, value, idSwf) {
	var varToAdd = '&'+ name +'='+ escape(value)
	var swf = GetFlashMovieObject(idSwf);
	GetFlashMovieObject(idSwf).SetVariable('_level0.'+ name , value);
}

/**
@public
@brief restituisce l'oggetto dell flash rispetto all'id passati
*/
function GetFlashMovieObject(movieName) {
	if (window.document[movieName]) {
		return window.document[movieName];
    }

    if (-1 == navigator.appName.indexOf("Microsoft Internet")) {
        if (document.embeds && document.embeds[movieName]) {
			return document.embeds[movieName];
        }
    } else {
		return document.getElementById(movieName);
    }
	return false;
}


/**
@private
@brief oggetto per variabili di flash
*/
function FlashCom() {
	this.loaded        = false;
	this.tentativi     = 40;  // num di volte prima che scada il check dell'mid del flash
	this.millisecRetry = 100; // tempo tra un tentativo e l'altro
	this.mid_lso       = '';  // mid del flash
	this.timeInterval;
}
var flash = new FlashCom();


/**
@private
@brief verifica che il flash sia caricato
*/
function CheckLoadedFlash() {
	flash.timeInterval = setInterval('CheckInterval()', flash.millisecRetry);
}


/**
@private
@brief funzione che verifica che il flash sia caricato

Per verificare che sia caricato, il flash setta la variabile this.loaded = true che di default è false. Quando verifica che è caricato setta delle variabili per far iniziare la verifica dell'lso
*/
function CheckInterval() {
	flash.tentativi--;
	if(true == flash.loaded) {
		AddFlashVar('topDomain', GetTopLevelDomain(), 'cookie_flash');
		AddFlashVar('getLSO', '1', 'cookie_flash');
		AddFlashVar('midjs', GetCookie('mid'), 'cookie_flash');
		VerifyCookieAndFlash();
		clearInterval(flash.timeInterval);
	}
	if (0 == flash.tentativi) {
		clearInterval(flash.timeInterval);
    }
}

/**
@private
@bref setta l'mid dello user

Funzione richiamata dal flash quando vede che l'id salvato nell'lso è diverso da quello passato dal js
*/
function SetNewId(midlso) {
	if ('' != midlso) {
		SetCookie('mid', midlso, 300, '/', GetTopLevelDomain());
	}
}

AddOnload(WriteFlashTag);
AddOnload(CheckLoadedFlash);

