function $$(id) { return document.getElementById(id) }
head = document.getElementsByTagName('head')[0];
body = document.getElementsByTagName('body')[0];
function addStyle(css) {
	var style = document.createElement('style'); style.setAttribute('type', 'text/css'); head.appendChild(style); style.innerHTML = css; 	
	return style;
}
function getElementsByClass(searchClass, tag, node){
	if (tag == null) { tag = '*'; } if (node == null) { node = document; }
	var classElements = new Array(), els = node.getElementsByTagName(tag), pattern = new RegExp("(^|\\\\s)"+searchClass+"(\\\\s|$)");
	for (var i = 0; i < els.length; i++) { if (pattern.test(els[i].className)) classElements.push(els[i]) }
	return classElements;
}

function getFirstLevelNodes(refContainer, refTag){
	var container = refContainer;
	if (typeof(refContainer) == "string") container = document.getElementById(refContainer);
	if (container.id == "") setRandomId(refContainer, 5);
	var nodes = container.getElementsByTagName(refTag);
	var fln = new Array();
	for (var n = 0; n < nodes.length; n++){
			if (nodes[n].tagName.toLowerCase() == refTag.toLowerCase() && nodes[n].parentNode.id == container.id){
					fln.push(nodes[n]);
			}
	}
	return fln;
}

function req(url, vars, callback){
	$.ajax({
   type: "POST",
   url: url,
   data: vars,
	 cache:false,
   success: callback
	});
}

function setTitle( title ){
	document.title = title + ' - Fallen Galaxy';
	$('#top_menu_page_title').html(title.toUpperCase());
	if( title.length <= 24){
		$('#top_menu #menu .mask').css({display:'none'});
	}else{
		$('#top_menu #menu .mask').css({display:'block'});
	}
}

