$(function() {
	
	$('#main-nav a:not(.selected)').hover(function() {
		$(this).stop().animate({color: '#267fc4'}, 250);
	}, function() {
		$(this).stop().animate({color: '#12234b'}, 250);
	});

	var flashId = 0;

	$('.flash-movie').each(function() {
		var $self	= $(this),
			movie	= $self.find('a').attr('href'),
			width	= parseInt($self.css('width'), 10),
			height	= parseInt($self.css('height'), 10),
			so		= new SWFObject(movie, ('swfobject-' + flashId++), width, height, 9, '#ffffff');
		so.addParam('wmode', 'transparent');
		so.write(this);
	});
	
	$('.video-player').each(function() {
		var $self	= $(this),
			movie	= $self.find('a').attr('href'),
			so		= new SWFObject('/sites/1/static/player.swf', ('swfobject-' + flashId++), 415, 220, 9, '#ffffff'),
			fv		= 'stretching=fill&file=' + escape(movie) + '&skin=/sites/1/static/modieus.swf&type=video',
			bg		= $self.find('img').attr('src');
		
		if (bg) fv += '&image=' + bg;
			
		so.addParam('allowfullscreen', 'true');
		so.addParam('flashvars', fv);
		so.write(this);
	});
	
	$('hr').each(function() {
		if (!$(this.parentNode).is('div.hr')) $(this).wrap("<div class='hr'/>");
	});
	
	$('.tabular-form table input, .tabular-form table textarea').wrap('<div class="iw"/>');

	$('input[title]').inputHint();

});

function justBehave(root) {
    
    $('.carousel', root).each(function() {
        
        var pageSize = 1;
        if ($(this).is('.carousel-default') || $(this).is('.carousel-large')) {
            pageSize = 2;
        }
	   
        var carouselWidth   = $(this).width(),
            $itemWrapper    = $(this).find('> ul'),
            $items          = $itemWrapper.find('> li'),
            itemCount       = Math.ceil($items.length / pageSize),
            itemWidth       = $items.outerWidth(true) * pageSize,
            $paginator      = $(this.parentNode).find('.x-paginator'),
            current         = 0,
            busy            = false,
            max             = itemCount - Math.ceil(carouselWidth / itemWidth);
            
        if (!$paginator.length) {
            return;
        } else {
            var $pages          = $paginator.find('a'),
                $pagesDirect    = $pages.filter('[rel=page]');
        }
        
        function scrollTo(index) {
            if (index >= 0 && index <= max && !busy) {
                busy = true;
                $itemWrapper.animate({marginLeft: -itemWidth * index}, function() {
                    current = index;
                    busy = false;
                });
            }
        }
        
        $pages.click(function() {
            var $self = $(this);
            switch ($(this).attr('rel')) {
                case 'prev': scrollTo(current - 1); break;
                case 'next': scrollTo(current + 1); break;
                case 'page': scrollTo($pagesDirect.index(this)); break;
                case 'link': return true;
            }
            return false;
        });
    
    });
    
    $('.rollover').rollover();
    
}

$(window).load(function() {

	$('.slideshow').each(function() {
		var $slideshow = $(this);
		if ($slideshow.find('li').length <= 1) return;
		setInterval(function() {
			$slideshow.find('li:last').animate({left: '-=100%'}, function() {
				$(this).prependTo($slideshow).css({left: 0});
			});
		}, 5000);
	});
	
    justBehave(document);
	
});