function getUrlParameter( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}

var mw_flashMovieId = "AveBrowser"; // the id/name of your flash app's HTML DOM element     
var mw_flashPlayerId = "externalPlayer"; // the id/name of your flash app's HTML DOM element     

//kind = {none|search|detail}

var _NPages={
	//"248":{		url :"home",			id	:"248",		pid	:"none",	kind	:"none",		y:0		},
	"257":{		url :"accueil",		id	:"257",		pid	:"none",	kind	:"none",		y:0		},
		"243":{		url :"caddy",			id	:"243",		pid	:"257",		kind	:"none",		y:342	},
		"249":{		url :"recherche",		id	:"249",		pid	:"257",		kind	:"search",		y:342	},
		"226":{		url :"inscription",	id	:"226",		pid	:"257",		kind	:"none",		y:342	},
		"223":{		url :"contact",		id	:"223",		pid	:"257",		kind	:"none",		y:342	},
	
		"239":{		url :"meilleur-ventes",id	:"239",		pid	:"257",		kind	:"none",		y:342	},
		"213":{		url :"nouveautes",		id	:"213",		pid	:"257",		kind	:"none",		y:342	},
		"229":{		url :"selection",		id	:"229",		pid	:"257",		kind	:"none",		y:342	},
		"231":{		url :"jeunesse",		id	:"231",		pid	:"257",		kind	:"none",		y:342	},
		"233":{		url :"comics",			id	:"233",		pid	:"257",		kind	:"none",		y:342	},
		"244":{		url :"humour",			id	:"244",		pid	:"257",		kind	:"none",		y:342	},
		"253":{		url :"manga",			id	:"253",		pid	:"257",		kind	:"none",		y:342	},
		"256":{		url :"aventure",		id	:"256",		pid	:"257",		kind	:"none",		y:342	},
		"288":{		url :"roman-graphique",	id	:"288",		pid	:"257",		kind	:"none",		y:342	},
	
		"267":{		url :"science-fiction",				id	:"267",		pid	:"257",		kind	:"none",		y:342	},
		"269":{		url :"moins-de-1",			id	:"269",		pid	:"257",		kind	:"none",		y:342	},
		"270":{		url :"moins-de-5",			id	:"270",		pid	:"257",		kind	:"none",		y:342	},
		"271":{		url :"moins-de-3",			id	:"271",		pid	:"257",		kind	:"none",		y:342	},
		"272":{		url :"moins-de-10",		id	:"272",		pid	:"257",		kind	:"none",		y:342	},
		"273":{		url :"anglais",		id	:"273",		pid	:"257",		kind	:"none",		y:342	},
		"274":{		url :"basque",			id	:"274",		pid	:"257",		kind	:"none",		y:342	},
		"275":{		url :"espagnol",		id	:"275",		pid	:"257",		kind	:"none",		y:342	},
		"276":{		url :"francais",		id	:"276",		pid	:"257",		kind	:"none",		y:342	},
		"277":{		url :"gratuit",		id	:"277",		pid	:"257",		kind	:"none",		y:342	},
		"278":{		url :"comingsoon",		id	:"278",		pid	:"257",		kind	:"none",		y:342	},
		"330":{		url :"offreGaston",		id	:"330",		pid	:"257",		kind	:"none",		y:342	},
		"332":{		url :"smiles",		id	:"332",		pid	:"257",		kind	:"none",		y:342	},
		
		"316":{		url :"happy_books",		id	:"316",		pid	:"257",		kind	:"none",		y:342	},
		
		
		"281":{		url :"points",			id	:"281",		pid	:"257",		kind	:"none",		y:342	},
			"282":{		url :"offrir-10000",	id	:"282",		pid	:"281",		kind	:"none",		y:342	},
			"283":{		url :"offrir-5000",		id	:"283",		pid	:"281",		kind	:"none",		y:342	},
			"284":{		url :"offrir-1000",		id	:"284",		pid	:"281",		kind	:"none",		y:342	},
			"285":{		url :"offrir-2500",		id	:"285",		pid	:"281",		kind	:"none",		y:342	},
		
		"220":{		url :"detail",			id	:"220",		pid	:"257",		kind	:"detail",		y:342	},
		"234":{		url :"detail",			id	:"234",		pid	:"257",		kind	:"detail",		y:342	},
		"230":{		url :"detail",			id	:"230",		pid	:"257",		kind	:"detail",		y:342	},
		
		"305":{		url :"detail",			id	:"305",		pid	:"257",		kind	:"detail",		y:342	},
		"306":{		url :"detail",			id	:"306",		pid	:"257",		kind	:"detail",		y:342	},
		"307":{		url :"detail",			id	:"307",		pid	:"257",		kind	:"detail",		y:342	},
		"308":{		url :"detail",			id	:"308",		pid	:"257",		kind	:"detail",		y:342	},
		"309":{		url :"detail",			id	:"309",		pid	:"257",		kind	:"detail",		y:342	},
		"310":{		url :"detail",			id	:"310",		pid	:"257",		kind	:"detail",		y:342	},
		"311":{		url :"detail",			id	:"311",		pid	:"257",		kind	:"detail",		y:342	},
		"312":{		url :"detail",			id	:"312",		pid	:"257",		kind	:"detail",		y:342	},
		"313":{		url :"detail",			id	:"313",		pid	:"257",		kind	:"detail",		y:342	},
		
		"323":{		url :"detail",			id	:"323",		pid	:"257",		kind	:"detail",		y:342	},
		"324":{		url :"detail",			id	:"324",		pid	:"257",		kind	:"detail",		y:342	},
		
		"260":{		url :"offrir",			id	:"260",		pid	:"257",		kind	:"detail",		y:342,	s:"260", u:"260"},
		"261":{		url :"recommander",	id	:"261",		pid	:"257",		kind	:"detail",		y:342,	s:"261", u:"261"},
		
		"251":{		url :"aide",			id	:"251",		pid	:"257",		kind	:"none",		y:342	},
			"224":{		url :"mobile",			id	:"224",		pid	:"251",		kind	:"none",		y:342	},
			
		"123":{		url :"concours",			id	:"123",		pid	:"257",		kind	:"none",		y:342	},
			"290":{		url :"sdl20101",		id	:"290",		pid	:"123",		kind	:"none",		y:342	},
			"291":{		url :"sdl20102",		id	:"291",		pid	:"123",		kind	:"none",		y:342	},
			"294":{		url :"sdl20103",		id	:"294",		pid	:"123",		kind	:"none",		y:342	},
	
	"236":{		url :"connexion",	id	:"236",		pid	:"none",	kind	:"none",		y:342,	force:true},		
		
	"237":{		url :"bibliotheque",		id	:"237",		pid	:"none",	kind	:"none",		y:342	},
		"262":{		url :"profile",		id	:"262",		pid	:"237",		kind	:"none",		y:342	},
		"286":{		url :"cadeaux",		id	:"286",		pid	:"237",		kind	:"none",		y:342	},
		
	"315":{		url :"points_et_cadeaux",		id	:"315",		pid	:"257",		kind	:"none",		y:342	},
		"347":{		url :"points",		id	:"347",		pid	:"315",		kind	:"none",		y:342	},
		"338":{		url :"cadeaux",		id	:"338",		pid	:"315",		kind	:"none",		y:342	},
		"333":{		url :"smiles",		id	:"333",		pid	:"315",		kind	:"none",		y:342	},
	
	"349":{		url :"cadeaux",		id	:"349",		pid	:"257",		kind	:"none",		y:342	}
		
};

