/* load css on JAVASCRIPT ACTIVATED */
document.write("<lin" + "k type=\"text/css\" rel=\"stylesheet\" href=\"/css/common/js.css\" media=\"screen\" />");


function getGallerySWF() {
	var swf_id = 'flashcontent';
	if ( navigator.appName.indexOf( 'Microsoft' ) != -1 ) {
		return document.getElementById ? document.getElementById( swf_id ) : window[ swf_id ];
	} else {
		return document[ swf_id ];
	}
}
/* fonction Envoyer ma Sélection à un ou des amis */
function send_select(id_01,id_02,id_03){	
	if(id_01!="" || id_02!="" || id_03!=""){
		var html = "/envoyer-a-un-ami.php?id_01="+id_01+"&id_02="+id_02+"&id_03="+id_03;	
		if ( !popit(3,html,450,340,'Envoyer_ma_selection') ) {
			getGallerySWF().popUpBlocker();
			return false;
		}
		return true;
	}	
	return false;
}
/* fonction Envoyer une galerie à des amis */
function send_galerie(id_01){	
	if(id_01!=""){
		var html = "/partager-cette-galerie.php?id_01="+id_01;	
		if ( !popit(3,html,680,525,'Envoyer_ma_selection') ) {
			getGallerySWF().popUpBlocker();
			return false;
		}
		return true;
	}	
	return false;
}

/* Auto Resize Gallerie Flash */
function galeryAutoResize(){
	var winWidth = $(window).width();
	var winHeight  = $(window).height();
	
	var myWidth = 0, myHeight = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
	//Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	}
	var winWidth 	= myWidth;
	var winHeight  	= myHeight;
	
	if(winHeight < 584){ 
		toHeight = 584;
	}else if(winHeight >= 584){
		toHeight = winHeight;
	}else {}
	if(winWidth < 1000){ 
		toWidth = 1000;
	}else if(winWidth >= 1000){
		toWidth = winWidth;
	}else {}
	$(".galerie #wrapper_page").width(toWidth);
	$(".galerie #wrapper_page").height(toHeight);
}
/* /Auto Resize Gallerie Flash */


/* AlertBox */
function closeAlertBox(){
	$("#overlay").removeClass("overlay");
	$("#overlay").css({"position":"absolute", "top":"0", "left":"0", "width":"0", "height":"0", "display":"none", "background":"#000", "opacity":"0", "z-index":"0"});
	/*
	$("#wrapper_page").animate({
		   opacity: '1'
	 }, "fast");
	$("#wrapper_page *").animate({
		   opacity: '1'
	 }, "fast");
	 */
	$("#alert_box").fadeOut("fast");
	$("#alert_box").css({"z-index" :"0"});
	$("#alert_box_content").empty();
	return false;
}
function showAlertBox(html){
	var winWidth 	= $("#wrapper_page").width();
	var winHeight  	= $("#wrapper_page").height();
	
	$("#overlay").addClass("overlay");
	$("#overlay").css({"position":"absolute", "top":"0", "left":"0", "width":winWidth, "height":winHeight, "display":"block", "background":"#000", "opacity":"0.5", "z-index":"9999"});
	
	$("#alert_box_content").empty();
	$("#alert_box").css({"z-index" :"99999"});
	$("#alert_box").fadeIn("fast");
	
	$.ajax({
			url: html,
			type: 'GET',
			dataType: 'html',
			timeout: 1000,
			error: function(){
				alert('Error loading document');
			},
			success: function(html_content){
				$("#alert_box #alert_box_content").html(html_content);
			}
	});	
	return false;
}
if (typeof $ !== "undefined") {
$(document).ready(function(){
	// le choix de thomas Lelu
	//$("#choice_box a").addClass("waiting");
	//$("a.le_choix_thomas").addClass("waiting");
	//$("a.choix_de_thomas_lelu").addClass("waiting");
	// le choix des internautes
	//$("#internautes_choice a").addClass("waiting");
	//$("a.choix_des_internautes").addClass("waiting");
	
	//Annonce de la fermeture du concours
	/*$("#win_box a.subscribe").addClass("close");*/
	$("#how_to_win a").addClass("close");
	/*$("#login_box a").addClass("close");
	$("#login_box a").css("display" ,"none");
	$("#login_box a.forgetPWD").removeClass("close");
	$("#login_box a.forgetPWD").css("display" ,"block");*/
	$("#footer a.subscribe").addClass("close");
	$("#navigation a.comment_participer").addClass("close");
	
	/* Auto Resize Gallerie Flash */
	galeryAutoResize();
	
	$(window).resize( function() { 
		
		/* Auto Resize Gallerie Flash on window resize */
		galeryAutoResize();
	
	} );

	/* alert_box */
	$("a.waiting").bind("click", function(e){
		showAlertBox("alert_box.html"); 
		return false;
	});
	/* alert_contest_finish */
	$("a.close").bind("click", function(e){
		showAlertBox("alert_contest_finish.html"); 
		return false;
	});
	
	
	/*
	$("a.blog_thomas").attr({"href":"#"});
	$("a.blog_thomas").attr({"onclick":"return false;"});
	$("a.blog_thomas").addClass("waiting_blog");
	$("a.waiting_blog").bind("click", function(e){
		showAlertBox("alert_box_blog.html"); 
		return false;
	});
	*/
});
}
/* /AlertBox */

