var bottom_counter = 0, top_counter = 0, stuck_counter = 0;
var headers = new Array(), content = new Array();
var window_height, title, title_top_pos, temp_top = 55, use_headers = true, fontResize, window_size = new Array();
var hover_colors = ["#991c1f", "#2c7238", "#d9be26", "#1b4896", "#f2f2f2"]

$(document).ready(function(){

	window_height = getWindowHeight();

	if ($('#present li').size() > 1){
		$('#present .projects').jcarousel({
			start: 1,
			offset: 1,
			scroll: 1,
	    	wrap: 'circular',
			auto: 4,
			initCallback: function(carousel){
				carousel.clip.hover(function() {
				    carousel.stopAuto();
				}, function() {
				    carousel.startAuto();
				});
			},
	        itemVisibleInCallback: {
	            onAfterAnimation:  function(carousel, item, idx){
		        	$('.jcarousel-clip').animate({height: $(item).height()}, function(){	
						// setUpHeaders();
		        	});
		        }
	        }
		});
	}
	$(document).bind('fontresize', function(){ setUpHeaders(); });
	$('a[rel="gallery"]').colorbox();

	var left_margin = Math.floor( (document.body.clientWidth - 970) / 2 );
	var default_bg = $('body').css('backgroundColor');
	var default_nav = $('#navigation a').css('color');
	var default_fb = $('#social').css('color');
	var hover_object = [
		{
			page: "about",
			color: hover_colors[0],
			text: "#fff",
			facebook: "#fff"
		},
		{
			page: "collaborator",
			color: hover_colors[1],
			text: "#fff",
			facebook: "#fff"
		},
		{
			page: "press",
			color: hover_colors[2],
			text: "#000",
			facebook: "#5b739d"
		},
		{
			page: "support",
			color: hover_colors[4],
			text: "#000",
			facebook: "#5b739d"
		},
		{
			page: "mailing-list",
			color: default_bg,
			text: default_nav,
			facebook: default_fb
		},
		{
			page: "search",
			color: hover_colors[4],
			text: "#000",
			facebook: "#5b739d"
		},
		{
			page: "performance",
			color: hover_colors[3],
			text: "#fff",
			facebook: "#fff"
		}
	];
	var speed = 150;
	
	$('li.project').each(function(){
		var random_color = Math.floor(Math.random()*4);
		$(this).css({backgroundColor: hover_colors[random_color]});
	});
	
	$('.mailing-list').load('/wp-content/themes/fp_live/form.html').click(function(){
		$('#mailing-list, #window').show();
	});

	$('#window').click(function(){
		$(this).hide();
		$('#mailing-list').hide();
	});

	$('#navigation li a').hover(function(){
		var color = $(this).parent().index('#navigation li');
		$('body').stop().animate({ backgroundColor: hover_object[color].color }, speed);
		$('#navigation a').stop().animate({ color: hover_object[color].text }, speed);
		$('#social').stop().animate({ borderTopColor: hover_object[color].facebook, borderRightColor: hover_object[color].facebook, borderBottomColor: hover_object[color].facebook, borderLeftColor: hover_object[color].facebook, color: hover_object[color].facebook }, speed);
		$('#s').stop().animate({ backgroundColor: hover_object[color].color, color: hover_object[color].text }, speed);
	},
	function(){
		$('body').stop().animate({ backgroundColor: default_bg }, speed);
		$('#navigation a').stop().animate({ color: default_nav }, speed);
		$('#social').stop().animate({ borderTopColor: default_fb, borderRightColor: default_fb, borderBottomColor: default_fb, borderLeftColor: default_fb, color: default_fb }, speed);
		$('#s').stop().animate({ backgroundColor: default_bg, color: default_nav }, speed);
	});

	$('#sortby a').click(function(){
		var sort_class = $(this).attr('class');
		sort_class = sort_class.split(" ");
		$('#sortby a.active').removeClass('active');
		$('#past .projects li').css('visibility', 'visible');
		$('#past .projects li:not(.' + sort_class[0] + ')').css('visibility', 'hidden');
		$(this).addClass('active');
		return false;
	});
	
	$('.show-all').colorbox({width: "680px", height: "700px", iframe: true, href: $(this).attr('href') });
	
	$('.search.menu-item').append('<form id="searchform" method="get" action="http://futureperfectfestival.org/"><input id="s" name="s" type="text" value="SEARCH" size="10" tabindex="1" /></form>');
	$('#s').css({ backgroundColor: default_bg, color: default_nav }).focus(function(){
		$('#mailing-list').hide();
		var top_position = parseInt($(this).css("top"));
		if($(this).css({border: "solid 1px", top: top_position - 1 + "px", left: "9px"}).val() === "SEARCH"){
			$(this).val("");
		}
	}).blur(function(){
		var top_position = parseInt($(this).css("top"));
		if($(this).css({border: "none", top: top_position + 1 + "px", left: "10px"}).val() === ""){
			$(this).val("SEARCH");
		}
	});
	
	$('h3').click(function(e){
		e.preventDefault();
		var position = $(this).attr("rel");
		$.scrollTo(position - $(this).outerHeight() - 12, 1000);
	});
	
	$('body.slug-press .sidebar a').click(function(e){
		e.preventDefault();
		var position_id = $(this).attr("href");
		var position = $(position_id).offset();
		$.scrollTo(position, 1000);
		return false;
	});
	
	$('#title a').click(function(e){
		e.preventDefault();
		$.scrollTo(0, 1000);
		return false;
	});
	
	title = document.getElementById('title');
	if (title !== null){
		title_top_pos = $('#title').offset().top;
	}

	setUpHeaders();

});

