/* Code to change screenshots on Screenshots page */
function changeScreenshot(theClass, navClass) {
    $('.screenshot-nav li').removeClass('active');
    $('.'+navClass).addClass('active');
    var currentSS = $('.screenshot-wrapper-active');
    $(currentSS).removeClass('.screenshot-wrapper-active');
    $('.'+theClass).addClass('screenshot-wrapper-active');
    $(currentSS).hide();
    $('.'+theClass).show();
    
}

/* Code to change iphone screenshots on home page */
$(document).ready(function() {
    $("#steps li").hover(function() {
        $(this).addClass("active");
        if ($(this).attr('id') == 'step-1') {
            $('#iphone-screen').attr('src', 'images/screenshots/screen-checkin.png');
        } else if ($(this).attr('id') == 'step-2') {
            $('#iphone-screen').attr('src', 'images/screenshots/screen-causemall.png');
        } else if ($(this).attr('id') == 'step-3') {
            $('#iphone-screen').attr('src', 'images/screenshots/screen-profile.png');
        }
    },function(){
        $(this).removeClass("active");
    });
});

/* Logging wrapper */

function writeLog(msg) {
	if (typeof console != "undefined")
		console.log(msg);
}

function feedsURL(limit) {
    return "http://causeworld.shopkick.com/causeworld/v1/feeds/get_feeds_web?callback=?&limit=" + limit;
} 

var mostRecentEvent;

// hack to fix oversized badge
function fixRightImageUrl(url) {
	if (url == "http://causeworld.shopkick.com/causeworld/images/levels/Volunteer_med.png") {
		return "http://causeworld.shopkick.com/causeworld/images/levels/Volunteer_sm2.png"
	} else if (url == "http://causeworld.shopkick.com/causeworld/images/levels/Fundraiser_med.png") { 
		return "http://causeworld.shopkick.com/causeworld/images/levels/Fundraiser_sm2.png";
	} else if (url == "http://causeworld.shopkick.com/causeworld/images/levels/Benefactor_med.png") { 
		return "http://causeworld.shopkick.com/causeworld/images/levels/Benefactor_sm2.png";
	}
	return url;
}

function getActivities() {
    
    $.getJSON(feedsURL(15),
        function(data) {
            $.each(data.feeds, function(i,feeds){
                
                if(feeds.timestamp > mostRecentEvent) {
                
                    var d = new Date();
                    var eventTime = d.setTime(feeds.timestamp);
                    // var eventTime = d.getTime;
                    var adjustMonth = d.getUTCMonth() + 1;
                    // 2008-07-17T09:24:17Z
                    var newEventTime = d.getUTCFullYear() + '-' + adjustMonth + '-' + d.getUTCDate() + 'T' + d.getUTCHours() + ':' + d.getUTCMinutes() + ':' + d.getUTCSeconds() + 'Z';
                    // alert(newEventTime);
                    
                    var timeSince = jQuery.timeago(newEventTime);
                    var newItem = '<li><div class="avatar"><img src="' + feeds.left_image_url + '" /></div><div class="username"><strong>' + feeds.title + '</strong> ' + feeds.details + '</div><div class="timestamp">' + timeSince + '</div><div class="icon">'; 
                    
                    if(feeds.right_image_url != null) {
			feeds.right_image_url = fixRightImageUrl(feeds.right_image_url);
                    	newItem += '<img src="' + feeds.right_image_url + '" />';
			
    		    }
                    newItem += '</li>';
                    // $(newItem).fadeIn().prependTo('#activity');
                    $(newItem).hide().prependTo('#activity').slideDown();

                
                    // set mostRecentEvent to timestamp of 1st item in feed
                    if (i == 0) { mostRecentEvent = feeds.timestamp; }
                    
                    
                    // remove the oldest item in the feed
                    $('#activity li:last-child').remove();
                }
                
            });
    });    
    
}
        
$(document).ready(function() {

    NewsBox.init();

    $(".newsletter-wrapper a").overlay({
        target: 'div#newsletter-overlay',
	left: 'center',
	top: 'center'
    });

    $.getJSON(feedsURL(16),
        function(data) {
            $.each(data.feeds, function(i,feeds){
                
                
                var d = new Date();
                var eventTime = d.setTime(feeds.timestamp);
                // var eventTime = d.getTime;
                var adjustMonth = d.getUTCMonth() + 1;
                // 2008-07-17T09:24:17Z
                var newEventTime = d.getUTCFullYear() + '-' + adjustMonth + '-' + d.getUTCDate() + 'T' + d.getUTCHours() + ':' + d.getUTCMinutes() + ':' + d.getUTCSeconds() + 'Z';
                // alert(newEventTime);
                var timeSince = jQuery.timeago(newEventTime);
                
                var item = '<li><div class="avatar"><img src="' + feeds.left_image_url + '" /></div><div class="username"><strong>' + feeds.title + '</strong> ' + feeds.details + '</div><div class="timestamp">' + timeSince + '</div><div class="icon">';
                if(feeds.right_image_url != null)
			feeds.right_image_url = fixRightImageUrl(feeds.right_image_url);
	                item += '<img src="' + feeds.right_image_url + '" />'; 
               	item += '</li>'
                $('#activity').append(item);
                
                // set mostRecentEvent to timestamp of 1st item in feed
                if (i == 0) { mostRecentEvent = feeds.timestamp; }
            });
    });        

    // initialize scrollable
    $("#causes-wrapper").scrollable({
        clickable: false,
        size: 2
    }).autoscroll().circular();

    setInterval( "getActivities()", 5000 ); 

});