function parseXml(xml){
	var random 		= jXML.getRandomObj(xml,{"Keywords":["keywords > keyword",null]});
	var output 		= '';
	var tiret 		= '&nbsp;-&nbsp;';
	var maxKeywords = 3;
	$("#output_keyword").empty();
	var base_url = $("#form_search").attr( 'action' );
	for (i = 0; i < maxKeywords; i++) {
		output = $(random["Keywords"][i]).text();
		if(i==maxKeywords-1) tiret = '';
		$("#output_keyword").append('<li><a href="' + base_url + '?search=' + escape( output ) + '" title="'+output+'" class="search_samples">'+output+'</a>'+tiret);
	}
}
var jXML = {
    getCount: function(xml,nodes) {
        var response = {};
        for (var node in nodes) {
            response[node] = $(nodes[node],xml).length;
        }
        return response;
    },
    getAttribute: function(xml,nodes) {
        var response = {};
        for (var node in nodes) {
            if (nodes[node][2] == null) {
                response[node] = $(nodes[node][0] + "[" + nodes[node][1] + "]",xml);
            } else {
                if ($(nodes[node][0],xml).attr(nodes[node][1]) == nodes[node][2]) {
                    response[node] = $(nodes[node][0],xml);
                }
            }
        }
        return response;
    },
    getRandomObj: function(xml,nodes) {
        var response = {};
        for (var node in nodes) {
            var a = [];
            var b = [];
            $(nodes[node][0],xml).each(function(i) {
                b[i] = this;
            });
            var c = b.length;
            if (nodes[node][1] != null && nodes[node][1] < c) {
                c = nodes[node][1];
            }
            for (i = 0; i < c; i++) {
                var e = Math.floor(Math.random() * b.length);
                a[i] = b[e];
                b.splice(e,1);
            }
            response[node] = a;
        }
        return response;
    }
};
if (typeof jQuery !== "undefined") {
jQuery(document).ready(function(){
	/* fonction boutons rollover */
	$('.rollover').hover(
	        function(){
	            t = $(this);
	            t.attr('src',t.attr('src').replace('_OFF','_ON'));
	        },
	        function(){ 
	            t= $(this);
	            t.attr('src',t.attr('src').replace('_ON','_OFF'));
	        }
	 );
	
	/* SEARCH FORM KEYWORD PARSING */
	$.ajax({
		type: "GET",
		url: "xml/keywords.xml",
		dataType: "xml",
		success: parseXml
	});
	/* FORM COMPONENT SKIN JQUERY */
	$("form.skinned select").each(function(i){
		var valeur = "";
		$('option[@value='+valeur+']',this).attr("selected", "selected");
		$(this).before('<span class="select" id="select_'+$(this).attr('id')+'">'+$(this).find(':selected').text()+'</span>');
		;
	});
	$("form.skinned select").each(function(i){
		$(this).change(function(){
			$(this).before('<span class="select" id="select_'+$(this).attr('id')+'">'+$(this).find(':selected').text()+'</span>');
		});
	});
	$("form.skinned input[type=checkbox],form.skinned input.checkbox").each(function(i){
		$(this).before('<span class="checkbox"></span>');
	});		
	
	$("form.skinned input[type=checkbox],form.skinned .checkbox").each(function(i){
		$(this).bind("click", function(e){
			if(!$(this).is(':checked')){
				$(this).removeClass("checkbox_checked");
				$(this).prev("span.checkbox").removeClass("checkbox_checked");
			}else{
				$(this).addClass("checkbox_checked");
				$(this).prev("span.checkbox").addClass("checkbox_checked");
			}
			if(!$(this).next("input[type=checkbox],input.checkbox").is(':checked')){
				$(this).addClass("checkbox_checked");
				$(this).next("input[type=checkbox],input.checkbox").attr("checked","checked");
			}else{
				$(this).removeClass("checkbox_checked");
				$(this).next("input[type=checkbox],input.checkbox").removeAttr("checked","checked");
			}					
		});	
		$(this).bind("mouseout", function(e){
			$(this).removeClass("checkbox_over");
			$(this).prev("span.checkbox").removeClass("checkbox_over");
			$(this).next("input[type=checkbox],input.checkbox").removeClass("checkbox_over");
		});	
		$(this).bind("mouseover", function(e){
			$(this).addClass("checkbox_over");
			$(this).prev("span.checkbox").addClass("checkbox_over");
			$(this).next("input[type=checkbox],input.checkbox").addClass("checkbox_over");
		});											
	});	

	/**	LOGIN */
	var default_login_text = 'email';
	var default_passwd_text = 'mot de passe';
	jQuery( '#identifiant' ).bind( 'focus', function( e ) {
		$(this).val( '' );
	} ).bind( 'blur', function( e ) {
		if ( '' == $(this).val() ) {
			$(this).val( default_login_text );
		}
	} );
	jQuery( '#motdepasse' ).bind( 'focus', function( e ) {
		$(this).val( '' );
	} ).bind( 'blur', function( e ) {
		if ( '' == $(this).val() ) {
			$(this).val( default_passwd_text );
		}
	} );

	jQuery('#form_login').submit( function( e ) {
		e.preventDefault();
		var usr_login = jQuery( '#identifiant', this ).val();
		var usr_passwd = jQuery( '#motdepasse', this ).val();

		$.ajax({
			type: "POST",
			url: "/connexion.html",
			data: 'login=' + usr_login + '&password=' + usr_passwd + '&rpc=1',
			dataType: 'json',
			success: function( data, status ) {
				if ( typeof data.error != 'undefined' ) {
					var errMsg = '';
					if ( typeof data.error == 'Array' ) {
						for( var i=0; i<data.error.length; i++ ) {
							errMsg += data.error[ i ] + '\n';
						}
					} else {
						for( var i in data.error ) {
							errMsg += data.error[ i ] + '\n';
						}
					}
					alert( errMsg );
				} else {
					redirect( '/' );
				}
			}
		});

		return false;
	} );
	jQuery("#login_box form a.forgetPWD").bind("click", function(e){
		e.preventDefault();
		jQuery("#form_login").hide();
		jQuery("#form_forgetPWD").show();

		jQuery( '#identifiant_perdu' ).bind( 'focus', function( e ) {
			$(this).val( '' );
		} ).bind( 'blur', function( e ) {
			if ( '' == $(this).val() ) {
				$(this).val( default_login_text );
			}
		} );
		return false;
	});
	
	jQuery('#form_forgetPWD').submit( function( e ) {
		jQuery("#form_forgetPWD").hide();
		jQuery("#form_login").show();
		e.preventDefault();
		var usr_login = jQuery( '#identifiant_perdu', this ).val();

		$.ajax({
			type: "POST",
			url: "/oubli-mot-de-passe.html",
			data: 'login=' + usr_login + '&rpc=1',
			dataType: 'json',
			success: function( data, status ) {
				if ( typeof data.error != 'undefined' ) {
					var errMsg = '';
					if ( typeof data.error == 'Array' ) {
						for( var i=0; i<data.error.length; i++ ) {
							errMsg += data.error[ i ] + '\n';
						}
					} else {
						for( var i in data.error ) {
							errMsg += data.error[ i ] + '\n';
						}
					}
					alert( errMsg );
				} else {
					alert( 'Mot de passe envoyé' );
					redirect( '/' );
				}
			}
		});

		return false;
	} );
	//	****
	
	/**	BLOC RECHERCHE */
	var search_label = $('#search').val();
	$('#search').bind('focus', function( e ) {
		$(this).val( '' );
	} );
	$('#search').bind('blur', function( e ) {
		if ( '' == $(this).val() ) {
			$(this).val( search_label );
		}
	} );
	$("#form_search").submit( function( e ) {
		e.preventDefault();
		redirect( $("#form_search").attr( 'action' ) + '?search=' + escape( $("#form_search input#search").val() ) );
	} );
	
	/** IMPORT CONTACT */
	$('a.import_contact').each( function( i ) {
		$(this).bind( 'click', function( e ) {
			e.preventDefault();

			popit( 1, $(this).attr( 'href' ), 620, 550, 'import_contact' );
		} );
	} );
	
	$('#code, #link').focus(function() { $(this).select(); });
	$('#code, #link').click(function() { $(this).select(); });

});
}
/*/FORM COMPONENT SKIN JQUERY */