var udetail = 234;
var sdetail = 220;

var idToUrl;
var UrlToId;

var currentPage;

var swfLoaded = false;




function swfComplete_Handler_2()
{
	mw_flashMovieId = AVEBrowser.id;
	swfLoaded = true;
	
	if(History.isRequested())
	{
		History.forceLoad=true;
		History.forceNavs=SWFAddress.getPathNames();
		History.forceReload();
	}
	
	updateAfc();
	
}


function hideWait()
{
	var wait = __$__('wait');
	newsDontStop = false;
	if(wait)
	{
		wait.style.visibility = "hidden";
		wait.style.display = "none";
	}
	
	var glasse = __$__('glasse');
	
	if(glasse && glass_can_be_hidden_by_flash)
	{
		glasse.style.visibility = "hidden";
		glasse.style.display = "none";
	}
	else
	{
		glass_can_be_hidden_by_js = true;
	}
}

function initHistory()
{
	idToUrl = new Object();
	UrlToId = new Object();
	
	for( var key in _NPages )
	{
		idToUrl[_NPages[key].id] = _NPages[key];
		UrlToId[_NPages[key].url] = _NPages[key];
	}
	History.init();
	
		History.activateListener();
	
}

/////////////
// HISTORY//
///////////
// @require http://www.asual.com/swfaddress/
var History = {
	//listenH:false,
	_history:null,
	__actualPage__:"/",
	__swfloadedforhistory__:false,
	__ready__:false,
	internalCall:false,
	forceLoad:false,
	forceNavs:null,
	initiated:false,
	
	__navArrayToAdress__:function( navs )
	{
		var naddr="/";
		for( var i=0;i<navs.length;i++ ) {
			naddr+= unescape(navs[i])+'/';
		}
		//naddr=( nadress==null || nadress == "" ) ? "/":nadress;
		return naddr;
	},
	
	// store actual path to prevent unecessary calls:
	_setActualPageFromArray:function( navs )
	{
		History.__actualPage__ = History.__navArrayToAdress__( navs );
	},
	
	_isActualPageSet:function( navs )
	{

		// get back the url and compare with the one set
		var nadress=History.__navArrayToAdress__( navs );
				
		return ( nadress == History.__actualPage__ );
	},
	
	isActivate:function()
	{
	//	if( History.__listen__ == false ) return false;
		//if ( History.listenH == false ) return false;
		return ( typeof SWFAddress != 'undefined' && true == History.__ready__);
	},
	
	
	init:function()
	{
		//if( typeof SWFAddress != 'undefined' ) return;
		History._history={};
		History.initiated = true;
		SWFAddress.addEventListener(SWFAddressEvent.CHANGE, History.addressChangeHandler);	
	},
	
	activateListener:function()
	{
		History.__swfloadedforhistory__ = true;
		History.__ready__ = true;
		
	},
	
	
	addDetails:function(navs)
	{
		if(!History.isActivate()) return;
		History.internalCall = true;
		//console.log("addDetails force load "+History.forceLoad+" navs : "+History.forceNavs);
		if(History.forceLoad == true)
		{
			
			if(false == History._isActualPageSet( navs ))
			{
				navigate(History.forceNavs);
				
			}
			else
			{
				History.forceLoad = false;
				
			}
		}
		else
		{
			if ( !History._isActualPageSet( navs ) )
			{
				
				History._setActualPageFromArray( navs );
				
				SWFAddress.setValue(
					History.__navArrayToAdress__(navs)
				);
			}
		}
	},
	
	addDetailsForce:function(navs)
	{
		if(!History.isActivate()) return;
		History.internalCall = true;
		
		History.forceLoad = true;
		navigate(navs,false);
		History._setActualPageFromArray( navs );
		SWFAddress.setValue(
			History.__navArrayToAdress__(navs)
		);
	},
	
	addressChangeHandler:function()
	{
		if( !History.isActivate() ) return;
		var innerReload = false;
		
		// decompose url bases:
		var navs=SWFAddress.getPathNames();
		
		
		if ( true == innerReload || !History._isActualPageSet( navs ) )
		{
			History._navigate( navs );
		}
		else
		{
			if(currentPage && currentPage.y)
			{
				goTop(currentPage.y);
			}
			else
			{
				goTop(0);
			}
		}
	},
	
	_navigate:function(navs)
	{
		History._setActualPageFromArray( navs );
		navigate(navs);
	},
	
	reload:function()
	{
		if( !History.isActivate() ) return;
		
		History.addressChangeHandler();
	},
	
	forceReload:function()
	{
		if( !History.isActivate() ) return;
		
		var navs=SWFAddress.getPathNames();
		History._navigate( navs );
	},
	
	// check the parmaters history presence:
	isRequested:function()
	{
		// maybe a little bit simple
		return ( window.location.href.indexOf( "#/" ) >= 0 );
	}
};

