/**
 * @author czapata
 */
if (typeof CM === 'undefined' || !FC)  {
    /**
     * Compost Mania global namespace object
     * @namespace CM global namespace object
     */
    var CM = {};
}

CM.slideShow = function(parent) {
	var 
        controls = $('.sldControls span', parent),
        slides = $('.sldWrap .sld', parent),
        slideWrap = $('.sldWrap', parent),
        nav = $('.sldNumbs', parent),
        timer = null,
        delay = 5, // in secs
        current = 0,
        paused = false,
        handleControlClick = function(el) {
            var className = jQuery(this).attr('class').split(' ');
            switch (className[0]) {
                case 'prev':
                    handlePrev();
                    break;
                case 'pause':
                    handlePause(this);
                    break;
                case 'next':
                    handleNext();
                    break;
                }
        },
        handlePrev = function() {
            if (current === 0) {
                skipTo(slides.length - 1)
            } else {
                skipTo(current - 1)
            }
            restartTimer();
        },
        handlePause = function(el) {
            if (paused) {
                paused = false;
                jQuery(el).removeClass('active');
                restartTimer();
            } else {
                clearInterval(timer); 
                jQuery(el).addClass('active');
                paused = true; 
            }
        },
        handleNext = function() {
            if (current === slides.length - 1) {
                skipTo(0)
            } else {
                skipTo(current + 1)
            }
            restartTimer();
        },
        skipTo = function(index) {
            jQuery(slides.get(current)).fadeOut('fast');
            jQuery(slides.get(index)).fadeIn('slow');
            nav.find('.selected').removeClass('selected').end().find('li:eq('+ index +')').addClass('selected')
            current = index;
        },
        createNav = function() {
            slides.each(function(index){
                var item = jQuery('<li class="' + (index === 0 ? 'selected' : '') + '"><img src="/site/images/clear.gif" height="11" width="11"></li>');
                item.appendTo(nav).click(function(){
                    skipTo(index);
                    restartTimer();
                })
            })
        },
        restartTimer = function() {
            clearInterval(timer);  
            startTimer();
        },
        startTimer = function() {
            timer = setInterval(function(){
                handleNext();
            }, delay * 1000)
        },
        init = function() {
            controls.click(handleControlClick);
            createNav();
            startTimer();
        };
    init();
}


jQuery(document).ready(function(){
	$('input.dflt-txt').focus(function(){
		var el = $(this);
		if (el.val() === el.attr('rel')) 
			el.val('');
	}).blur(function(){
		var el = $(this);
		if (el.val() === '') 
			el.val(el.attr('rel'));
	})
});
