$(function() {
    startFrontpage();
});
mainMenu1Cur = 1;
var gsi = 1;
var s = 2200;
var cs = 220;
var __int = 5500;
var t = 0;
var prev = a;

var isPlaying = true;

$(document).ready(function() {
    //welcomeInterval = setTimeout("welcomeAnotherOne(1,1)", 2000);
    
	/*$("#gsn_" + gsi).addClass("grande_spot_nav_activ");
    $(".grande_spot_nav").click(function() {
        grandeClick($(this).attr("id"));
    });
	*/
	
	

	/*
    $(".grande_spot_holder").hover(function() {
        $(".grande_spot_image").fadeTo(90, 0.93)
    },
    function() {
        $(".grande_spot_image").fadeTo(90, 1)
    });
	*/

});
function startFrontpage() {
    $(".grande_spot_nav").click(function() {
        grandeClick($(this).attr("id"))
    });
    $("#gsn_" + gsi).addClass("grande_spot_nav_activ");
    $(".grande_spot_nav").css("cursor", "pointer");
	
	
	$(".grande_spot_holder").append("<div id=\"grande_spot_control\"></div>");
		$("#grande_spot_control").click( function() {
			if(isPlaying==true){
				//Pause the fader
				grandeSpotStop();
				$(this).css("background-position","0px -16px");
				isPlaying=false;
			} else {
				//Play the fader
				grandeSpotGo();
				$(this).css("background-position","0px 0px");
				isPlaying=true;
			};
		});
	
	
	$("#projectThumbs img").click(function(){
		if ($(this).attr('rel') !== undefined) {
			window.location = $(this).attr("rel");
		} else {
			grandeClick( ($(this).attr("id")).split("-")[1] );
			$("html").animate({ scrollTop: 0 }, "slow");
		};
	});
	
    if (a > 1) {
        setTimeout('t = setTimeout("grandeSpot(a,0)",__int);', 2000);
        
		/*$(".grande_spot_holder").hover(function() {
            grandeSpotStop()
        },
        function() {
            if(isPlaying!=false){grandeSpotGo()};
        })*/
    }
	
	
}

function grandeSpot(B, A) {
	//console.log("grandeSpot Called");
	//Set playing to true if it's currently false
				
	clearTimeout(t);
    if (B > 1) {
        if (A > 0) {
            if (c == gsi) {} else {
                prev = gsi;
                next = A;
                $("#gs_" + next).clone(true).attr("id", "gsv_" + next).insertBefore("#gsv_" + prev);
                $("#gsv_" + prev).fadeOut(cs).queue(function() {
                    $(this).remove();
                });
                setTimeout("$('.grande_spot_nav').removeClass(\"grande_spot_nav_activ\")", cs / 2);
                setTimeout("$('#gsn_'+next).addClass(\"grande_spot_nav_activ\")", cs / 2);
                gsi = next;
                t = setTimeout("grandeSpot(a,0)", __int)
            }
        } else {
            if (gsi >= B) {
                prev = B;
                next = 1;
                gsi = 1
            } else {
                prev = gsi;
                next = gsi + 1
            }
            $("#gs_" + next).clone(true).attr("id", "gsv_" + next).insertBefore("#gsv_" + prev);
            $("#gsv_" + prev).fadeOut(s).queue(function() {
                $(this).remove()
            });
            setTimeout("$('.grande_spot_nav').removeClass(\"grande_spot_nav_activ\")", s / 2);
            setTimeout("$('#gsn_'+next).addClass(\"grande_spot_nav_activ\")", s / 2);
            gsi = next;
            t = setTimeout("grandeSpot(a,0)", __int)
        }
    }
	// CRASHES BROWSER!!! //if(isPlaying!=true){grandeSpotStop()}; //If user has paused, make sure more images are not shown
}
function grandeSpotStop() {
	//console.log("grandeSpotStop");
    pr = $("#gsv_" + prev).css("opacity");
    if (pr != null) {
        if (pr < 0.4) {
            $("#gsv_" + prev).stop()
        } else {
            grandeClick("gsn_" + prev)
        }
    }
    clearTimeout(t);
}
function grandeSpotGo() {
	//console.log("grandeSpotGo");
    t = setTimeout("grandeSpot(a,0)", __int)
}
function grandeClick(A) {
	
	//alert(isPlaying);
	if(isPlaying!=true){
		$("#grande_spot_control").css("background-position","0px 0px");
		isPlaying=true;
	};
	
    clearTimeout(t);
    c = parseInt(A.replace("gsn_", ""));
	//console.log("a=" + a);
	//console.log("c=" + c);
    grandeSpot(a, c)
}
