// remap jQuery to $
(function ($) {})(window.jQuery);


/* trigger when page is ready */
	// Night Switch
    var hr = (new Date()).getHours();
    var min = (new Date()).getMinutes();

    if (hr >= 16  || hr < 10) {
    	if (hr == 16) {
	    	if (min >= 30) {
		        $('body').addClass('night').removeClass('day');
		        spectrum("on");
	    	}
    	}
    	else {
	        $('body').addClass('night').removeClass('day');
	        spectrum("on");
    	}
    }

$(document).ready(function () {

	var selected = "";
	var selectedurl = "";
	var lastel = "";

	$("[data-expander]").click(function() {
	
	    var effect = "easeOutQuad";
	    var el = $(this);
	    var selector = el.data("expander").selector;
	    var url = el.data("expander").url;
	    var item = el.data("expander").item;
	    var width = el.data("expander").width;
	    var innerwidth = el.data("expander").innerwidth;
	
	    if (selected == selector && url == selectedurl) {
	        //COLLAPSE OTHERS
	        $('#home').find('> .' + selected).animate({
	            width: '300px'
	        }, 'normal', effect);
	        selected = "";
	        	     footer();
	        // SCROLLTO
      // $('#home').find('div.' + selector).each(function() 
	            $("body").scrollTo(0, 800, {
	                offset: {
	                    left: -40,	
	                    top: -300
	                },
	                queue: true,
	                easing: effect
	            });
	       // });

		 }
	    else {
	        if (selected !== "") {
	            //COLLAPSE OTHERS
	            $('#home').find('> .' + selected).animate({
	                width: '300px',
	            }, 'normal', effect);
	            $(lastel).find(lastel.data("expander").item).slideUp();
	            console.log(lastel.data("expander").item);
	            selected = "";
	        }
	        $('#loading').fadeIn();
	        // AJAX LOAD
	        $('#home').find('div.' + selector + ' .grid').width(innerwidth + 'px').load(url + ' ' + item, function() {
	        	$(item).show();
	            $('.share, .fbcomments, #sidebar, .hide').hide();
	            $('.readmore').show();
	            // EXPAND THIS
	            lastel = el;
	            selected = selector;
	            selectedurl = url;
	            $('#home').find('.' + selector).animate({
	                width: width + 'px'
	            }, 'normal', effect, function() {
	             	footer();
	                // SCROLLTO
	                $("body").scrollTo($(this), 800, {
	                    offset: {
	                        left: -40,
	                        top: -300
	                    },
	                    queue: true,
	                    easing: effect
	                });
	                
	            });
	        });
	        $('#loading').fadeOut();
	    }
	    return false;
	});
    // Reset Scroll
    $("body").scrollTo({ top:0, left:0}, 0);


$("body.home [data-url]").livequery(function(){
	$(this).click(function(){
            window.location.href = $(this).data("url");
	});
});



    // Slider Up Box
    $(".slideup").livequery(function () {
        $(this).hover(function () {
            $(this).find(".detail").stop().animate({
                top: 100
            }, 90);
        }, function () {
            $(this).find(".detail").stop().animate({
                top: 240
            }, 90);
        }).click(function () {
            var url = $(this).data("url");
            window.location.href = url
        });
    });



    // Night CSS 
    $("#night").click(function () {
        var text = $(this).text();
        if (text == "off") {
            $(this).text('on');
            $('body').addClass('night').removeClass('day');
            spectrum("on");
        } else {
            $(this).text('off');
            $('body').removeClass('night').addClass('day');
            spectrum("off");
        }

    });

	// Tool Tip
    $("[title]").livequery(function () {
    	if ($(this).parents().hasClass('share') == false) {
        $(this).simpletooltip();
        }
    });

	//Cycle
    $('.cycle').cycle({
        fx: 'scrollVert',
        next: '#cycledown',
        prev: '#cycleup',
        height: 230
    });

	$('#twitter ul').cycle({
	        fx: 'scrollUp',
	        height: 105,
	        delay: 2000
	    });
	
	

	$('#mail-email').focus(function(){
		if ($(this).val() == "JOIN OUR MAILING LIST!"){
			$(this).val("");
		}
	}).blur(function(){
		if ($(this).val() == ""){
			$(this).val("JOIN OUR MAILING LIST!");
		}
	});
	
	var liDelay = 100;
	$('#home').find('> .grid_4').each(function(){
	    $(this).hide().delay(liDelay).fadeIn(1000, function (){footer()})
	    liDelay += 500;
	});
	var fDelay = 0;
	$('#features-category').find('> .grid_4').each(function(){
	    $(this).hide().delay(fDelay).fadeIn(1000, function (){footer()})
	    fDelay += 300;
	});
	
	var docwidth = $(document).width() + 16;
	$('.doc-width').each(function(){
		$(this).outerWidth(docwidth);
	});


	if ($("#fbevent").attr("href") == "") {
		$("#fbevent").hide();
	}


	$(window).scroll(function(){
	        $('#header').css({"top" : "-" + $(document).scrollTop() + "px"});
	        if ($(window).width() >= 960) {
	        $('#footer > div').css({"marginLeft" :  [$(document).scrollLeft() + 20] + "px"});

	        	//$('#footer > div').stop(true, true).animate({"marginLeft" :  [$(document).scrollLeft() + 20] + "px"}, "fast");
	        }
	         footer();
	});
	 footer();
});




var animation;

function spectrum(toggle) {
    if (toggle == "on") {
        $("#address").animate({
            color: "yellow"
        }, 1000);
        animation = window.setInterval(function () {
            var hue = 'rgb(' + (Math.floor(Math.random() * 256)) + ',' + (Math.floor(Math.random() * 256)) + ',' + (Math.floor(Math.random() * 256)) + ')';
            $("#address").animate({
                color: hue
            }, 1000);

        }, 1000);
    } else {
        window.clearInterval(animation);
        $("#address").animate({
            color: "black"
        }, 1000);
    }
}


function footer() {
	if ($("body").height() < $(document).height() && $("body").height() < [$(window).height() - 100]) {
 	$('#footer').addClass('fixed');
 }
 else {
 	$('#footer').removeClass('fixed');

 }

}


$(window).resize(function() {
 footer();
});

/*

 optional triggers

$(window).load(function() {
	
});




*/
