(function($) {
    $(document).ready(function() {
        $('#archives #sAuthor').change(function () {
            $('#archives').submit();
        });
        $('#archives #sPeriod').change(function() {
			window.location.href	= $('#archives #sPeriod').val();
        });
        

        carousel.init('.carousel', 400, 204);


        /**
        * @section       Print button
        * @note          Open the print nav window on click
        */
        if ($('a.print').length > 0) {
            $('a.print').click ( function () {
                window.print();
                return false;
            });
        }
    });

    /**
     * @section     carousel
     */
    var carousel = {
        init: function (elmt, duration, distance, leftArrowHtml, rightArrowHtml) {
            var elmt = $(elmt);

            var leftArrow = '<a href="#" class="leftArrow"><img src="/wp-content/themes/images/btn/btn-arrow-left-2.png" alt="previous month" /></a>';
            var rightArrow = '<a href="#" class="rightArrow"><img src="/wp-content/themes/images/btn/btn-arrow-right-2.png" alt="next month" /></a>';

            if(leftArrowHtml) {leftArrow = leftArrowHtml;}
            if(rightArrowHtml) {rightArrow = rightArrowHtml;}

            elmt.find('.nav').parent().prepend(leftArrow);
            elmt.find('.nav').parent().append(rightArrow);

            if(!elmt.find('.wrapper ul li:first').hasClass('first')) {elmt.find('.wrapper ul li:first').addClass('first')};
            if(!elmt.find('.nav li:first').hasClass('first')) {elmt.find('.nav li:first').addClass('first')};

            if (elmt.find('.current').hasClass('.current')) {
                do {
                    var firstElmt = elmt.find('.wrapper ul li.first');
                    var firstElmtNav = elmt.find('.nav li.first');
                    elmt.find('.wrapper ul li.first').remove();
                    elmt.find('.nav li.first').remove();
                    elmt.find('.wrapper ul').append(firstElmt);
                    elmt.find('.nav').append(firstElmtNav);

                    elmt.find('.wrapper ul li.first').removeClass('first');
                    elmt.find('.nav li.first').removeClass('first');
                    elmt.find('.wrapper ul li:first').addClass('first');
                    elmt.find('.nav li:first').addClass('first');
                }
                while (!elmt.find('li.first').hasClass('current'));
            };
            
            carousel.bindClick(elmt, duration, distance);
            
            $('#subscribeForm #sflastname').focus(function(){
            	if ($(this).val() == 'Name') {
	                $(this).val('');
	            }
	        });
            $('#subscribeForm #sflastname').blur(function(){
            	if ($(this).val() == '') {
	                $(this).val('Name');
	            }
	        });
            $('#subscribeForm #sffirstname').focus(function(){
            	if ($(this).val() == 'First Name') {
	                $(this).val('');
	            }
	        });
            $('#subscribeForm #sffirstname').blur(function(){
            	if ($(this).val() == '') {
	                $(this).val('First Name');
	            }
	        });
            $('#subscribeForm #sfemail').focus(function(){
            	if ($(this).val() == 'E-Mail') {
	                $(this).val('');
	            }
	        });
            $('#subscribeForm #sfemail').blur(function(){
            	if ($(this).val() == '') {
	                $(this).val('E-Mail');
	            }
	        });

        },
        bindClick: function (elmt, duration, distance) {
            var elmt = $(elmt);

            elmt.find('.leftArrow').click(function () {
                if(!elmt.hasClass('move')) {
                    carousel.slide(elmt, 'left', duration, distance);
                }
                return false;
            });

            elmt.find('.rightArrow').click(function () {
                if(!elmt.hasClass('move')) {
                    carousel.slide(elmt, 'right', duration, distance);
                }
                return false;
            });

        },
        slide: function (elmt, direction, duration, distance) {
            var elmt = $(elmt);
            elmt.addClass('move');
            var generalMove = distance+"px";

            var signe = "+";
            if(direction == "right") {
                signe = "-";
            } else {
                var lastElmt = elmt.find('.wrapper ul li:last');
                var lastElmtNav = elmt.find('.nav li:last');
                elmt.find('.wrapper ul li:last').remove();
                elmt.find('.nav li:last').remove();
                elmt.find('.wrapper ul').prepend(lastElmt);
                elmt.find('.nav').prepend(lastElmtNav);

                elmt.find('.wrapper ul .first').removeClass('first');
                elmt.find('.nav .first').removeClass('first');

                elmt.find('.wrapper ul li:first').addClass('first');
                elmt.find('.nav li:first').addClass('first');

                elmt.find('.wrapper ul li').css('left', "-"+distance+"px");
                generalMove = "0px";
            }

            elmt.find('.wrapper ul li:not(.first)').css('position', 'relative').animate({
                left: signe+generalMove
            }, duration);


            elmt.find('.wrapper ul .first').css('position', 'relative').animate({
                left: signe+generalMove
            }, duration, function () {
                if(direction == "right") {
                    elmt.find('.wrapper ul li').css('left', '0');
                    elmt.find('.wrapper ul li').removeAttr('style');

                    var firstElmt = elmt.find('.wrapper ul li.first');
                    var firstElmtNav = elmt.find('.nav li.first');
                    elmt.find('.wrapper ul li.first').remove();
                    elmt.find('.nav li.first').remove();
                    elmt.find('.wrapper ul').append(firstElmt);
                    elmt.find('.nav').append(firstElmtNav);

                    elmt.find('.wrapper ul li.first').removeClass('first');
                    elmt.find('.nav li.first').removeClass('first');
                    elmt.find('.wrapper ul li:first').addClass('first');
                    elmt.find('.nav li:first').addClass('first');

                } else {
                    elmt.find('.wrapper ul li').removeAttr('style');
                }
                elmt.removeClass('move');
            });
        }
    };
})(jQuery)