(function($) {
    $.fx.step['pulse'] = function(fx){
    	var t = (fx.now > 0.5)? 2*(1-fx.now) : 2*fx.now;
    	var val = 1.0 + t*0.1;
        $(fx.elem).css('-webkit-transform', 'scale('+val+')');
        $(fx.elem).css('-moz-transform', 'scale('+val+')');
    };
})(jQuery); 

if (typeof shnap === 'undefined') {
	shnap = {};
}

shnap.updateShnapInfo = function(shnapObj, transitioning) {
    if (transitioning) {
		$('#shnap-pic').animate({opacity: 0.0}, shnap.constants.FAST_FADE, function() {
			var curUrl = $('#shnap-pic').attr('src');
			if (curUrl === shnapObj.url) {
				$('#shnap-pic').animate({ opacity: 1.0 }, shnap.constants.SLOW_FADE);
			} else {
		        $('#shnap-pic').attr('src', shnapObj.url);
		        // onLoad handler set in renderShnap will fire	
			}
	    });
    } else {
        $('#shnap-pic').load(function() {
        	$('#shnap-pic').animate({ opacity: 1.0 }, shnap.constants.SLOW_FADE);
        });
    	$('#shnap-profile-pic').load(function() {
    		$('#shnap-profile-pic').animate({ opacity: 1.0 }, shnap.constants.FAST_FADE);	
    	});
    }
	
	shnapObj.stats = shnap.calculateStats(shnapObj.options, shnapObj.responses);
    var order = [2, 5, 3, 1, 4];
    for (var i=0; i<5; i++) {
        var bubble = shnap.statBubbles[i];
        var stat = shnapObj.stats.stats[order[i]];
        if (transitioning) {
        	bubble.renderStat(stat);
        } else {
            (function(b, s) {
                setTimeout(function() {
                    b.renderStat(s);
                }, 500);
            })(bubble, stat);	
        }
    }	
    $('#shnapper-count').html(''+shnapObj.stats.totalResponses);
    if (shnapObj.stats.totalResponses != 1) {
        $('#shnapper-count-nv').html('shnappers have');
    } else {
        $('#shnapper-count-nv').html('shnapper has');    
    }
    if (transitioning) {
    	$('.shnap-author-name').html(shnap.sanitize(shnapObj.username));
    	$('#shnap-author-shnap-count').html(shnapObj.userShnapCount);
    	$('#shnap-author-level').html(shnapObj.userLevel);
		$('#shnap-caption').html(shnap.sanitize(shnapObj.title));
		$('#shnap-author-bio').html(shnap.sanitize(shnapObj.userBio));
    	$('#shnap-profile-pic').animate({opacity: 0.0}, shnap.constants.FAST_FADE, function() {
    		var curUrl = $('#shnap-profile-pic').attr('src');
    		if (curUrl === shnapObj.profileUrl) {
    			$('#shnap-profile-pic').animate({ opacity: 1.0 }, shnap.constants.FAST_FADE);
    		} else {
    			$('#shnap-profile-pic').attr('src', shnapObj.profileUrl);
    		}
	    });
	} 
	
	var originalSize = new shnap.Size(shnapObj.width, shnapObj.height);
	var imageSize = new shnap.Size(512, 512);
	shnap.adjustForBounds(originalSize, imageSize);
	if (transitioning) {
		var stepFn = function(now, fx) {
    		$('#photo-group').css('top', shnap.calculatePhotoGroupTopOffset()+'px');
    	};	
    	$('#shnap-photo-frame').animate({
    		width: imageSize.width+'px',
    		height: imageSize.height+'px'
    	}, {step:stepFn, duration:500} );

    	if (shnapObj.refresh) {
    		shnap.slideshowCounter++;
    		$.getJSON('/ajax?cmd=random&counter='+shnap.slideshowCounter, function(shnapObj) {
    			shnap.replaceWithShnap(shnapObj);
    		});
    	}   
	} else {
		$('#shnap-photo-frame').css('width', imageSize.width+'px');
		$('#shnap-photo-frame').css('height', imageSize.height+'px');
		$('#photo-group').css('top', shnap.calculatePhotoGroupTopOffset()+'px');	
	}
};

shnap.renderShnap = function(shnapObj) {
	shnap.updateShnapInfo(shnapObj, false);
	if (shnapObj.refresh) {
		shnap.slideshowCounter++;
		$.getJSON('/ajax?cmd=random&counter='+shnap.slideshowCounter, function(shnapObj) {
			shnap.replaceWithShnap(shnapObj);
		});
	}
};

shnap.replaceWithShnap = function(shnapObj) {
	// Start loading next image
	var preloadedImage = document.createElement('img');
	preloadedImage.src = shnapObj.url;
	setTimeout(function() {
		shnap.updateShnapInfo(shnapObj, true);
	}, 7000);
};

shnap.calculatePhotoGroupTopOffset = function() {
	return Math.floor(($('#sidebar').height()-$('#photo-group').outerHeight())/2 + 23);
};

shnap.renderShnapPage = function() {
	$('#shnap-profile-pic').css('opacity', 0.0);
	$('#shnap-pic').css('opacity', 0.0);
	shnap.statBubbles = [];
	for (var i=0; i<5; i++) {
		var bubble = new shnap.StatBubble();
		bubble.div().css('left', 6+i*65+'px');
		bubble.div().css('bottom','0px');
		if (!$.browser.msie) {
			bubble.div().css('opacity', 0.0);
		}
		$('#stats-widget').append(bubble.div());
		shnap.statBubbles.push(bubble);
	}
	shnap.renderShnap(shnap.curShnap);	
};

shnap.setUpBasePage = function() {

	var linkStepFn = function(now, fx) {
		// Change color along with opacity (1.0 - white, 0.5 - pink)
		var value = Math.floor(207+(255-207)*(now-0.5)*2);
		$(fx.elem).css('color', 'rgb(255,'+value+','+value+')');
	};	
	$('a.fadeable').hover(
		function() {
            $(this).stop().animate({opacity: 0.5}, {step:linkStepFn, duration:250});
		},
        function() {
            $(this).stop().animate({opacity: 1.0}, {step:linkStepFn, duration:250});
        }
    );

};