$(window).scroll(function(){

	if (use_headers){

		if ($('body').hasClass("home")){
			if ($(this).scrollTop() > 410){
				$('#title').removeClass('bottom').addClass('fix');
			} else if ($(this).scrollTop() <= 410) {
				$('#title').removeClass('fix').addClass('bottom');
			}
		} else {
			if ($(this).scrollTop() > (title_top_pos - 10)){
				$('#title').removeClass('bottom').addClass('fix');
			} else if ($(this).scrollTop() <= title_top_pos - 10) {
				$('#title').removeClass('fix').addClass('bottom');
			}
		}
	
	formatHeaders();
	
	}
	
});

$(window).resize(function(){
	setUpHeaders();
});

var setUpHeaders = function(){
	var left_margin, temp_top_pos, temp_left_pos, temp_fixed_pos, total_header_width = 0, header_widths = new Array(), offset_width = 0, offset_top, temp;

	window_height = getWindowHeight();
	sticky_headers = document.getElementsByTagName("h3")
	left_margin = Math.floor( (document.body.clientWidth - 970) / 2 );
	
	$('ul.sidebar').css({left: left_margin});
	
	if (title !== null){
		title.style.width = title.clientWidth + "px";
		title.style.marginLeft = 0;
		if (document.getElementById('artists')){
			document.getElementById('artists').style.bottom = $(title).outerHeight() + 18 + "px";
		}
	}
	
	if ($('body').hasClass('home')){ temp_top = 85; }
	if (sticky_headers){
		for (i = 0; i < sticky_headers.length; i++){
			temp = $(sticky_headers[i]);
			total_header_width = total_header_width + temp.outerWidth() + 6;
			header_widths.push(temp.outerWidth());
		}
		first_position = Math.floor(((document.body.clientWidth) / 2) - (total_header_width / 2)) - left_margin;
		for (i = 0; i < sticky_headers.length; i++){
			temp = $(sticky_headers[i]);
			if ($('body').hasClass("home")){ offset_top = 0; } else { offset_top = i * 28; }
			temp_left_pos = offset_width + (i * 6) + first_position;
			temp_fixed_pos = offset_width + (i * 6) + first_position + left_margin;
			headers[i] = {
				id: temp.html(),
				topPosition: (temp.offset().top - offset_top),
				leftPosition: temp_left_pos,
				fixedPosition: temp_fixed_pos,
				object: temp
			}
			offset_width = offset_width + temp.outerWidth();
			$(headers[i].object).attr("rel", headers[i].topPosition);
		}
	}
	formatHeaders();
}

var formatHeaders = function(){
	var bottom_offset;

	if (use_headers){
	
		for (var i = 0; i < headers.length; i++){
			if (headers[i].id === "past"){ bottom_offset = 0; } else { bottom_offset = 6; }	
						
			// check if header is outside of the window
			if ( ( (headers[i].topPosition + headers[i].object.outerHeight() + bottom_offset) - $(window).scrollTop() ) > window_height){
			
			//	alert(headers[i].id + "fixed bottom");
				headers[i].object.css({ 
					position: "fixed", 
					bottom: bottom_offset + "px", 
					top: "", 
					left: headers[i].fixedPosition 
				});
				if (headers[i].id === "past"){
					$('#sortby').css({ 
						position: "fixed", 
						bottom: "-30px", 
						left: headers[i].fixedPosition - 225,
						top: "" 
					});
				}
			} 

			// check if header should be fixed at the top
			else if (headers[i].topPosition < ($(window).scrollTop() + temp_top)){
				headers[i].object.css({ 
					position: "fixed", 
					bottom: "", 
					top: temp_top + "px", 
					left: headers[i].fixedPosition 
				});
				if (headers[i].id === "past"){
					$('#sortby').css({ 
						position: "fixed", 
						bottom: "", 
						left: headers[i].fixedPosition - 225,
						top: temp_top + "px" 
					});
				}
			} 

			// else move with the flow of the page
			else { 
				headers[i].object.css({ 
					position: "absolute", 
					bottom: "", 
					top: 0, 
					left: headers[i].leftPosition 
				});
				if (headers[i].id === "past"){
					$('#sortby').css({ 
						position: "absolute", 
						bottom: "", 
						left: headers[i].leftPosition - 225,
						top: 0 
					});
				}
			}
		}

	}
}

var getWindowHeight = function(){
	var myHeight;
	if( typeof( window.innerWidth ) == 'number' ) {
		myHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		myHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		myHeight = document.body.clientHeight;
	}
	return myHeight;
}
