var currentpub = 0;
var publiste = [
					{"src":"i/pub/vielibertine2.gif" , "alt":"Un autre site libertin a voir" , "link":"http://www.vie-libertine.com"}, 
					{"src":"i/pub/banpub_10_01.gif" , "alt":"Plaisirs de femmes" , "link":"http://www.sexy-boutiques.com/fr/categories/10/femmes.html"}, 
					{"src":"i/pub/banpub_60_02.gif" , "alt":"DVD Adultes" , "link":"http://www.sexy-boutiques.com/fr/categories/60/dvd.html"}, 
					{"src":"i/pub/banpub_45_01.gif" , "alt":"Gadgets sexy, Adult Toys" , "link":"http://www.sexy-boutiques.com/fr/categories/45/bazar_sexy.html"}, 
					{"src":"i/pub/banpub_4580_02.gif" , "alt":"Librairie, DB Adultes" , "link":"http://www.sexy-boutiques.com/fr/categories/4580/librairie_coquine.html"}, 
					{"src":"i/pub/banpub_1030_01.gif" , "alt":"Lingerie Sexy" , "link":"http://www.sexy-boutiques.com/fr/categories/1030/lingerie_sexy.html"}, 
					{"src":"i/pub/banpub_4530_02.gif" , "alt":"Sex Toys" , "link":"http://www.sexy-boutiques.com/fr/categories/4530/gadgets_humour.html"}, 
					{"src":"i/pub/banpub_40_02.gif" , "alt":"Bijoux adultes" , "link":"http://www.sexy-boutiques.com/fr/categories/40/bijoux.html"}, 
					{"src":"i/pub/ban22_468x.gif" , "alt":"Créer votre propre boutique sexy" , "link":"http://www.busyx.com/?pr_id=12745"}
				];

function GetSize()
{
	var scrollH = 0;var scrollW = 0;var windowH = 0;var windowW = 0;
	if (document.doctype == null || document.documentElement.clientHeight == 0)
	{
		if (window.innerWidth || (document.body.offsetHeight == document.documentElement.offsetHeight && document.body.offsetWidth == document.documentElement.offsetWidth)) { scrollH = document.body.scrollTop;scrollW = document.body.scrollLeft;windowH = document.body.clientHeight;windowW = document.body.clientWidth; }
		else { scrollH = document.documentElement.scrollTop;scrollW = document.documentElement.scrollLeft;windowH = document.documentElement.clientHeight;windowW = document.documentElement.clientWidth; }
	}
	else
	{
		if (document.doctype.publicId.search(/xhtml/i) != -1) { scrollH = document.documentElement.scrollTop;scrollW = document.documentElement.scrollLeft; }
		else { scrollH = document.body.scrollTop;scrollW = document.body.scrollLeft; }
	}
	if (window.innerWidth && window.innerHeight) { windowH = window.innerHeight; windowW = window.innerWidth; }
	return {"width":windowW,"height":windowH,"scrollW":scrollW, "scrollH":scrollW};
}

function Navigateur() 
{
 var ua, s, i;
  this.isIE    = false;  // Internet Explorer
  this.isOP    = false;  // Opera
  this.isNS    = false;  // Netscape
  this.version = null;

  ua = navigator.userAgent;
  s = "Opera";
  if ((i = ua.indexOf(s)) >= 0) { this.isOP = true; this.version = parseFloat(ua.substr(i + s.length)); return; }
  s = "Netscape6/"; 
  if ((i = ua.indexOf(s)) >= 0) {this.isNS = true;this.version = parseFloat(ua.substr(i + s.length));return;}
  // Treat any other "Gecko" browser as Netscape 6.1.
  s = "Gecko";
  if ((i = ua.indexOf(s)) >= 0) {this.isNS = true;this.version = 6.1;return;}
  s = "MSIE";
  if ((i = ua.indexOf(s))) {this.isIE = true;this.version = parseFloat(ua.substr(i + s.length));return;}
}


function App ()
{
	// Le type de browser utilisé par l'application
	this.browser = new Navigateur();
}

App.prototype.Build = function( ) 
{
	// Définition des éléments de l'application
	this.workspace = $("workspace");
	this.LeftPan = $("LeftPan");
		this.LeftPanSep = $("LeftPanSep");
		this.OnlineTitle =$("online_title");
		this.OnlineData =$("online_data");
	this.Header =  $("header");
		this.HeaderInfo =  $("info");
		this.HeaderPub  =  $("pub");
			this.HeaderBan  =  $("pub_banniere");
	this.Frame = $("Frame");
	this.mFrame = $("mFrame");
	this.MdiFrame = $("MdiFrame");
	this.BottomPan = $("BottomPan");

	// Gestion du panneau de gauche
	$("HidePanLeft").onclick = function()
	{
		if (site.LeftPan.style.display != "none") {site.LeftPan.style.display="none";site.Resize();this.src=slideon;}
		else {site.LeftPan.style.display="inline";site.Resize();this.src=slideoff;}
	};

	// Resize de la fenêtre entraine le resize du workspace
	window.onresize = function(){ site.Resize();};

}

