YSI.jQuery(document).ready(function($) {

    // Day or Night based on sunrise/sunset times
    sunrise = new Array("07:53","07:15","07:22","06:20","05:32","05:11","05:27","06:05","06:47","07:28","07:15","07:51");
    sunset = new Array("16:45","17:33","19:15","19:59","20:40","21:09","21:03","20:21","19:21","18:21","16:33","16:18");
    d = new Date();
    m = d.getMonth();
    t = d.toTimeString();
    (t > sunset[m] || t < sunrise[m]) ? $("body").addClass("night") : $("body").addClass("day");
    
    // Class rounded corners
    $(".ysi_image_button.ysi_ask_button,.ysi_image_button.ysi_answer_button").addClass("bigCorners");
    $("#ysi_user_main,#ysi_ask_question_main").addClass("mediumCorners");
    $(".skyBox,#contentWrapper,.ysi_error,.ysi_notice,.ysi_success").addClass("smallCorners");
    $("#ysi_question_main,#ysi_answers .ysi_lf span").addClass("topCorners");
    $("#ysi_new_answer,#ysi_activate_rte,#ysi_rte_help_link").addClass("bottomCorners");

    // Reorganize the necessary dom elements
    function moveNode(source,target,place) {
        source.hide();
        source.detach();
        if(place == "after") {
            target.after(source);
        } else {
            target.append(source);
        }
        source.show();
    }
    moveNode($("#ysi_nav"),$("#navItems"),"after");
    moveNode($("#ysi_category_list"),$("#contentCategories"));
    moveNode($("#ysi_topic_users"),$("#contentContributors"));
    moveNode($(".ysi_share"),$("#ysi_new_answer"),"after");
    
    // Sky boxes
    $(".skyBox").each(function() {
        var item = window[$(this).attr("id")];
        if(item.show) {
            $(this).addClass(item.type);
            $(this).addClass(item.size);
            $(this).append("<div class='skyHeader'>"+item.header+"</div>");
            $(this).append("<a class='skyPic' href='"+item.primaryUrl+"'><img src='"+item.picSrc+"'/></a>");
            $(this).append("<div class='skyBody'><a class='skyTitle' href='"+item.primaryUrl+"'>"+item.title+"</a><div class='skyText'>"+item.text+"</div>");
            $(this).append(function() {
                var l = item.footerLeft ? "<a class='left' href='"+item.footerLeftUrl+"'>"+item.footerLeft+"</a>" : "",
                    r = item.footerRight ? "<a class='right' href='"+item.footerRightUrl+"'>"+item.footerRight+"</a>" : "";
                return "<div class='skyFooter'>"+l+r+"</div>";
            });
        } else {
            $(this).remove();
        }
    });
    
    // Replace the last sky box with an ad if applicable
    if($("#adSkyWrap a").html()) {
        $(".skyBox").last().remove();
        $("#adSkyWrap").addClass("skyBox smallCorners last");
    } else {
        $(".skyBox").last().addClass("last");
    }
    $(".skyBox").fadeIn("slow");
    
    // Side boxes
    $("#aotw .sideboxPic").attr("src",aotw.winnerPic);
    $("#aotw .aotwWinner").text(aotw.winnerName);
    $("#aotw .aotwAsker").text(aotw.askerName);
    $("#aotw .aotwUrl").attr("href",aotw.questionUrl);
    $("#aotw .aotwUrl").text(aotw.questionText);
    
    // Other Cities Popup
    $("#otherCitiesLink, #otherCitiesClose").click(function() {
        $("#otherCities").slideToggle("normal");
    });

    // Smile the shroom and light up the gnome
    $("#question_title").focus(function() {
        $(".ysi_ask .ysi_box_h div").css("background-position","right top");
        $(".ysi_ask .ysi_box_h").css("background-position","right top");
    });

    // Rest/Play
    $("#rest").click(function() {
        $("#skyWrap,#contentWrapper,#footerContent").animate({"opacity":0},500);
        $(this).hide();
        $("#play").show();
    });
    $("#play").click(function() {
        $("#skyWrap,#contentWrapper,#footerContent").animate({"opacity":1},500);
        $(this).hide();
        $("#rest").show();
    });
    
    // Shooting star for night sky
    function doStar() {
        $('body.night #sstar').animate({'opacity':1}, 300);
        $('body.night #sstar').animate({
            'backgroundPosition':'0px 0px','top':'15%', 'opacity':0
        }, 500);
        $('body.night #sstar').css({
            'backgroundPosition':'80px -200px','top':'10%'
        });        
        setTimeout(doStar,3000);
    }
    //doStar();

    // Balloons for day sky
    function doBalloons() {
        var bezier_params = {
            start: { 
              x: 0, 
              y: 50, 
              angle: -30
            },  
            end: { 
              x:300,
              y:-30, 
              angle: -30, 
              length: 0.25
            }
        }
        $("#balloon1").animate({path : new $.path.bezier(bezier_params)},50000,'linear');
    }
    //setTimeout(doBalloons,1000);
    
});

