$(document).ready(function(){
	//console.log('output');
	console_debug = true;
	
	$('#header').hover(
		function () {
			$(this).addClass('hover');
		}, 
		function () {
			$(this).removeClass('hover');
		}
	);
	
	$('.lightbox a[class!="download nobox"][class!="share nobox"][rel!="nobox"]').lightBox();
	
	even_sides();
	$.preloadCssImages();
	background_images();
	news_scroller();
	video_toggle();
	filmmaker_form();
	critic_form();
	ajax_file_uploads();
	pagination_switcher();
	//$("#header_wrapper, #top_nav, #navigation, #panel, #main").pngFix();
	$(document).pngFix();
});

function even_sides() {
	//$('#content').height('');
	objects = new Array('#navigation', '#content', '#panel');
	for(i=0;i<objects.length;i++) {
		$(objects[i]).height('');
	}
	
	objects = new Array('#navigation .bottom .top', '#panel .bottom .top');
	max = 0;
	for(i=0;i<objects.length;i++) {
		current_height = $(objects[i]).height();
		if(current_height > max) {
			max = current_height
		}
	}
	for(i=0;i<objects.length;i++) {
		$(objects[i]).height(max);
	}
	
	objects = new Array('#navigation', '#content', '#panel');
	max = 0;
	for(i=0;i<objects.length;i++) {
		current_height = $(objects[i]).height();
		if(current_height > max) {
			max = current_height
		}
	}
	for(i=0;i<objects.length;i++) {
		$(objects[i]).height(max);
	}
}

function background_images() {
	/*
	 * BACKGROUND IMAGE TRANSITIONS
	 */
	
	if($('#background').height() < $('#container').height())
		$('#background').height($('#container').height());
	
	fade_speed = fade_speed * 1000;
	fade_interval = (fade_interval * 1000) + fade_speed;
	
	path = '/img/backgrounds/original/';
	
	var keys = new Array();
	var formats = new Array();
	var preloads = new Array();
	var current_img = 0;
	for (key in backgrounds) {
		keys.push(key);
		formats.push(backgrounds[key]);
		preloads.push(path+key+'.'+backgrounds[key]);
	}
	var length = keys.length;
	
	$('#background').hide();
	
	preload = function(){
		if(current_img < length-1) {
			current_img++;
		} else {
			current_img = 0;
		}
		
		jQuery("<img>").load(function(event) {
			//event.success // jquery
			$('#background').fadeOut(fade_speed, function() {
				$('#background').css('background-image', 'url('+path+keys[current_img]+'.'+formats[current_img]+')');
				$('#background').fadeIn(fade_speed, function(){
					bgTimeout = setTimeout('preload()', fade_interval);
				});
			});
		}).attr("src", preloads[current_img]);
	}
	preload();
	
}

function news_scroller() {
	/*
	 * NEWS SCROLLER FUNCTIONALITY
	 */
	var counter = 0;
	var scroll_size = 2;
	var total_height = 0;
	var building_width = 0;
	var headline_margin = 30;
	var interval = 25; // milliseconds
	
	if (ticker_speed) {
		var interval = ticker_speed/500;
	}
	
	$("#headlines ul").liScroll({travelocity: interval});
}

function video_toggle() {
	$('.video_details .toggle').toggle(
      function (e) {
	  	e.preventDefault();
		/*
		$('.video_details .description').slideDown('fast', function(){
			$('.video_details .synopsis').slideDown('fast', function(){
				even_sides();
			});
		});
		*/
		$('.video_details .description').show();
		$('.video_details .synopsis').show();
		even_sides();
		$(this).addClass('opened');
      },
      function (e) {
	  	e.preventDefault();
		/*
		$('.video_details .synopsis').slideUp('fast', function(){
			$('.video_details .description').slideUp('fast', function(){
				even_sides();
			});
		});
		*/
		$('.video_details .description').hide();
		$('.video_details .synopsis').hide();
		even_sides();
		$(this).removeClass('opened');		
      }
    );
}

function filmmaker_form(){
	$('#filmmaker_submit').live("click", function(e){
		$('#ajax_form').append("<input type='hidden' name='data[Filmmaker][url]' value='"+window.location+"' />");
		e.preventDefault();
		$('#update_status').show();
		even_sides();
		$.ajax({
			type: "POST",
			cache: false,
			url: $('#ajax_form').attr('action'),
			data: $("#ajax_form").serialize(),
			success: function(response){
				$('#filmmaker_details').html(response);
				$('#update_status').hide();
				even_sides();
			}
		});
    });
}

