var ROOT = "http://gamessphere.de/";
var navTimer;
var nav_btn_Timer;
var lastNav;
var currentFeaturedGame = 1;
var featuredGamesTimer;
var featuregame = new Array();
var featured_games_count;
var currentArticlePage = 1;
var games_expanded = 0;
var fastNavTimer;

function check_comment_and_submit(){
	comment_string = new String($("#new_comment").val());
	
	if(comment_string.length < 15 || comment_string.length > 1000){
		alert("Dein Kommentar/Meinung sollte aus mindestens 15 Zeichen und nicht mehr als 1000 Zeichen bestehen. Derzeit verwendest du "+comment_string.length+" Zeichen.");
		return false;
	}
	else{
		document.forms["comment_form"].submit();
	}
}

function cancel_nav_timeout(exept){
	window.clearTimeout(navTimer);
	close_nav_menues(exept);
	lastNav = exept;
	window.setTimeout("close_nav_menues2()", 300);
}

function close_nav_menues(exept){
	if(exept != 0){ $("#m0").hide(); }
	if(exept != 1){ $("#m1").hide(); }
	if(exept != 2){ $("#m2").hide(); }
	if(exept != 3){ $("#m3").hide(); }
	if(exept != 4){ $("#m4").hide(); }
    
    close_nav_buttons(exept);
}
function close_nav_menues2(){
	if(lastNav != 0){ $("#m0").hide(); }
	if(lastNav != 1){ $("#m1").hide(); }
	if(lastNav != 2){ $("#m2").hide(); }
	if(lastNav != 3){ $("#m3").hide(); }
	if(lastNav != 4){ $("#m4").hide(); }
    
 	close_nav_buttons(lastNav);
}

function close_nav_buttons(dont_close_id){
    if(dont_close_id != 0){
        if(active_menue != 0){ $("#nav_btn_0").attr("class", "btn_start") }
    }
    if(dont_close_id != 1){
        if(active_menue != 1){ $("#nav_btn_1").attr("class", "btn_news") }
    }
    if(dont_close_id != 2){
        if(active_menue != 2){ $("#nav_btn_2").attr("class", "btn_games") }
    }
    if(dont_close_id != 3){
        if(active_menue != 3){ $("#nav_btn_3").attr("class", "btn_charts") }
    }
    if(dont_close_id != 4){
        if(active_menue != 4){ $("#nav_btn_4").attr("class", "btn_community") }
    }
    if(dont_close_id == undefined){
        nav_btn_Timer = window.setTimeout("reset_nav_buttons()", 30);
    }
}

function reset_nav_buttons(){
    if(active_menue == 0){ $("#nav_btn_0").attr("class", "btn_start_checked") }
    if(active_menue == 1){ $("#nav_btn_1").attr("class", "btn_news_checked") }
    if(active_menue == 2){ $("#nav_btn_2").attr("class", "btn_games_checked") }
    if(active_menue == 3){ $("#nav_btn_3").attr("class", "btn_charts_checked") }
    if(active_menue == 4){ $("#nav_btn_4").attr("class", "btn_community_checked") }
}

function start_nav_timeout(){
	navTimer = window.setTimeout("close_nav_menues()", 250);
}

function show_menue(menue_layer, id){
	var animationdur = 0;
	$(menue_layer).show(animationdur);
    
    $("#nav_btn_0").attr("class", "btn_start");
    $("#nav_btn_1").attr("class", "btn_news");
    $("#nav_btn_2").attr("class", "btn_games");
    $("#nav_btn_3").attr("class", "btn_charts");
    $("#nav_btn_4").attr("class", "btn_community");
    
    if(id == 0){ $("#nav_btn_0").attr("class", "btn_start_checked") }
    if(id == 1){ $("#nav_btn_1").attr("class", "btn_news_checked") }
    if(id == 2){ $("#nav_btn_2").attr("class", "btn_games_checked") }
    if(id == 3){ $("#nav_btn_3").attr("class", "btn_charts_checked") }
    if(id == 4){ $("#nav_btn_4").attr("class", "btn_community_checked") }
}