function goTop(y)
{
	//window.scrollTo(0,y);
}

function navigate(navs,toTop)
{
	if(toTop != false)
		toTop = true;
	if(navs.length > 0)
	{
		var page;
		var product_id;
		if(navs.length > 1 && typeof UrlToId[navs[navs.length-2]] != 'undefined')
		{
			//test detail
			page = UrlToId[navs[navs.length-2]];
			if("detail" == page.kind || "search" == page.kind)
			{
				product_id = navs[navs.length-1];
			}
			else
			{
				page = UrlToId[navs[navs.length-1]];
			}
		}
		else if(UrlToId[navs[0]] != 'undefined')
		{
			page = UrlToId[navs[0]];
		}
		
		if(typeof page != 'undefined' && true == swfLoaded)
		{
			if("detail" == page.kind)
			{
				if(page.u && page.s)
				{
					swfobject.getObjectById(AVEBrowser.id).showDetailOfProduct(product_id, page.s, page.s, page.s, page.u, page.u, page.u);
				}
				else
				{
					swfobject.getObjectById(AVEBrowser.id).showDetailOfProduct(product_id, sdetail, sdetail, sdetail, udetail, udetail, udetail);
				}
			}
			else if("search" == page.kind)
			{
				
				swfobject.getObjectById(AVEBrowser.id).goSearch( page.id, unescape(product_id) );
			}
			else
			{
				
				swfobject.getObjectById(AVEBrowser.id).goPage(page.id);
			}
		}
		
		if(typeof page != 'undefined' && false == swfLoaded && false == firstCallForceChangePage)
		{
			if("detail" == page.kind)
			{
				if(page.u && page.s)
				{
					var data = {};
					data['page'] = page.s;
					data['p'] = product_id;
					AVEBrowser.goToPage('http://www.avecomics.com/index.php',data);
				}
				else
				{
					var data = {};
					data['page'] = sdetail;
					data['p'] = product_id;
					AVEBrowser.goToPage('http://www.avecomics.com/index.php',data);
				}
			}
			else if("search" == page.kind)
			{
				var data = {};
				data['page'] = page.id;
				data['s'] = unescape(product_id);
				AVEBrowser.goToPage('http://www.avecomics.com/index.php',data);
			}
			else
			{
				var data = {};
				data['page'] = page.id;
				AVEBrowser.goToPage('http://www.avecomics.com/index.php',data);
			}
		}
		
	}
	if(toTop) goTop(0);
}

