/**
 * noke.js
 *
 * JavaScript for Noke website
 *
 * JavaScript
 *
 *  @category JavaScript
 *  @package  Noke.me.uk
 *  @author   Adam Phillips <adam@pfunc.co.uk>
 *  @license  http://www.pfunc.co.uk pfunc
 *  @link     http://www.pfunc.co.uk
 */
var backgroundOffset = 200;
var circleBackgroundOffset = -2600;

var totalBackgroundWidth = 3277;

var backgroundOffsets = {
    home: "50px",
    contact: "150px",
    gigs: "250px",
    pictures: "350px",
    tunes: "450px",
    band: "550px"
};

var currentPageId = "home";

var circlePrefix = "/images/noke-circle-";
var currentCircleColor = 0;
var circleColors = {
    0:"yellow"
};
var circleOffsets = {
    0:0,
    1:100,
    2:100
}
var numCircleColors = 3;

var animDuration = 1500;

var pageOrders = {
    "home": 0,
    "gigs": 1,
    "tunes": 2,
    "band": 3,
    "pictures": 4,
    "contact": 5
}

var videoUrls = {
    "lateen": "http://www.youtube.com/v/i_bslQdvJEw&hl=en&fs=1",
    "igotthe": "http://www.youtube.com/v/ciLx4yPrvDI&hl=en&fs=1",
    "tubdub":"http://www.youtube.com/v/IuqUuRrpGS8&hl=en&fs=1",
    "yotto": "http://www.youtube.com/v/RGBiMbL-sGs&hl=en&fs=1"
}

$(document).ready(function(){	
    //$(".lightbox").lightbox();
    $("#javaScriptWarning").hide();
    
    $("div#link_container a").each(function() {
        
        var href = String($(this).attr("href"));
        
        var url = href.substr(1);
        
        $(this).click(function() {

            clickLink(url);

        })
        
    }).attr("href", "#");
          
    loadCircles();
    loadBackground();
    preload();
//startRain(500, 10);
	
});


function changeCircle(pageId, pageOffset)
{
    var currentCircle = circleColors[currentCircleColor];
	
    if (pageId=="")
    {
        circleColor = circleColors[0];

        //$("#circle_" + currentCircle).fadeOut(animDuration);
        $("#circle_" + circleColor).fadeIn(animDuration);
    }
    else
    {
        currentCircleColor++;
	
        if (currentCircleColor > numCircleColors) currentCircleColor = 0;
		
        /*
		circleColor = circleColors[currentCircleColor];	
		
		var newBgImage = circlePrefix + circleColor + ".png";
		*/
		
        $("#circle_" + circleColors[0]).css("opacity", 0);
		
        var backgroundPosition = pageOffset + circleBackgroundOffset;
		
        while (backgroundPosition >= totalBackgroundWidth) backgroundPosition -= totalBackgroundWidth;
        while ((backgroundPosition + totalBackgroundWidth) < totalBackgroundWidth) backgroundPosition += totalBackgroundWidth;
		
        var relativePos = backgroundPosition / totalBackgroundWidth;
        relativePos = Math.round(relativePos * 100) / 100;
        //relativePos = Math.round(relativePos * 100);
		
        //$("#circle_" + circleColors[1]).css("opacity", relativePos);
        //$("#circle_" + circleColors[2]).css("opacity",  (1 - relativePos));
		
        var newOpacity = relativePos;
		
        if (relativePos > 0.5)
        {
            newOpacity = (1-relativePos);
        }
	 
        newOpacity *= 2;
	 
        $("#circle_" + circleColors[1]).animate({
            opacity: newOpacity
        }, 1000);
        $("#circle_" + circleColors[2]).animate({
            opacity:(1 - newOpacity)
            }, 1000);

    }
		
}

function clickLink(pageid)
{
    bgOffset = backgroundOffsets[pageid];
	
    $("#content").fadeOut(300, function () {

        loadContent(pageid);

    });
	
	
    if (pageid != "tunes") $('#youtube_container').css("marginTop", "1000px");
	
    switch(pageid)
    {
        case "tunes":
		
            $('#youtube_container').show();
            $('#youtube_container').animate({
                "marginTop": "-172px"
            }, animDuration);
			
            break;
    }

    scrollBackground(pageid);
	
    currentPageId = pageid;
}