App.prototype.Resize = function( ) 
{
	// Déterminer la Taille de la fenêtre	
	var FrameSize = GetSize();
	// Resize du worspace [largeur fenêtre -2*Marge)	
	this.workspace.style.width  = (FrameSize.width - 2 * this.workspace.offsetLeft - 25)+"px";
	this.workspace.style.height = (FrameSize.height- 2 * this.workspace.offsetLeft)+"px";
	// Resize Frame [Largeur fenêtre - 2*Marge - Largeur(LeftPan) -- Largeur(LeftPanSep)]
	this.Frame.style.width  = ( FrameSize.width - 2 * this.workspace.offsetLeft - this.LeftPan.offsetWidth - this.LeftPanSep.offsetWidth - 25) +"px";
	this.Frame.style.height = ( FrameSize.height- 2 * this.workspace.offsetLeft ) +"px";
	// Resize mFrame [Largeur fenêtre - 2*Marge - Largeur(LeftPan) -- Largeur(LeftPanSep)]
	this.mFrame.style.width = ( FrameSize.width - 2 * this.workspace.offsetLeft - this.LeftPan.offsetWidth - this.LeftPanSep.offsetWidth - 21 - 25) +"px";
	// Hauteur de la Frame [ Hauteur fenêtre - 2*Marge - Hauteur(menu) - Hauteur(bottompan)]
	this.MdiFrame.style.height  = ( FrameSize.height- 2 * this.workspace.offsetLeft - this.mFrame.offsetHeight - this.BottomPan.offsetHeight - this.Header.offsetHeight ) +"px";
	// Bottom Frame
	this.BottomPan.style.top  = ( FrameSize.height- 2 * this.workspace.offsetLeft - this.BottomPan.offsetHeight - this.Header.offsetHeight ) +"px";
	this.BottomPan.style.width = this.Frame.style.width;
	
	this.Header.style.width  = this.Frame.style.width;
	this.HeaderPub.style.width = ( parseInt(this.Header.style.width)-  this.HeaderInfo.offsetWidth) +"px";
	this.HeaderBan.style.left  = ( (parseInt(this.HeaderPub.style.width) - 470 ) / 2 )  +"px";
	
	this.OnlineData.style.height  = ( FrameSize.height- 2 * this.workspace.offsetLeft - this.OnlineTitle.offsetHeight ) +"px";
	
}

App.prototype.Pub = function( ) 
{
	//return;
	if ( currentpub > 8) { currentpub=0; }
	$("banniere").src = publiste[currentpub].src; $("banniere").alt = publiste[currentpub].alt; $("pub_link").href = publiste[currentpub].link;
	currentpub+=1; ctrl = setTimeout("site.Pub()",30000);
}

var site = new App();
function run() 
{ 
$("Work").style.display="inline";
site.Build(); 
$("workspace").style.display="inline";
site.Resize(); 
site.Pub(); 
$("Work").style.display="none";
annuaire.build();
}


