function onLoad() {
	// Obtient l'url
	/(http:\/\/[^\?]*)\??/.test(document.URL);

	var catUrl   = RegExp.$1;
	var catBook  = swfobject.getQueryParamValue("book");
	var catPage  = swfobject.getQueryParamValue("page");
	var catMarks = swfobject.getQueryParamValue("marks");

	if (typeof window.book != 'undefined') catBook = book;
	if (typeof window.page != 'undefined') catPage = page;

	var flashVars = {
		book  			: catBook,
		page  			: catPage,
		marks 			: catMarks,
		urlrewriting 	: true,
		url			 	: catUrl
	}

	var params = {}
		params.allowfullscreen = true;	
	
	swfobject.embedSWF("htMain.swf", "flashContent", "100%", "100%", "8.0.23", null, flashVars, params);

}

window.onload = onLoad;

// -------------------------------------------------------------------------------------
// Spécifiques aux ouvertures de fenêtres
// -------------------------------------------------------------------------------------

/* Spécifique à la géo localisation */
function renderGeoLoc(lat, lng, txt, img) {
	openWindow('ext/geoLoc/index.html?lat=' + lat + '&lng=' + lng + '&txt=' + txt, 650, 595);
}

/* ouverture video en pop up */
function renderVideo() {
	openWindow('ext/video/video.asp',400, 390);
}

function openWindow(url, width, height) {
	var _stageWidth  = screen.width;
	var _stageHeight = screen.height;

	var _left = (_stageWidth - width)/2;
	var _top  = (_stageHeight - height) /2;

	window.open(url,'popup','left=' + _left + ',' +
				'top=' + _top + ',' +
				'width=' + width + ',' +
				'height=' + height + ',' +
				'scrollbars=no, resizable=no, toolbar=no, location=no, status=no');
}

// -------------------------------------------------------------------------------------
// Spécifiques aux bookmarks
// -------------------------------------------------------------------------------------

function setBookmarks(bookmarks, book) {
	setCookie('book' + book, bookmarks);
}

function getBookmarks(book) {
	var _ret = getCookie('book' + book);
	if (_ret) {
		return _ret.split(',');
	} else 
		return null;
}

// -------------------------------------------------------------------------------------
// Spécifiques aux cookies
// -------------------------------------------------------------------------------------

function setCookie(name, value) { 
	var date = new Date();
		date.setFullYear(date.getFullYear()+1);
		document.cookie = name + "=" + escape(value) + "; expires=" + date.toGMTString();
}

function getCookieVal(offset) {  
	var endstr = document.cookie.indexOf(";", offset);  
	if (endstr == -1)    
		endstr = document.cookie.length;  
		return unescape(document.cookie.substring(offset, endstr));
}

function getCookie(name) {  
	var arg = name + "=";  
	var alen = arg.length;  
	var clen = document.cookie.length;  

	var i = 0;  
	while (i < clen) {    
		var j = i + alen;    
		if (document.cookie.substring(i, j) == arg)      
			return getCookieVal (j);    	
			i = document.cookie.indexOf(" ", i) + 1;    
			if (i == 0) break;   
		}  
	return '';
}

// -------------------------------------------------------------------------------------
// Spécifique à l'éditeur
// -------------------------------------------------------------------------------------

function getText(text) {
	var _ret = '';
	
	// On récupère seulement certains caractères
	var _matches = text.toLowerCase().match(/[\w-\\\.\s\'@œçàâéèêëùûüîïô]*/g); 

	// On concatène les correspondances
	for (var _i = 0; _i<_matches.length; _i++) _ret += _matches[_i];
	
	return(_ret);
}

function getMatches(text, pattern, replace) {
	// On décode la chaine
	pattern = unescape(pattern);
	// On la convertie à la volée en object
	pattern = eval(pattern);
	// On récupère les correspondances
	var _matches = text.match(pattern);
	
	for (var _i=0; _i<_matches.length; _i++) {
		// On récupère les remplacements
		var linkText = _matches[_i];
		var linkUrl  = linkText.replace(pattern, unescape(replace));
		_matches[_i] = [];
		_matches[_i][0]= linkText;
		_matches[_i][1]= linkUrl;
	}
	
	return _matches;
}


// -------------------------------------------------------------------------------------
// Spécifiques à la recherche
// -------------------------------------------------------------------------------------

function clean(text) {
    temp = text.replace(/[àáâ]/gi,"a")
	temp = temp.replace(/[ç]/gi,"c")
    temp = temp.replace(/[éèêë]/gi,"e")
    temp = temp.replace(/[îï]/gi,"i")
    temp = temp.replace(/[ô]/gi,"o")
    temp = temp.replace(/[ùû]/gi,"u")	 
	temp = temp.toLowerCase();
	return temp;
}


// -------------------------------------------------------------------------------------
// Spécifiques à analytics
// -------------------------------------------------------------------------------------

function trackPage(provider, account, bookIndex, page, cover) {
	provider = provider.toLowerCase();

	// on récupère l'instance du tracker
	if (provider=='google') var _tracker = _gat._getTracker(account);
	
	for (var _i=0; _i<=((!cover) ? 1 : 0); _i++) {
		// On définie l'url de la page
		//_url = "/" + bookIndex  + "-" + (page+_i) + "/" + "page" + (page+_i) + ".html";
		_url = "/page " + (page+_i);

		switch (provider) {
			case 'google':
				_tracker._trackPageview(_url);
				break;
		 }
	}
}

function trackEvent(provider, account, category, action) {
	switch(provider.toLowerCase()) {
		case 'google':
			// on récupère l'instance du tracker
			var _tracker = _gat._getTracker(account);		
			// On track l'évènement
			_tracker._initData();
			_tracker._trackEvent(category, action);
			break;
	}
}