function loadBackground()
{
    $('#background_container').fadeIn(1000);

    $('#farhills_container').hide();
    $('#nearhills_container').hide();
    //$('#mountains_container').css("backgroundImage", "url('/images/layers/mountains.png')").fadeIn(1000);
    $.get("/images/layers/nearhills.png", function() {
    
        $('#nearhills_container').css("backgroundImage", "url('/images/layers/nearhills.png')");
        $('#nearhills_container').fadeIn(1000);
        
        $.get("/images/layers/farhills.png", function() {
            
            $('#farhills_container').css("backgroundImage", "url('/images/layers/farhills.png')");
            $('#farhills_container').fadeIn(1000);

        });
    });
//$('#sand_container').css("backgroundImage", "url('/images/layers/sand.png')").fadeIn(1000);
//$('#clouds_container').css("backgroundImage", "url('/images/layers/clouds.png')").fadeIn(1000);
}

function loadCircles()
{
    if ($.support.opacity)
    {
        $("#circle_" + circleColors[0]).css("opacity", 0);

        $.get(circlePrefix + circleColors[0] + ".png", function() {

            $("#circle_" + circleColors[0]).css("backgroundImage", "url('" + circlePrefix + circleColors[0] + ".png')");

            $("#circle_" + circleColors[0]).animate({
                opacity:1
            }, 1000);

        });
    }
    else
    {
        $("#circle_" + circleColors[0]).css("backgroundImage", "url('" + circlePrefix + circleColors[0] + ".png')");
    }

    //
    for (i=1; i<0; i++)
    {
        //alert(circleColors[i]);
        $("#circle_" + circleColors[i]).css("backgroundImage", "url('" + circlePrefix + circleColors[i] + ".png')");
		
        /*
		if (circleOffsets[i])
		{
			if (!(bgpos = $("#circle_" + circleColors[i]).css("background-position")))
			{
				bgpos = $("#circle_" + circleColors[i]).css("background-position-x");
				useHypenX = true;
			}
		}
		
		//alert(bgpos);
		
		bgpsplit = split(bgpos, " ");
		
		newbgpos = circleOffsets[i];
		
		if (bgpsplit[1])
		{
			newbgpos += parseInt(bgpsplit[1]);
		}

		if (newbgpos)
		{
			if (useHyphenX)
			{
				$("#circle_" + circleColors[i]).css("background-position-x", newbgpos + "px");
			}
			else
			{	
				$("#circle_" + circleColors[i]).css("background-position", newbgpos + "px");
			}
		}
		*/
		
        $("#circle_" + circleColors[i]).animate({
            opacity:0
        }, 1000);
    }
}

function loadContent(pageid)
{
    $("div#link_container a").removeClass("selected");

    $.get("/content/" + pageid + ".php", function(data)
    {
        //alert(data);
        $("#content").html(data);
        $("#content").fadeIn(500);

        $("div#link_container a#link_" + pageid).addClass("selected");
        //log in google analytics
        try
        {
            pageTracker._trackPageview("/" + pageid);
        }
        catch(err) {}

    });
}

function loadVideo(uid)
{
    url = videoUrls[uid];
	
    $('#youtube_container').show();
    $('#youtube_container').animate({
        "marginTop": "-172px"
    }, animDuration);
	
    //$('#youtube_container').html("<p>Loading.  Please wait...</p>");
	
    swfobject.embedSWF(url, "youtube_player", "266", "208", "9.0.0");
}

function mouseOver(pageid, newWidth, newHeight)
{
//$("#link_" + pageid + "_submenu").css("display", "block");
	
//$("#link_" + pageid + "_submenu").css("width", newWidth + "px")
//$("#link_" + pageid + "_submenu").css("height", newHeight + "px")
//$("#link_" + pageid + "_submenu").fadeIn(animDuration);
}

function mouseOut(pageid, newWidth, newHeight)
{
//$("#link_" + pageid + "_submenu").css("display", "none");
	
//$("#link_" + pageid + "_submenu").fadeOut(animDuration);
//$("#link_" + pageid + "_submenu").css("width", newWidth + "px")
//$("#link_" + pageid + "_submenu").css("height", newHeight + "px")
}