function init_featured_games(){
	$("#featuregame_teaser").attr("src", featuregame[currentFeaturedGame]['image']);
	$("#featured_game_titel").html(""+featuregame[currentFeaturedGame]['name']+"");
	$("#featured_game_desc").html(""+featuregame[currentFeaturedGame]['description']+"");
	featuredGamesTimer = window.setTimeout("feature_games_timer()", 5000);
	nextPreLoadIndex = 2;
	var image1 = $('<img />').attr('src', featuregame[nextPreLoadIndex]['image']);
	// Insert preloaded image into the DOM tree
	$('.profile').append(image1);
}

function feature_games_timer(){
	next_featured_game();
	featuredGamesTimer = window.setTimeout("feature_games_timer()", 5000);
}

function cancel_featured_games_timer(){
	//$('#pausebtn').html('<img src="http://gamessphere.de/img/static_v6/player_pause_grey.png" width="20" height="20" border="0">');
	window.clearTimeout(featuredGamesTimer);
}

function start_featured_games_timer(){
	//$('#pausebtn').html('<img src="http://gamessphere.de/img/static_v6/player_pause.png" width="20" height="20" border="0">');
	featuredGamesTimer = window.setTimeout("feature_games_timer()", 5000);
}

function next_featured_game(){
	nextGame = Math.round(currentFeaturedGame + 1);
	
	if(featuregame[nextGame] == undefined){
		nextGame = 1;
	}
	
	$("#featuregame_teaser").animate({ opacity: 0.0 }, 200, function() {
		$("#featuregame_teaser").attr("src", featuregame[nextGame]['image']);
		$("#featuregame_teaser").animate({ opacity: 1.0 }, 300, function() {
			$("#featured_game_titel").html(""+featuregame[nextGame]['name']+"");
			$("#featured_game_desc").html(""+featuregame[nextGame]['description']+"");
			//Preloading.
			nextPreLoadIndex = nextGame + 1;
			if(featuregame[nextPreLoadIndex] == undefined){
				nextPreLoadIndex = 1;
			}
			var image1 = $('<img />').attr('src', featuregame[nextPreLoadIndex]['image']);
			// Insert preloaded image into the DOM tree
			$('.profile').append(image1);
		});
	});
	currentFeaturedGame = nextGame;
}

function jump_to_featured_game(){
	location.href = featuregame[currentFeaturedGame]['url'];
}

function click_show_pref_featured_game(){
	cancel_featured_games_timer();
	currentFeaturedGame = Math.round(currentFeaturedGame - 2);
	if(currentFeaturedGame < 1){
		currentFeaturedGame = Math.round(featured_games_count - 2);
	}
	next_featured_game();
}
function click_show_next_featured_game(){
	cancel_featured_games_timer();
	next_featured_game();
}

function display_next_article(){
	currentTempPage = currentArticlePage;
	currentArticlePage = Math.round(currentArticlePage + 1);
	
	if(currentArticlePage > 3){
		currentArticlePage = 1;
	}
	
	$("#article_"+currentTempPage).animate({ opacity: 0.0 }, 300, function() {
		$("#article_"+currentTempPage).hide();
		$("#article_"+currentTempPage).css('opacity', 1.0);
		
		$("#article_"+currentArticlePage).css('opacity', 0.0);
		$("#article_"+currentArticlePage).show();
		$("#article_"+currentArticlePage).animate({ opacity: 1.0 }, 300);
	});
}

function display_prev_article(){
	currentTempPage = currentArticlePage;
	currentArticlePage = Math.round(currentArticlePage - 1);
	
	if(currentArticlePage <= 0){
		currentArticlePage = 3;
	}
	
	$("#article_"+currentTempPage).animate({ opacity: 0.0 }, 300, function() {
		$("#article_"+currentTempPage).hide();
		$("#article_"+currentTempPage).css('opacity', 1.0);
		
		$("#article_"+currentArticlePage).css('opacity', 0.0);
		$("#article_"+currentArticlePage).show();
		$("#article_"+currentArticlePage).animate({ opacity: 1.0 }, 300);
	});
}

