﻿/// <reference path="/js/jquery-1.3.2.js" />
var curNavSection = "";
var curSubNavSection = "";


$(document).ready(function() {
    //Initialization code in here.
    //Fade Rollover Code

    //Load up the Join the Project Feed

    if ($(".twitter_feed").length > 0) {
        $.getJSON("http://twitter.com/users/show/26604436.json?callback=?", function(data) {
            var age = dateToAge(data.status.created_at);
            if (age == "NaN seconds ago") age = "";
            $(".twitter_feed .dyn").html("&ldquo;" + data.status.text + "&rdquo;<br /><div class='tweet_age'>" + age + "</div>");
        });
    }

    $("div.scrollable").scrollable({
        size: 2
    });

    $("div.workscrollable").scrollable({
        size: 1
    });

    if (curSubNavSection != "") {
        $("a[rel='" + curSubNavSection + "']").addClass("activepage");
    }
    $("div.navi a:eq(0)").attr("id", "scroll_one");
    $("div.navi a:eq(1)").attr("id", "scroll_two");
    $("div.navi a:eq(2)").attr("id", "scroll_three");
    $("div.navi a:eq(3)").attr("id", "scroll_four");
    $("div.navi a:eq(4)").attr("id", "scroll_five");
    $("div.navi a:eq(5)").attr("id", "scroll_six");
    $("div.navi a:eq(6)").attr("id", "scroll_seven");
    $("div.navi a:eq(7)").attr("id", "scroll_eight");
    $("div.navi a:eq(8)").attr("id", "scroll_nine");
    $("div.navi a:eq(9)").attr("id", "scroll_ten");

    $(".ui-swap-rollover").each(function() {
        if ($(this).attr("ui-swap-rollover-effect_loaded") == "true") return;
        $(this).attr("ui-swap-rollover-effect_loaded", "true");

        var isOn = false;

        if ($(this).attr("alt") != null && curNavSection != "" && $(this).attr("alt").indexOf(curNavSection) != -1) {
            $(this).attr("src", $(this).attr("src").replace("_off", "_over"));
            isOn = true;
        }

        if ($(this).attr("alt") != null && curSubNavSection != "" && $(this).attr("alt").indexOf(curSubNavSection) != -1) {
            $(this).attr("src", $(this).attr("src").replace("_off", "_over"));
            isOn = true;
        }

        if (!isOn) {
            $(this).bind("mouseenter", function(e) {
                $(this).attr("src", $(this).attr("src").replace("_off", "_over"));
            });
            $(this).bind("mouseleave", function(e) {
                $(this).attr("src", $(this).attr("src").replace("_over", "_off"));
            });
        }

    });

    $(".ui-fade-rollover").each(function() {
        if ($(this).attr("ui-fade-rollover-effect_loaded") == "true") return;
        $(this).attr("ui-fade-rollover-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);
        });

    });




    jQuery('#parallax').jparallax({ triggerExposesEdges: true });

    jQuery('a.goto').click(function() {
        var ref = jQuery(this).attr("href");
        // console.log(ref);
        jQuery('#parallax').trigger("jparallax", [ref]);
        return false;
    });
    // if ($.browser.msie && parseInt($.browser.version.substr(0, 1)) < 7) {
    //nothing
    //} else {
    // $("a#contact").fancybox({ 'hideOnContentClick': false, 'overlayShow': true, 'overlayOpacity': .80, 'frameWidth': 513, 'frameHeight': 343 });
    //}
});


function dateToAge(time)  {
   var month = 1000 * 60 * 60 * 24 * 30;
   var week = 1000 * 60 * 60 * 24 * 7;
   var day = 1000 * 60 * 60 * 24;
   var hour = 1000 * 60 * 60;
   var minute = 1000 * 60;
   var second = 1000;
   
   var now = new Date();
   var then = new Date();
   then.setTime(Date.parse(time));

   var diff = now - then;
   var text = "";
   var timeType = "";
   if (diff > month)  {
       text = Math.floor(diff / month);  
       textType = " month";
   }
   else if (diff > week)  {
       text = Math.floor(diff / week);
       textType = " week";
   }
   else if (diff > day)  {
       text = Math.floor(diff / day);
       textType = " day";
   }
   else if (diff > hour)  {
       text = Math.floor(diff / hour);
       textType = " hour";
   }
   else if (diff > minute)  {
       text = Math.floor(diff / minute);
       textType = " minute";
   }
   else  {
       text = Math.floor(diff / second);
       textType = " second";
   }

   text += textType += text == "1" ? "" : "s"; 
   text += " ago";
   
   return text;
}



function scrollTo(num, obj) {
    $("div.workscrollable").scrollable().setPage(num);
    $(".scrollbutton").removeClass("active");
    $(obj).addClass("active");
    return false;
}