function critic_form(){
	$('#critic_submit').live("click", function(e){
		$('#ajax_form').append("<input type='hidden' name='data[Critic][url]' value='"+window.location+"' />");
		e.preventDefault();
		$('#update_status').show();
		even_sides();
		$.ajax({
			type: "POST",
			cache: false,
			url: $('#ajax_form').attr('action'),
			data: $("#ajax_form").serialize(),
			success: function(response){
				$('#critic_details').html(response);
				$('#update_status').hide();
				even_sides();
			}
		});
    });
}

function ajax_file_uploads() {
	if($("#picture_upload").length>0) {
		path_inject = $("#picture_upload").attr('class').split(' ');
		new AjaxUpload('picture_upload', {
			data: { type : 'image' },
			action: '/'+path_inject[0]+'/upload',
			responseType: 'text/html',
			onChange: function(file, extension){},
			onSubmit: function(file, extension) {
				if (! (extension && /^(jpg|png|jpeg|gif)$/.test(extension))){
					alert('Error: invalid file extension. Please use one of the following formats: JPG, PNG, GIF');
					return false;
				} else {
					$('#picture_upload_complete').hide();
					$('#picture_upload_status').show();
				}
			},
			onComplete: function(file, response) {
				$('#picture_upload_status').hide();
				$('#picture_upload_complete').show();
				$('#picture_display').html(response);
				even_sides();
				if(console_debug) console.log(response);
			}
		});
	}
	if($("#document_upload").length>0) {
		new AjaxUpload('document_upload', {
			data: { type : 'document' },
			action: '/_filmmakers/upload',
			responseType: 'text/html',
			onChange: function(file, extension){},
			onSubmit: function(file, extension) {
				if (!(extension && /^(doc|txt|pdf|rtf)$/.test(extension))) {
					alert('Error: invalid file extension. Please use one of the following formats: DOC, TXT, PDF, RTF');
					return false;
				}
				else {
					$('#document_upload_complete').hide();
					$('#document_upload_status').show();
				}
			},
			onComplete: function(file, response) {
				$('#document_upload_status').hide();
				$('#document_upload_complete').show();
				even_sides();
				if(console_debug) console.log(response);
			}
		});
	}
	if($("#video_upload").length>0) {
		new AjaxUpload('video_upload', {
			data: { type : 'video' },
			action: '/_filmmakers/upload',
			responseType: 'text/html',
			onChange: function(file, extension){},
			onSubmit: function(file, extension) {
				if (!(extension && /^(mov|flv|mp4|wmv|avi|mpg|mpeg|m4v)$/.test(extension))) {
					alert('Error: invalid file extension. Please use one of the following formats: MOV, FLV, MP4, WMV, AVI, MPG, MPEG, M4V');
					return false;
				}
				else {
					$('#video_upload_complete').hide();
					$('#video_upload_status').show();
				}
			},
			onComplete: function(file, response) {
				$('#video_upload_status').hide();
				$('#video_upload_complete').show();
				$('#picture_display').html(response);
				even_sides();
				if(console_debug) console.log(response);
			}
		});
	}
}

function pagination_switcher(){
	$('#video_grid .video_pagination a').live("click", function(e){
		e.preventDefault();
		$('#update_status').show();
		even_sides();
		current_sorting = $('#video_grid .video_pagination .video_sorting').attr('value');
		target = escape($(this).attr('href'));
		$.ajax({
			type: "POST",
			cache: false,
			url: '/_videos/pagination'+target,
			data: { sorting : current_sorting },
			success: function(response){
				$('#video_grid').html(response);
				$('#update_status').hide();
				even_sides();
			}
		});
    });
	
	$('#video_grid .video_pagination .video_sorting').live("change", function(e){
		form = $(this).parents('form');
		//alert($(form).serialize());
		//alert($(form).attr('action'));
		e.preventDefault();
		$('#update_status').show();
		even_sides();
		$.ajax({
			type: "POST",
			cache: false,
			url: $(form).attr('action'),
			data: $(form).serialize(),
			success: function(response){
				$('#video_grid').html(response);
				$('#update_status').hide();
				even_sides();
			}
		});
	});
}

function post_play(action) {
	switch(action) {
		case 'vote': $('#voting.multiple').show(); even_sides(); break;
	}
}
