slideshow_interval = 5000;
slideshow_fadetime = 2500;

$(function(){
	window.defaultStatus="Design + Programming by FEINHEIT kreativ studio · www.feinheit.ch";

	bind();
});

function titleFormat(title, array, index) {
	return $(array[index]).next().clone().show();
}

function inline_youtube() {
	$.fancybox({
			'padding'		: 0,
			'autoScale'		: false,
			'transitionIn': 'elastic',
	        'easingIn': 'easeOutBack',
	        'easingOut': 'easeInBack',
			'title'			: this.title,
			'width'			: 900,
			'height'		: 540,
			'href'			: this.href.replace(new RegExp("watch\\?v=", "i"), 'v/') + '?autoplay=1&rel=0&fs=1',
			'type'			: 'swf',
			'swf'			: {
			   	 'wmode'		: 'transparent',
				'allowfullscreen'	: 'true'
			}
		});

	return false;
}

function bind() {
	
	moodboard_slideshow();
	preview_slideshow();
	
	$('.DivToggler.DivClosed').next().hide();
	$('.DivToggler').click(toggleNext);
	$('.news-list div:first').show();
	
	// accreditation form automatisation
	$('.combobox').combobox({
		autoShow: false,
		arrowURL : '/media/lib/jquery-combobox/drop_down.png'
	});
	
	check_film();
	$('#id_film_submitted').live('change', check_film);
	
	$('a[rel=lightbox]').fancybox({
        'transitionIn': 'elastic',
        'transitionOut': 'elastic',
        'easingIn': 'easeOutBack',
        'easingOut': 'easeInBack',
        'cyclic': true,
        'width': 980,
        'height': 600,
        'autoScale': false,
        'overlayOpacity': 0.7,
        'overlayColor': '#111',
        'padding': 10,
        'titlePosition': 'inside',
		'titleFormat' : titleFormat
    });
	
	$('a.trailer').click(inline_youtube);
	
	//$(".cineman_teaser").dropShadow();

}

function check_film() {
	if ($('#id_film_submitted').attr('checked')) {
		$('#row_film').show()
	} else {
		$('#row_film').hide()
	}
}

function moodboard_slideshow() {
	var slideshow = $('#moodboard img.slideshow')
	
	//var counter = Math.floor(Math.random()*(slideshow.length))
	var counter = 0;
	
	var current = $(slideshow[counter]);
	if (slideshow.length > counter + 1) {
		var next = $(slideshow[counter + 1]);
	}
	else {
		var next = $(slideshow[0]);
	}
	
	slideshow.hide();
	current.show();
	
	// raise counter for loop
	counter ++;
	
	if (slideshow.length > 1) {	
		setInterval(function(){
			current.fadeOut(slideshow_fadetime);
			next.fadeIn(slideshow_fadetime);
			
			current = next;
			
			counter++;
			
			if (slideshow.length < counter + 1) {
				counter = 0;
			}
			
			next = $(slideshow[counter]);
			
			
		}, slideshow_interval)
	}
}

function preview_slideshow() {
	if ($('.preview_holder').length == 0) {
		$('#moodboard').removeClass('big').addClass('medium');
	}
	
	preview_slideshow_col();
	setInterval(function(){
		preview_slideshow_col()
	}, 8000);
}

function preview_slideshow_col() {
	preview_slideshow_animation('.one', 2000);
	preview_slideshow_animation('.two', 4000);
	preview_slideshow_animation('.three', 6000);
}

function preview_slideshow_animation(query, delay) {
	if ($(query + ' .col_holder').children().length > 1) {
		$(query + ' .col_holder').delay(delay).animate({top: -120}, function() {
			$(query + ' .col_holder').append(
				$(query + ' .col_holder').children().first()
			);
			$(query + ' .col_holder').css('top', '0px');
		});
	}
}

function toggleNext(event) {
	event.preventDefault();
	
	if ($(event.target).parent().is('.DivToggler')) {
		toggler = $(event.target).parent();
		target = toggler.next();
	} else {
		toggler = $(event.target);
		target = toggler.next();
	}
	
	if (target.css('display') == 'none') {
		target.slideDown(400, function() {
			toggler.addClass('DivOpen');
			toggler.removeClass('DivClosed');
		});
	} else {
		target.slideUp(400, function() {
			toggler.addClass('DivClosed');
			toggler.removeClass('DivOpen');
		});
	}
}