$(document).ready(init_JQuery);
function init_JQuery() {
	
	// JQUERY
	
	$('a[rel*="lightbox"]').lightBox();
	
	if( !jQuery.browser.msie ){
		$(".fadeB").css({ opacity: 0.7 });
		$(".fadeB").hover( function(evt) {
			$(evt.currentTarget).stop().animate({ opacity: 1 }, 200);
		}, function(evt) {
			$(evt.currentTarget).stop().animate({ opacity: 0.7 }, 200);
		});
		
		$(".fadeB2").css({ opacity: 0.1 });
		$(".fadeB2").hover( function(evt) {
			$(evt.currentTarget).stop().animate({ opacity: 1 }, 200);
		}, function(evt) {
			$(evt.currentTarget).stop().animate({ opacity: 0.1 }, 200);
		});
	
		/*$("img").css({ opacity: 0 }).delay(2000).stop().animate({opacity: 1}, 500, function(){
			this.setAttribute("style", "");
		});*/
		
		//$("img").css({ opacity: 0 });
		
		
		var nbr_img_total = 0;
		var nbr_img_chargees = 0;
		
		//$("#preloader").fadeOut(250);
		//$("#preloader_progress").delay(4000).fadeIn(500).addClass("visible").delay(4000).fadeOut(500);
		//$("#preloader").delay(8000).fadeOut(500);
		
		//$(".preload_img").each(function(){
			/*nbr_img_total ++;
			if( nbr_img_total < nbr_img_total *0.05 && $(this).complete ){
				nbr_img_chargees ++;
				$("#preloader_progress_bar").css({width: (nbr_img_chargees/nbr_img_total*100) +"%"});
				if( nbr_img_total >= nbr_img_total *0.05 ){
					$("#preloader").stop().fadeOut(500);
					$("#preloader_progress").stop().fadeOut( 300 );
				}
			}*/
			
			/*var src = this.src;
			this.src = "";
			this.src = src;*/
			
			/*$(this).load(function(){
				nbr_img_chargees ++;
				$("#preloader_progress_bar").html(nbr_img_chargees+"/"+nbr_img_total);
				$("#preloader_progress_bar").css({width: (nbr_img_chargees/nbr_img_total*100) +"%"});
				if( nbr_img_chargees >= nbr_img_total *0.75 ){
					$("#preloader").stop().fadeOut(500);
					$("#preloader_progress").stop().fadeOut( 300 );
				}
			});*/
		//});
		
		/*$(".preload_img").load(function(){
			if( nbr_img_total < nbr_img_total *0.75 ){
				nbr_img_chargees ++;
				//$("#preloader_progress_bar").html(nbr_img_chargees+"/"+nbr_img_total);
				$("#preloader_progress_bar").css({width: (nbr_img_chargees/nbr_img_total*100) +"%"});
				if( nbr_img_chargees >= nbr_img_total *0.75 ){
					$("#preloader").stop().fadeOut(500);
					$("#preloader_progress").stop().fadeOut( 300 );
				}
			}
		});*/
		
		
		
		/*$("a").click(function(event){
			if( this.href != undefined && (this.href.indexOf("#") < 0 || this.href.indexOf("#") > 3 ) && $(this).attr("rel") != "lightbox" ){
				event.preventDefault();
				var linkLocation = this.href;
				$("#preloader_progress").stop().css({opacity: 0});
				$("#preloader").stop().fadeIn(250, function(){
					window.location = linkLocation;
				});
			}
		});*/
		
		$(".animated").css({opacity:0, bottom:-40});
		
		var num_img = 0;
		
		$(".animated").each(function(){
			$(this).delay(230*num_img).animate({opacity:1, bottom:0}, 200);
			num_img ++;
		});
		
		
	}else{
		$("#preloader").css({display:'none'});
	}
	
	/*$(".popup_submenu").css({display:'none', opacity:0});
	$(".popup_menu").hover( function(evt) {
		$(".popup_submenu").stop(true).animate({ opacity:0 }, 150, function(){
			$(this).css({display:'none'});
		});
		$(evt.currentTarget).children(".popup_submenu").stop(true).css({ display:'block'}).animate({ opacity:1 }, 150, function(){
		
		});
	}, function(evt) {
		$(evt.currentTarget).children(".popup_submenu").stop(true).delay(200).animate({ opacity:0 }, 250, function(){
			$(evt.currentTarget).children(".popup_submenu").css({display:'none'});
		});
	});*/
	
	/* $(".list_element").hover( function(evt) {
		$(evt.currentTarget).stop().animate({ backgroundColor:"#393939" }, 250);
	}, function(evt) {
		$(evt.currentTarget).stop().animate({ backgroundColor:"#333333" }, 500);
	}); */
	

	$( ".selectable" ).selectable();
	
	$( ".checkbox" ).mousedown( function(evt){
		$(evt.currentTarget).addClass('ui-selecting');
	});
	
	$( ".checkbox" ).mouseup( function(evt){
		$(evt.currentTarget).removeClass('ui-selecting');
		if( $(evt.currentTarget).hasClass('ui-selected') ){
			$(evt.currentTarget).removeClass('ui-selected');
		}else{
			$(evt.currentTarget).addClass('ui-selected');
		}
	});
	
	$(".popup_window").css({display:'none'});
	$(".popup_window .mask").css({opacity:0});
	
	$(".popup_window").parent().children(".popup_window_button").mouseup( function(evt){
		showPopupWindow($(evt.currentTarget).parent().children(".popup_window"));
	});
	
	$(".popup_window .close_button").mouseup( function(evt){
		var parent = $(evt.currentTarget).parent();
		while( !parent.hasClass('popup_window') ){
			parent = parent.parent();
		}
		hidePopupWindow( parent );
	});
	
	
	
	$('.auto_more_content').addClass('hidden');
	$('.auto_more_content').hide('blind', null, 1);
	
	$(".auto_show_more").click( function(evt){
		var cont;
		if($(this).attr('showTargetId') != undefined){
			cont = $("#" + $(this).attr('showTargetId'));
		}else{
			cont = $(evt.currentTarget).parent().children(".auto_more_content");
		}
		
		if( cont.hasClass('hidden') ){
			cont.removeClass('hidden');
			$(evt.currentTarget).addClass('close');
			if($(evt.currentTarget).hasClass('once')){
				$(evt.currentTarget).stop().animate({opacity:0.5}, 100).animate({opacity:1}, 100).animate({opacity:0}, 200, function(){
					$(this).css({display:'none'});
				});
				cont.delay(200).show('blind', null, 500);
			}else{
				cont.show('blind', null, 500);
			}
		}else if(!$(evt.currentTarget).hasClass('once')){
			cont.addClass('hidden');
			$(evt.currentTarget).removeClass('close');
			cont.hide('blind', null, 500);
		}
	});
	
}

function showPopupWindow( elem ){
	elem.css({display:'block'});
	elem.children('.contener').css({display:'block'});
	elem = elem.children('.mask');
	elem.stop(true).animate({ opacity:0.9 }, 500);
	
	$('body').addClass('fixed');
}

function hidePopupWindow( elem ){
	elem = elem.children('.mask');
	elem.parent().children('.contener').css({display:'none'});
	elem.stop(true).animate({ opacity:0 }, 500, function(){
		$(this).parent().css({display:'none'});
	});
	
	$('body').removeClass('fixed');
}

function output( text ){
	$('#alert_popup_content').html(text);
	showPopupWindow($('#alert_popup_window'));
}
