jQuery(document).ready(function() {

    jQuery('div.albumpic img').rotate(-15);

    // Constants
    var frameChangeSizeX = 320;
    var frameChangeSizeY = -86;
    var startPositionX = parseInt(jQuery(".album-container").css("left"));
    var startPositionY = parseInt(jQuery(".album-container").css("top"));

    // Variables
    var xPosition = 0;
    var yPosition = 0;
    var currentIndexPosition = 0;
    var minIndexPosition = ((jQuery(".albumpic").length) - 1) * -1;
    var frameMove = false;

    // Show menu if applicable.
    if ((jQuery("div.albumpic").length) > 1) {
        jQuery('div.albumbuttons').fadeIn('medium');
    }


    // Defines the pictures' positions
    jQuery(".albumpic").each(function(index) {
        //var index = jQuery(this).index;

        var elementXPosition = index * frameChangeSizeX;
        var elementYPosition = index * frameChangeSizeY;
        jQuery(this).css("margin-left", elementXPosition + "px");
        jQuery(this).css("margin-top", elementYPosition + "px");
    });


    // Any button click action.
    jQuery('.albumbutton').click(function() {

        var currentPlace = parseInt(jQuery('.album-container').css("left"));
        var strButtonId = jQuery(this).attr('id');
        if ((strButtonId == 'album-previousbutton') && (currentIndexPosition < 0)) {
            currentIndexPosition += 1;
            frameMove = true;
        }
        else if ((strButtonId == 'album-firstbutton') && (currentIndexPosition < 0)) {
            currentIndexPosition = 0;
            frameMove = true;
        }
        else if ((strButtonId == 'album-nextbutton') && (currentIndexPosition > minIndexPosition)) {
            currentIndexPosition -= 1;
            frameMove = true;
        }
        else if ((strButtonId == 'album-lastbutton') && (currentIndexPosition > minIndexPosition)) {
            currentIndexPosition = minIndexPosition;
            frameMove = true;
        }
        else {
            frameMove = false;
        }

        if (frameMove) {
            xPosition = startPositionX + (frameChangeSizeX * currentIndexPosition) + 'px';
            yPosition = startPositionY + (frameChangeSizeY * currentIndexPosition) + 'px';
            jQuery(".album-container").animate({ "left": xPosition, "top": yPosition }, { duration: 350, easing: "easeInOutExpo" });
        }

        // Buttons will be disabled accordingly
        if (currentIndexPosition == 0) {
            jQuery('.albumbutton:lt(2)').css('background-position', '0 -44px').addClass('disabled');
            jQuery('.albumbutton:gt(1)').css('background-position', '0 0').removeClass('disabled');
        }
        else if (currentIndexPosition == minIndexPosition) {
            jQuery('.albumbutton:gt(1)').css('background-position', '0 -44px').addClass('disabled');
            jQuery('.albumbutton:lt(2)').css('background-position', '0 0').removeClass('disabled');
        }
        else {
            jQuery('.albumbutton').css('background-position', '0 0').removeClass('disabled');
        }

    })
		.
		hover(
			function() {
			    jQuery(this).css('background-position', '0 -22px');
			    jQuery(this).css('cursor', 'arrow');
			}
			,
			function() {
			    jQuery(this).css('background-position', '0 0');
			}
		);

    jQuery('div.album-wrapper').css("top", 0);
});