//SWF Handler functions:
function changePage(pageID,idProduct,searchValue)
{	
//console.log("AVEBroser go page "+pageID);
	if(typeof idToUrl[pageID] != 'undefined')
	{
		var page = idToUrl[pageID];
		var nav = new Array();
		var force = false;
		if(page.force == true)
			force = true;
		
		currentPage = page;
		
		if("detail" == page.kind)
		{
			nav.unshift(idProduct);
		}
		else if("search" == page.kind)
		{
			nav.unshift(escape(searchValue));
		}
		
		nav.unshift(page.url);
		while(page.pid != 'none')
		{
			page = idToUrl[page.pid];
			nav.unshift(page.url);
		}
		
		if(force && swfLoaded == false)
		{
			History.addDetailsForce(nav);
		}
		else
		{
			History.addDetails(nav);
		}
		//History.addDetails(["ifYouLikeIt", idProduct]);
		//History.addDetails(["search", "result", "all", escape(searchValue)]);
	}
};

var firstCallForceChangePage = false;
//SWF Handler functions:
function forceChangePage(pageID,idProduct,searchValue)
{	
	if(typeof idToUrl[pageID] != 'undefined')
	{
		var page = idToUrl[pageID];
		var nav = new Array();
		
		currentPage = page;
		
		if("detail" == page.kind)
		{
			nav.unshift(idProduct);
		}
		else if("search" == page.kind)
		{
			nav.unshift(escape(searchValue));
		}
		
		nav.unshift(page.url);
		while(page.pid != 'none')
		{
			page = idToUrl[page.pid];
			nav.unshift(page.url);
		}
		
		if(History.initiated)
		{
			if(firstCallForceChangePage || History.isRequested() == false)
			{
				History.addDetailsForce(nav);
			}
			else
			{
				History.forceReload();
			}
		}
		firstCallForceChangePage = true;
	}
};