//******************************************************************************************************
//gestion de la partie annuaire de liens
var annuaire = 
{
	// constructeur
	build : function( )
	{
		// Gestion des A, pour compter le nb de click sur les liens
		annuaire.liste_link();
		// Gestion des bouton de commandes [ Enregistrer Lien ]
		if ( $("ann_submit_lien") ) {$("ann_submit_lien").onclick = annuaire.check_form; }
		// Bouton de recherche de site		
		if ( $("search-btn3") ) { $("search-btn3").onclick = annuaire.search;  }
		
	},
	// Recherche tous les A.ann_lnk
	liste_link : function()
	{
		if ( $("content_annuaire") )
		{
			lst = $("content_annuaire").select("A.ann_lnk");
			lst.each(  function(s) {s.onclick = function() {return annuaire.click_link(s.id);}; });
		}
	},
	// Enregistrer le click sur le lien et retourner True pour poursuivre 
	click_link : function( id )
	{
		new Ajax.Request
		(	cfg.request,
			{
				method: 'get', parameters: {mode : 'annuaire_click', id: id }
			}
		);
		return true;
	},
	// Controle du formulaire avant enregistrement de la demande
	check_form : function( )
	{
		// Tous les champs sont obligatoires
		if ($("titre").value.length == 0)  {annuaire.log_error("Champ Titre" , "Veuillez péciser <B><font color='red'>le titre de votre site</font></B> avant de poursuivre, Merci.");return false; }
		if ($("url").value.length == 0)  {annuaire.log_error("Champ url" , "Veuillez péciser <B><font color='red'>l'adresse de votre site</font></B> avant de poursuivre, Merci.");return false; }
		if ($("description").value.length == 0)  {annuaire.log_error("Champ Description" , "Veuillez péciser <B><font color='red'>la description de votre site</font></B> avant de poursuivre, Merci.");return false; }
		if ($("nom").value.length == 0)  {annuaire.log_error("Champ Nom" , "Veuillez péciser <B><font color='red'>votre nom</font></B> avant de poursuivre, Merci.");return false; }
		var re=new RegExp("^[a-zA-Z0-9\-_]+[a-zA-Z0-9\.\-_]*@[a-zA-Z0-9\-_]+\.[a-zA-Z\.\-_]{1,}[a-zA-Z\-_]+","i");
		if(!re.test($("email").value)) { annuaire.log_error("Champ Email" , "L'adresse Email n'est pas valide!");return false;  }
		if( $("categorie").selectedIndex == 0)  {annuaire.log_error("Champ categorie" , "Veuillez péciser <B><font color='red'>une catégorie </font></B> avant de poursuivre, Merci.");return false;}
		if ($("url_retour").value.length == 0)  {annuaire.log_error("Champ Url de retour" , "Veuillez péciser <B><font color='red'>l'adresse de la page ou se trouve notre lien</font></B> avant de poursuivre, Merci.");return false; }
		// Passer à l'enregistrement dans la base
		annuaire.check_base();
		
	},
	check_base : function()
	{
		// Enregistrement dans la base
		new_val = $('new_lien').serialize(true) ;
		// Enregistrement des données
		new Ajax.Request
		(	cfg.request,
			{
				method: 'get', parameters: { mode: 'new_lien', values : Object.toJSON(new_val) },
				onSuccess: function(transport)  
				{ 
					if ( transport.responseText.strip() == "1" )
					{
						// enregistrement du lien OK 
						annuaire.log_ok("Demande enregistrée" , "Votre demande de lien a bien été enregistrée, nos webmasters visiteront votre site dés que possible, merci.");
					}
				},
				onFailure: function() {}
			}
		);
	},
	log_error : function (as_titre , as_texte)
	{
		$('Hide').style.display = "block";
		
		$("error_titre").update(as_titre);		
		$("error_text").update(as_texte);		
		popup.setposition( "cont_alerte" , "full", 460,275, "erreur_inscription" , "" );
	},
	log_ok : function (as_titre , as_texte)
	{
		$("erreur_inscription").value="Ok";
		$('Hide').style.display = "block";
		$("error_titre").update(as_titre);		
		$("error_text").update(as_texte);		
		popup.setposition( "cont_alerte" , "full", 460,275, "erreur_inscription" , annuaire.go_index );
		
	},
	go_index : function()
	{
		window.location ="index.htm";
	},
	search : function ()
	{
		if ( $("search_site") ) { window.location ="index.htm?search="+ $("search_site").value; }
	}
	
};

//******************************************************************************************************
// Gestion commune des fausses popup
// ( Pb de centrage sur les pages Annuaire-Annonces )
var popup  =
{
	// Positionner une fause popup dans la page
	setposition : function ( id_popup, ref_size,  width, height , id_close, callback_close )
	{
		// Centrer la popup
		window.scrollTo(0,0);
		t = parseInt(( document.viewport.getHeight() - height ) / 2) + "px"; 
		
		var FrameSize = GetSize();
		
		if ( ref_size =='full')  l = parseInt((FrameSize.width - width ) / 2) + "px";
		else 	l = parseInt(( 780 - width ) / 2) + "px";
		
		if ( Prototype.Browser.IE ) 
		{ 
			t = parseInt(( $('Hide').getHeight() - height ) / 2) + "px";
			$(id_popup).style.height = height+"px"; 
		}
		
		$(id_popup).style.width = width+"px"; 
		$(id_popup).style.top = t;  $(id_popup).style.left = l;
		// Afficher la popup
		$(id_popup).style.zIndex = "200"; 
		$(id_popup).style.display = "inline";
		// Bouton de fermeture de la popup
		$(id_close).onclick = function()
		{
			// $(id_popup).update(); 
			$(id_popup).style.display = "none"; 
			$('Hide').style.display = "none";
			if ( callback_close != "") { callback_close(); }
		};
	},
	closepopup : function(as_dest)
	{
		$('Hide').style.display = "none";
		
		// $("cont_alerte").update(); 
		$("cont_alerte").style.display = "none"; 
		window.location = as_dest;
	} 
};

