jQuery.fn.exists = function(){return jQuery(this).length>0;}

$(document).ready(function() {
	$(".no_visible").animate({ opacity: 0.0}, 0);
	if( $(".wysiwyg").exists() ) {
		$(".wysiwyg").cleditor({width:"796px"})[0].focus(); 
	}
	MakingTextEffect();
	WindowCentered();
});

$(window).resize(function() {
	var margin = (( $(document).width()/2) -476);
	if( isIE() ) {
		margin = (margin-11);
        }
	$('#ReverseBar').css({ position: "absolute", top: "331px", left: ""+margin+"px" });

	WindowCentered();
});
$(window).scroll(function () { 
	WindowCentered();
});

var remove_timer;
var bool_timer = 0;
var reversebar_time = 2000;

function OpenLoginBox() {
	var str = '';
	str = str + '<form action="/forum/ucp.php?mode=login" method="post" id="login"><table border="0">';
	str = str + '<tr><td>Nom d\'utilisateur:</td><td><input type="text"  name="username" id="username" size="25" value="" /></td></tr>';
	str = str + '<tr><td>Mot de passe:</td><td><input type="password" id="password" name="password" size="25" /></td></tr>';
	str = str + '<tr><td><input type="hidden" name="autologin" value="1" />';
//	str = str + '<input type="hidden" name="viewonline" value="0" />';
	str = str + '<input type="hidden" name="redirect" value="/index.php" /></td>';
	str = str + '<td><input type="submit" name="login" tabindex="6" value="Connexion" class="button1" /></td></tr>';
	str = str + '</table></form>';

	MakingAlert('Se connecter', str);
}
function WindowCentered() {
	$(".WindowCentered").each( function(i) {
		var scroll_top = $(window).scrollTop();
		var top = (($(window).height()/2.0)+scroll_top);
		
		var scroll_left = $(window).scrollLeft();
                var left = (($(window).width()/2.0)+scroll_left);

                $(this).animate({ top: ""+top+"px", left: ""+left+"px"}, {queue: false});
        });
}
function MakingAlert( titre, text ) {
	var str = '';

	str = str + '<div style="position:absolute;top:-250px;left:50%;width:0px;height:0px;z-index:1002;" class="WindowCentered" id="errorbox">';
	str = str + '<div class="errorbox ThemeErrorBox">';

	str = str + '<div class="errorbox_title ThemeColor">'+titre+'</div>';
	str = str + '<div class="errorbox_message ThemeLettre">'+text+'</div>';
	str = str + '<div class="errorbox_close" onclick="DeleteAlert(); return false;">Fermer</div>';

	str = str + '</div></div>';

	str = str + '<div style="position:fixed;top:0px;left:0px;width:100%;height:100%;background-color:#000000;" id="errorbox_bg"></div>';
	
	$("body").append(str);
	$("#errorbox_bg").animate({ opacity: 0.6}, 0);

	WindowCentered();
}
function DeleteAlert() {
        $("#errorbox").animate({ opacity: 0.0}, 500, function() {
		$("#errorbox").remove();
	});
        $("#errorbox_bg").animate({ opacity: 0.0}, 500, function() { 
		$("#errorbox_bg").remove();
	});
}