function userConnected(flag) {
	if(flag)
	{
		if(currentPage && "detail" == currentPage.kind)
		{
			History.forceReload();
		}
	}
}


//EXTERNALE PLAYER

/* AB --> JS */
var _statPlayer = '0';
var _pageEP = 0;

function openPlayer()
{
	var regParam="";
	if( navigator.userAgent.indexOf( "MSIE" ) >= 0 ) { // ie
		// prevent IE to store SWF in cache --> all external interface handlers are lost if set
		regParam="?reg="+new Date().getTime()+"&pck=1.0.3";
	} else {
		regParam="?pck=1.0.3";
	}
	
    
	
	var ind='<table id="player-table"><tr><td id="player-td"><div id="ct"><object type="application/x-shockwave-flash" data="http://www.avecomics.com/2010/AVEBrowser/externalPlayer.swf'+regParam+'" ';
	ind+='id="';
	ind+= mw_flashPlayerId;
	ind+='" width="100%" height="100%">';
	ind+='<param name="movie" value="http://www.avecomics.com/2010/AVEBrowser/externalPlayer.swf" />';
	ind+='<param name="quality" value="high" />';
	ind+='<param name="bgcolor" value="#000000" />';
	ind+='<param name="allowFullScreen" value="true" />';
	ind+='<param name="allowScriptAccess" value="always" />';
	ind+='<param name="flashVars" value="language=fr&button=http://wlp.ave-comics.com/relaycom/AcheterAVE.png&button_width=320&button_height=261" />';
	ind+='</object></div></td></tr></table>';
	
	_statPlayer = '1';
	_pageEP = 0;
	closePlayer();
	var popup=document.createElement("div");
	popup.id = "player-box";
	popup.innerHTML=ind;
	document.body.appendChild(popup);
	/*Shadowbox.open({
		player:     'html',
        title:      '',
        content:    ind,
        height:     782,
        width:      1212
    },{onClose: sbClose_Handler});*/
};

function openScreenPlayer()
{
	closePlayer();
	var popup=document.createElement("div");
	popup.id = "player-box";
	
	var regParam="";
	if( navigator.userAgent.indexOf( "MSIE" ) >= 0 ) { // ie
		// prevent IE to store SWF in cache --> all external interface handlers are lost if set
		regParam="?reg="+new Date().getTime();
	}
	var ind='<object type="application/x-shockwave-flash" data="http://www.avecomics.com/2010/AVEBrowser/externalPlayer.swf'+regParam+'" ';
	ind+='id="';
	ind+= mw_flashPlayerId;
	ind+='" width="100%" height="100%">';
	ind+='<param name="movie" value="http://www.avecomics.com/2010/AVEBrowser/externalPlayer.swf" />';
	ind+='<param name="quality" value="high" />';
	ind+='<param name="bgcolor" value="#000000" />';
	ind+='<param name="allowFullScreen" value="true" />';
	ind+='<param name="allowScriptAccess" value="always" />';
	ind+='<param name="flashVars" value="language=fr&button=http://wlp.ave-comics.com/relaycom/AcheterScreen.png&button_width=379&button_height=38" />';
	ind+='</object>';
	popup.innerHTML=ind;
	_pageEP = 0;
	
	//document.body.style.overflow = 'hidden';
	document.body.appendChild(popup);
	
	_statPlayer = '1';
};