function preload()
{
    urlsToLoad = [
    "/images/home-over.png",
    "/images/gigs-over.png",
    "/images/tunes-over.png",
    "/images/band-over.png",
    "/images/pix-over.png",
    "/images/contact-over.png",

    "/content/band.php",

    "/images/bandicons/vox.png",
    "/images/bandicons/keys.png",
    "/images/bandicons/sax.png",
    "/images/bandicons/perc.png",
    "/images/bandicons/drums.png",
    "/images/bandicons/guitar.png"
    ];

    numUrls = urlsToLoad.length;

    for(i=0; i<numUrls; i++)
    {
        jQuery.get(urlsToLoad[i]);
    }
}

function scrollBackground(pageid)
{
    //bgOffset = backgroundOffsets[pageid];

    //cpageorder = pageOrders[currentPageId];
    //newpageorder = pageOrders[pageid];

    var bplabel = "background-position";
	
    var cBgPos = $('#farhills_container').css(bplabel);
	
    if (cBgPos == undefined)
    //looks like potential ie users
    {
        bplabel = "background-position-x";
        cBgPos = $('#farhills_container').css(bplabel);
    }
	
    if (cBgPos)
    {
        var bgPosSplit = cBgPos.split(" ");
		
        var bgX = parseInt(bgPosSplit[0]);
		
        //alert(bgX);
        var newBgXPos = bgX;
		
        //changeCircle(backgroundOffset);

        switch (pageid)
        {
            case "home":
            case "gigs":
            case "tunes":
			
                newBgXPos = (bgX + backgroundOffset);
	
                break;
				
            case "band":
            case "pictures":
            case "contact":
			
                newBgXPos = (bgX - backgroundOffset);

                break;
        }
		
        /*
		if (cpageorder > newpageorder)
		{
		}
		else
		{
		}
		*/ 

        //changeCircle(pageid, newBgXPos);

        if (bplabel == "background-position-x")
        {
            //$('#mountains_container').animate({backgroundPositionX: newBgXPos + "px"}, animDuration * 2);
            $('#farhills_container').animate({
                backgroundPositionX: newBgXPos + "px"
                }, animDuration * 2);
            $('#nearhills_container').animate({
                backgroundPositionX: newBgXPos + "px"
                }, animDuration * 2);
        //$('#sand_container').animate({backgroundPositionX: newBgXPos + "px"}, animDuration * 2);
        //$('#clouds_container').animate({backgroundPositionX: newBgXPos + "px"}, animDuration * 2);
        }
        else
        {
            //$('#mountains_container').animate({backgroundPosition: newBgXPos + "px"}, animDuration * 2);
            $('#farhills_container').animate({
                backgroundPosition: newBgXPos + "px"
                }, animDuration * 2);
            //$('#farhills_container').animate({backgroundPosition: (newBgXPos * 1.2) + "px"}, animDuration * 2);
            $('#nearhills_container').animate({
                backgroundPosition: (newBgXPos * 1.8) + "px"
                }, animDuration * 2);
        //$('#sand_container').animate({backgroundPosition: (newBgXPos  * 2.8) + "px"}, animDuration * 2);
        //$('#clouds_container').animate({backgroundPosition: (newBgXPos * 0.6) + "px"}, animDuration * 2);
        }
		
    //startRain(500, 10);
    }
}

function toggleLargePopup(id)
{
    $id = $("#" + id);
    
    if ($id.css("display") == "none")
    {
        $("#" + id).fadeIn(1000);
    }
    else
    {
        $("#" + id).fadeOut(1000);
    }
}

function startRain(amount, duration)
{
    if (!amount) amount = 500;
    if (!duration) duration = 10;

    var newBgXPos =  0 - amount;
    var newBgYPos = amount;
	

    var bplabel = "background-position";
	
    var cBgPos = $('#rain_container').css(bplabel);
	
    if (cBgPos == undefined)
    //looks like potential ie users
    {
        bplabel = "background-position-x";

        $('#rain_container').css(bplabel, "0px");

        bplabel = "background-position-y";

        $('#rain_container').css(bplabel, "0px");
    }
    else
    {
        $('#rain_container').css(bplabel, "0px 0px");
    }
	
    if (bplabel == "background-position-x")
    {
        $('#rain_container').animate({
            backgroundPositionX: newBgXPos + "px",
            backgroundPositionY: newBgYPos + "px"
        }, animDuration * 2);
    }
    else
    {
        //$('#rain_container').css("background-position", "0px 10px")

        $('#rain_container').animate({
            backgroundPosition: "-500px " + newBgYPos + "px"
        }, animDuration * duration);
	 
	
    }

}


