﻿var curNavSection = "";
var curSubNavSection = "";

var ratio = null;
var t1 = 0;
var t2 = 0;
var t3 = 0;
var once=true;
$(document).ready(function() {
    //Initialization code in here.
    $.preloadImages(imgArray);

    //Popins
    $("a.Fprivacy").fancybox({
        'hideOnContentClick': false,
        'padding': 20
    });

    //Fade Text Links
//    $("#Tnav a").mouseover(function() {
//        $(this).stop();
//        $(this).animate({
//            color: "#50b848"
//        }, 750);
//    });

//    $("#Tnav a").mouseout(function() {
//        $(this).stop();
//        $(this).animate({
//            color: "#666666"
//        }, "medium");
//    });

    $("#Tnav a").find("a[href='/Donate.aspx']").each(function() {
        $(this).addClass("navCurrent")
        //add your own logic here if needed
    })


    $("a.Fcontact").fancybox({
        'hideOnContentClick': false,
        'frameWidth': 350,
        'frameHeight': 300,
        'padding': 20
    });

    //Fade Rollover Code
    $(".ui-fade-rollover").each(function() {
        if ($(this).attr("effect_loaded") == "true") return;

        $(this).attr("effect_loaded", "true");

        if ($(this).attr("alt") != null && curNavSection != "" && $(this).attr("alt").indexOf(curNavSection) != -1)
            $(this).attr("src", $(this).attr("src").replace("_off", "_over"));

        if ($(this).attr("alt") != null && curSubNavSection != "" && $(this).attr("alt").indexOf(curSubNavSection) != -1)
            $(this).attr("src", $(this).attr("src").replace("_off", "_over"));

        var wrapper = "<div style='display: block; height: auto; width: auto; background-repeat: no-repeat; background-image: url(" + $(this).attr("src").replace("_off", "_over") + ")'></div>";

        if ($(this).parent().get(0).tagName == "A")
            $(this).parent().wrap(wrapper);
        else
            $(this).wrap(wrapper);

        $(this).bind("mouseenter", function(e) {
            $(this).stop().animate({ opacity: 0 }, 300);
        });
        $(this).bind("mouseleave", function(e) {
            $(this).stop().animate({ opacity: 1 }, 300);
        });
    });
});

$(window).load(function()
{
    resizenow();


	    	        //Twitter Feed
	    	        $("#twitter").getTwitter({
	    	            userName: "caretolearn",
	    	            numTweets: 1,
	    	            loaderText: "Loading last tweet...",
	    	            errorText: "Error retrieving tweet",
	    	            slideIn: false,
	    	            //slideDuration: 750,
	    	            showHeading: false,
	    	            //headingText: "Latest Tweets",
	    	            showProfileLink: false,
	    	            showTimestamp: true
	    	        });



});



$(window).bind("resize", function()
{
    resizenow();
    
});

$.preloadImages = function()
{
    for (var i = 0; i < imgArray.length; i++) {
        $("<img>").attr("src", "/images/background/" + imgArray[i]);
    }
}

function resizenow()
{
    var browserwidth = $(window).width();
    var browserheight = $(window).height();

    if (ratio == null || isNaN(ratio)) {
        ratio = $("#background img").height() / $("#background img").width();
    }
    if ((browserheight / browserwidth) > ratio) {
        $("#background img").height(browserheight);
        $("#background img").width(browserheight / ratio);
        $("#background img").children().height(browserheight);
        $("#background img").children().width(browserheight / ratio);
    } else {
        $("#background img").width(browserwidth);
        $("#background img").height(browserwidth * ratio);
        $("#background img").children().width(browserwidth);
        $("#background img").children().height(browserwidth * ratio);
    }

    $("#background img").css("top", (browserheight - $("#background img").height()) / 2);
    $("#background img").css("left", (browserwidth - $("#background img").width()) / 2);
}

//ORIGINAL
//function getNextSelectedImg()
//{
//    var n = Math.round(Math.random() * (imgArray.length - 1));

//    if (n != selectedImg) {
//        selectedImg = n;
//        alert(n);
//    }
//    else
//        getNextSelectedImg();
//}


function getNextSelectedImg() {

    selectedImg = (selectedImg < imgArray.length - 1) ? selectedImg += 1: 0;
}

function loadBackground()
{

    $.get("/targets/background.aspx?file=" + imgArray[selectedImg] + "&r=" + Math.random(), function(data)
    {
        $("#background").hide().fadeOut("slow", function() {
            $("#background").html(data);
            $("#background").fadeIn("fast", function()
            {
                if (slideShow == "True") t1 = setTimeout("loadMessage()", 2000);
            });

            resizenow();
        });
    });
}

function getBackground()
{
    getNextSelectedImg();

    if(selectedImg < 0 || isNaN(selectedImg))
        selectedImg = 0;

    loadBackground();
}

function loadMessage()
{
    clearTimeout(t1);
    $("#message img").hide().attr('src', "/images/messages/" + imgArray[selectedImg].replace(".jpg", ".png")).fadeIn("slow");
    
    t2 = setTimeout("loadPayoff()", 3000);
}

function loadPayoff()
{
    clearTimeout(t2);
    $("#payoff img").hide().attr('src', "/images/payoff/" + imgArray[selectedImg].replace(".jpg", ".png")).fadeIn("slow");
    
    t3 = setTimeout("getNewBackground()", 7000);
}

function getNewBackground()
{
    clearTimeout(t3);
    $("#message img").fadeOut("slow");
    $("#payoff img").fadeOut("slow");
    $("#background img").fadeOut("slow", function()
    {
        getBackground();
    });
}