/* POPUP */
function popit(type,page,largeur,hauteur,popupname){
	if(!popupname || popupname ==''){popupname="pop_up"}
	var top=(screen.height-hauteur)/2;
	var left=(screen.width-largeur)/2;
	switch(type){
		case 1 :var fenetre = window.open(page,popupname,"top="+top+",left="+left+",width="+largeur+", height="+hauteur+",resizable=yes,scrollbars=yes");break;
		case 2 :var fenetre = window.open(page,popupname+Math.round((Math.random()*10000000000000)),"top="+top+",left="+left+",width="+largeur+", height="+hauteur+",scrollbars=yes,status=yes,toolbar=yes,resizable=yes,menubar=yes,location=yes");break;
		case 3 :var fenetre = window.open(page,popupname+Math.round((Math.random()*10000000000000)),"top="+top+",left="+left+",width="+largeur+", height="+hauteur+",scrollbars=no,status=no,toolbar=no,resizable=no,menubar=no,location=no");break;
		case 4 :var fenetre = window.open(page,popupname+Math.round((Math.random()*10000000000000)),"top="+top+",left="+left+",width="+largeur+", height="+hauteur+",scrollbars=yes,status=no,toolbar=no,resizable=yes,menubar=no,location=no");break;
		case 0 :
		default :var fenetre = window.open(page,popupname+Math.round((Math.random()*10000000000000)),"top="+top+",left="+left+",width="+largeur+", height="+hauteur+",resizable=yes,scrollbars=yes");
	}
	if ( fenetre ) {
		fenetre.focus();
		return true;
	} else {
		//	bloqu?e !?
		return false;
	}
}
/* /POPUP */

function timerRedirect(temps,myUrl){counter=setTimeout("redirect('"+myUrl+"')", temps*1000);}
function redirect(myUrl){document.location.href = myUrl;}


/**
 * Tracking ga from flash
 */
function		ga(uri) {
	try {
		if (typeof window.pageTracker != "undefined") {
			window.pageTracker._trackPageview(uri);
		}
	} catch (e) {}
}