/* AB --> EP */
function checkIfPlayerIsConnected()
{
	if(document[mw_flashPlayerId]) document[mw_flashPlayerId].checkIfPlayerIsConnected();
};

/* AB <-- EP */
function playerConnected()
{
	swfobject.getObjectById(AVEBrowser.id).playerConnected();
};

/* AB --> EP */
function readAVE(in1,in2,in3,in4,in5,in6)
{
	_pageEP = in5;
	if(document[mw_flashPlayerId]) document[mw_flashPlayerId].readAVE(in1,in2,in3,in4,in5,in6);
};

/* AB --> EP */
function readBook(in1,in2,in3,in4,in5,in6,in7)
{
	_pageEP = in5;
	if(document[mw_flashPlayerId]) document[mw_flashPlayerId].readBook(in1,in2,in3,in4,in5,in6,in7);
};

/* AB --> EP */
function closePlayerSWF()
{
	if(document[mw_flashPlayerId]) document[mw_flashPlayerId].closePlayerSWF();
};

/* AB <-- EP */
function playerReadyToClose(inPage)
{
	_pageEP = inPage;
	swfobject.getObjectById(AVEBrowser.id).playerReadyToClose(_pageEP);
};

/* AB --> JS */
function closePlayer()
{
	if(_statPlayer == '1')
	{
		if(document.getElementById("player-box"))
		{
			var player = document.getElementById("player-box");
			document.body.removeChild(player);
			//document.body.style.overflow = 'auto';
		}
	}
	else if(_statPlayer == '2')
	{
		Shadowbox.close();
	}
};

/* AB <-- JS */
function sbClose_Handler()
{
	swfobject.getObjectById(AVEBrowser.id).closePlayer(_pageEP);
};


/* EP --> JS */
function updatePageEP(inPage)
{
	_pageEP = inPage;
};

function updateSettingEP(inSettings)
{
	swfobject.getObjectById(AVEBrowser.id).updateExtPlayerSettings(inSettings);
};

function buyProduct(inID)
{
	 swfobject.getObjectById(AVEBrowser.id).addToCart(inID);
};

function _$_(id) {
	return document.getElementById(id);
};



//Local Variable with Flash Content id. default = flashcontent

//Initialize Facebook
function fbInit(pApi_key,pReceiver)
{
	FB.init(pApi_key,pReceiver, {permsToRequestOnConnect : "email"});
}

//JavaScript Connect methods
function login(){
	FB.Connect.requireSession( onLoginHandler );
}

//Event Handlers
function onLoginHandler(){
	flashCallBack( "onLogIn" );
}

//Method to dispatch an Event to Flash
function flashCallBack ( func ) {
	var fl = swfobject.getObjectById(AVEBrowser.id);
	
	if( arguments.length > 1 ){
		fl[func]( Array.prototype.slice.call(arguments).slice(1)[0]);
	}else{
		fl[func]();
	}
}


//initHistory();
//fbInit("e37b65d1bcdddd1114e6374c7f7d3f62", "xd_receiver.htm");

function updateAfc() {
	var list = GetCookie('AVafclist');
	var afcc = list.split(',');
	for(i in afcc)
	{
		var afc = GetCookie(afcc[i]);
		if(afc)
		{
			var afcp = afc.split('=');
			if(afcp[0] && afcp[1])
				swfobject.getObjectById(AVEBrowser.id).addAfc(afcp[0],afcp[1]);
		}
	}
}