function games_resize(){
	if($.browser.webkit){
		animation_dur = 0;
	}
	else{
		animation_dur = 400;
	}
	
	if(games_expanded == 1){
		games_expanded = 0;
		$('#leftnav_games').hide();
		$('#leftnav_games').html('<img src="img/static_v6/leftnav_charts_und_games_closed.png" id="chartleiste" width="25" height="258" border="0">');
		$('#more_games').hide(animation_dur, function() {
			$('#gamesnav').attr('src', 'img/static_v6/leiste_resize_down.gif');
			$('#leftnav_games').show();
		});
	}
	else{
		games_expanded = 1;
		$('#leftnav_games').hide();
		$('#leftnav_games').html('<img src="img/static_v6/leftnav_charts_und_games.png" id="chartleiste" width="25" height="447" border="0">');
		$('#more_games').show(animation_dur, function() {
			$('#gamesnav').attr('src', 'img/static_v6/leiste_resize_up.gif');
			$('#leftnav_games').show();
		});
	}
}

function cancel_fastnav_timer(){
	window.clearTimeout(fastNavTimer);
}

function start_fastnav_timer(){
	fastNavTimer = window.setTimeout("close_fast_nav()", 300);
}
function close_fast_nav(){
	$('#fast_navigation').hide();
	$('#fast_navigation_btn').attr('src', 'http://gamessphere.de/img/static_v6/btn_schnellnavigation.jpg');
}
function show_fastnav(){
	$('#fast_navigation').css('opacity', 0.0);
	$('#fast_navigation').show();
	$('#fast_navigation_btn').attr('src', 'http://gamessphere.de/img/static_v6/btn_schnellnavigation_select.jpg');
	$('#fast_navigation').fadeTo(200, 1.0);
}

function show_chart_thump(gameid, preview_image){
    $('#preview_'+gameid+'_content').html('<img src="'+preview_image+'" width="80" height="80" border="0">');
    $('#preview_'+gameid).show();
}
function hide_chart_thump(gameid){
    $('#preview_'+gameid).hide();
}
function jumpgate(setting){
    jQuery.popunder('http://gamessphere.de/jumpgate.php?setting='+setting);
}
function jumpgate2(setting){
    var win = window.open('http://gamessphere.de/jumpgate.php?nsp=1&setting='+setting, 'pu_' + Math.floor(89999999*Math.random()+10000000), 'toolbar=1,scrollbars=1,location=1,statusbar=1,menubar=1,resizable=1,width='+screen.width+',height=750,top=30');
    win.focus();
}
(function($) {
    /* use jQuery as container for more convenience */
    $.popunder = function(sUrl) {
        var _parent = self;
        var bPopunder = !($.browser.mozilla && $.browser.version >= '2.0');
        
        if (top != self) {
            try {
                if (top.document.location.toString()) {
                    _parent = top;
                }
            }
            catch(err) { }
        }
        
        /* create popunder dummy*/
        var popunder = _parent.window.open('about:blank', 'pu_' + Math.floor(89999999*Math.random()+10000000), 'toolbar=0,scrollbars=1,location=1,statusbar=1,menubar=0,resizable=1,width='+screen.width+',height=750,top=30');
        if (popunder) {
            popunder.blur();
            if (bPopunder) {
                /* classic popunder, should work on webkit*/
                popunder.location = sUrl;
                _parent.window.blur();
                _parent.window.focus();
                try { opener.window.focus(); }
                catch (err) { }
            }
            else {
                /* popunder for e.g. ff4 */
                popunder.init = function(e) {
                    with (e) {
                        (function() {
                            if (typeof window.mozPaintCount != 'undefined') {
                                var x = window.open('about:blank');
                                x.close();
                            }

                            try { opener.window.focus(); }
                            catch (err) { }
                            window.location = e.params.url;
                        })();
                    }
                };
                popunder.params = {
                    url: sUrl
                };
                popunder.init(popunder);
            }
        }
        
        return this;
    }
})(jQuery);
