$(document).ready(function() {
	var slider = $('#s3slider');
	
	if (slider.length) {
		slider.s3Slider({
			timeOut: 4000
		});
	}
	
	$('add_to_favourites').click(function () {
		alert($(this).html())
		return false;
	})

	var carousel = $('#week_items');
	
	if (carousel.length) {
		carousel.jcarousel({
			size:				60,
			scroll:				6,
			initCallback:		carousel_initCallback,
			itemLoadCallback:	carousel_itemLoadCallback,
			buttonNextHTML:		null,
			buttonPrevHTML:		null
		});
	}
	
	$(document).pngFix();
	$('input[type=password]').keypress(function(e) {   	  
		var charCode = e.which;	   
		var shifton = false;

		if (e.shiftKey) {
			shifton = e.shiftKey;
		} else if (e.modifiers) {
			shifton = boolean(e.modifiers & 4);
		}
	   
		if (charCode >= 97 && charCode <= 122 && shifton) {
			return caps_lock_on($(this));
		}
	   
		if (charCode >= 65 && charCode <= 90 && !shifton) {
			return caps_lock_on($(this));
		} 

		$('span.caps-lock-on').remove();	   
	});
});

function caps_lock_on(element)
{
	if ($('span.caps-lock-on', $(element).parent()).length == 0) {
		$('<span class="caps-lock-on"><small>Warning: Caps lock is currently switched on</small><br /></span>').insertBefore(element);
	}
}

function carousel_initCallback(carousel) {
    $('#week_next').bind('click', function() {
        carousel.next();
		
		// inevitable IE6 hack
		if ( $.browser.msie && (parseInt($.browser.version) == 6) ) {
			$('body').addClass('usegif');
		}
		
        return false;
    });

    $('#week_prev').bind('click', function() {
        carousel.prev();
        return false;
    });
};

function carousel_itemLoadCallback(carousel, state)
{
    // Check if the requested items already exists
    if (carousel.has(carousel.first, carousel.last)) {
        return;
    }

    $.get(
        ar('/inc/ajax/public/dates'),
        {
            first: carousel.first,
            last: carousel.last
        },
        function(xml) {
            carousel_itemAddCallback(carousel, carousel.first, carousel.last, xml);
        }
    );
};

function carousel_itemAddCallback(carousel, first, last, xml)
{
    // Set the size of the carousel
	carousel.size(carousel.size() + 6);

    $('#week_items LI', xml).each(function(i) {
        carousel.add(first + i, jQuery(this).html()).addClass('slot');
    });
};
