// JavaScript Document

jQuery.jQueryRandom = 0;
jQuery.extend(jQuery.expr[":"],
{
    random: function(a, i, m, r) {
        if (i == 0) {
            jQuery.jQueryRandom = Math.floor(Math.random() * r.length);
        };
        return i == jQuery.jQueryRandom;
    }
});

var controlsLocked = false;
var currentItem = 1;

jQuery(document).ready(function(){

	if(jQuery(".editmode").text() != "editmode"){
		buildNav();
		
		if(typeof document.body.style.maxHeight === "undefined"){
			//ie6 detected, init tablike behavior
			buildIE6tabs();
		}else{
			//other browser, jCarousel
			jQuery('#allegro_slideshow').jcarousel({
				initCallback: slideshow_initCallback,
				itemLoadCallback:{
					onBeforeAnimation: lockControls,
					onAfterAnimation: unlockControls
				},
				itemVisibleInCallback:{
					onBeforeAnimation: highlightNumber
				},
				scroll:1,
				visible:1,
				auto:8,
				wrap:'last',
				animation:500
			});
		}
	}
		
	function buildNav(){
		jQuery("#allegro_slideshow li").each(function(i){
			var classes = "slideshow_nr";
			if(i==0){
				classes = "slideshow_nr selected";			
			}
			jQuery('#allegro_slideshow_controls').append('<a href="#" class="'+ classes +'" id="'+ (i+1) +'">'+ jQuery("#allegro_slideshow li:eq("+i+")").find('.title').html() +'</a>');	
		});
	}
	
	function buildIE6tabs(){
		jQuery("#allegro_slideshow").addClass("ie6");
		jQuery('#allegro_slideshow_controls a').bind('click', function(){
			var tabToShow = jQuery(this).attr('id');
			showTab(tabToShow);
			return;
		});
		showTab(currentItem);
	}
	function showTab(itemnr){
		jQuery("#allegro_slideshow li:eq(" + (itemnr-1) + ")").show('fast').siblings().hide('fast');
		jQuery('#allegro_slideshow_controls a:eq('+ (itemnr-1) +')').addClass('selected').siblings().removeClass('selected');
	}
	
	function slideshow_initCallback(carousel){
		
		jQuery('#allegro_slideshow_controls a').bind('click', function() {			
			var newItem = jQuery.jcarousel.intval(jQuery(this).attr('id'));
			if(!controlsLocked && currentItem != newItem){
				carousel.scroll(newItem);
				currentItem = newItem;
				jQuery(this).addClass('selected').siblings().removeClass('selected');
			}
			carousel.stopAuto();
			carousel.startAuto(1000);
			return false;
		});
		
	}
		
	function highlightNumber(carousel, item, idx, state){
		jQuery('#allegro_slideshow_controls a:eq('+ (parseInt(idx)-1) +')').addClass('selected').siblings().removeClass('selected');
		currentItem = parseInt(idx);
	}
	
	function lockControls(carousel, state){
		controlsLocked = true;
	}
		
	function unlockControls(carousel, state){
		controlsLocked = false;
	}	
});		

/* clients & spinoffs slideshows */
function slideSwitch() {
    var $slideshows = jQuery('.allegro_company_slideshow')

	jQuery.each($slideshows, function(){	
		var $current = jQuery(this).find('div.active');
		if($current.length == 0) $current = jQuery(this).find('div.company_slide:last');
		
		var $next =  $current.next().length ? $current.next() : jQuery(this).find('div.company_slide:first');

		$current.addClass('last-active');
	
		$next.css({opacity: 0.0})
			.addClass('active')
			.animate({opacity: 1.0}, 1000, function() {
				$current.removeClass('active last-active');
				jQuery(this).remove('div.white');
			});
	});
}

jQuery(function() {
	var $slideshows = jQuery('.allegro_company_slideshow')
	jQuery.each($slideshows, function(){
		jQuery(this).find('div.company_slide:random').addClass('active');
	});
	
	setInterval( "slideSwitch()", 5000 );
});