function SelectAll(id) {
    document.getElementById(id).focus();
    document.getElementById(id).select();
}
function isIE() {
	return /msie/i.test(navigator.userAgent) && !/opera/i.test(navigator.userAgent);
}
function MakingTextEffect() {

	$(".blinking").animate({ opacity: 1.0}, 500).animate({ opacity: 0.0}, 500);

	setTimeout(function() { MakingTextEffect(); }, 500);
}
function Event_ReverseBar() {
	$('#ReverseBar').mouseenter( function() {
	        clearTimeout(remove_timer);
	});
	$('#ReverseBar').mouseleave( function() {
		Stay_ReverseBar();
	});
}
function Stay_ReverseBar() {
	clearTimeout(remove_timer);
	remove_timer = setTimeout( function() { Remove_ReverseBar(); }, reversebar_time);
}
function NormaliseUrl( url ) {

        var arr = url.split('/');
        var page_url = arr[ arr.length-1 ].split('#');
        page_url = page_url[0].split('&steamid=');
        page_url = page_url[0].split('&errTitle=');
	page_url = page_url[0].split('&errMessage=');

	return page_url[0];
}
function ReverseBar() {

	var this_id = 0;
	var txt = '';

	var page_url = NormaliseUrl( document.location.href ) ;
	
	for( var i = 0; i < arguments.length; i++ ) {
		var arg = arguments[i].split("|");
		if( arg[0] == 'id' ) { this_id = parseInt(arg[1]); continue; }

		var dest_url = NormaliseUrl( arg[0] );

		if( page_url == dest_url ) {
			txt = ''+txt+'<li><a href="'+ arg[0] +'" class="here">'+ arg[1] +'</a></li>';
		}
		else {
			txt = ''+txt+'<li><a href="'+ arg[0] +'">'+ arg[1] +'</a></li>';
		}
	}

	var margin = (( $(document).width()/2) -476);
	if( isIE() ) {
		margin = (margin-11);
	}
	if( bool_timer > 0 && bool_timer != this_id ) {
		$('#ReverseBar').html(''+txt+'');
		
		if( isIE() ) {
			$('#ReverseBar').css({ position: "absolute", top: "331px", left: ""+margin+"px", height: "36px"});
		}
		else {
			$('#ReverseBar').css({ position: "absolute", top: "331px", left: ""+margin+"px", height: "36px", opacity: "1.0"});
		}

		bool_timer = this_id;
		

		return false;
	}
	else if( bool_timer > 0 && bool_timer == this_id ) {
		return false;
	}

        Remove_ReverseBar();

	$('body').append('<ul id="ReverseBar">'+txt+'</ul>');
	if( isIE() ) {
		$('#ReverseBar').css({ position: "absolute", top: "331px", left: ""+margin+"px", height: "0px"});
	}
	else {
		$('#ReverseBar').css({ position: "absolute", top: "331px", left: ""+margin+"px", height: "0px", opacity: "0.0"});	
	}
	Event_ReverseBar();
	
	if( isIE() ) {
		$('#ReverseBar').animate({ height: "36px"}, "fast");
	}
	else {
		$('#ReverseBar').animate({ height: "36px", opacity: "1.0"}, "fast");
	}

	Stay_ReverseBar();
	bool_timer = this_id;
}
function Remove_ReverseBar() {
	$('#ReverseBar').animate({ height: "0px", opacity: "0.0"}, "normal", function() {
		$('#ReverseBar').remove();
		bool_timer = 0;
		clearTimeout(remove_timer);
	});
}
function getCookie(c_name) {
	var i,x,y,ARRcookies=document.cookie.split(";");
	for (i=0;i<ARRcookies.length;i++) {
		x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
		y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
		x=x.replace(/^\s+|\s+$/g,"");
		if (x==c_name) {
			return unescape(y);
		}
	}
	return "";
}
function setCookie(c_name,value,exdays) {
	var exdate=new Date();
	exdate.setDate(exdate.getDate() + exdays);
	var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
	document.cookie=c_name + "=" + c_value;
}

	$(function(){

        var iFrames = $('iframe');

    	function iResize() {

    		for (var i = 0, j = iFrames.length; i < j; i++) {
    		  iFrames[i].style.height = iFrames[i].contentWindow.document.body.offsetHeight + 'px';}
    	    }

        	if ($.browser.safari || $.browser.opera) { 

        	   iFrames.load(function(){
        	       setTimeout(iResize, 0);
               });

        	   for (var i = 0, j = iFrames.length; i < j; i++) {
        			var iSource = iFrames[i].src;
        			iFrames[i].src = '';
        			iFrames[i].src = iSource;
               }

        	} else {
        	   iFrames.load(function() {
        	       this.style.height = this.contentWindow.document.body.offsetHeight + 'px';
        	   });
        	}

        });

