var current_page = false;

			   

var video_configs = {
	'practice_video': {
		file: "/images/history/practice.m4v",
		poster: "/images/history/practice.jpg",
		image: "/images/history/practice.jpg",
		width: 272, 
		height: 480 
	},
	'lindsay_video': {
		file: "/images/history/lindsay.mp4",
		poster: "/images/history/lindsay.jpg",
		image: "/images/history/lindsay.jpg",
		width: 640, 
		height: 360 
	},

	'nathan_video': {
		file: "/images/history/nathan.mp4",
		poster: "/images/history/nathan.jpg",
		image: "/images/history/nathan.jpg",
		width: 640,
		height: 360
	},
	
	'nicpeepee_front_page_video':{
		file: "/images/advertising/nicspeepee.mov", 
		poster: "/images/advertising/nicspeepee.jpg",
		image: "/images/advertising/nicspeepee.jpg",
		width: 480,
		height: 360,
		front_page: true
	},
	
	'john_basketball':{
		file: "/images/advertising/johnbasketball.mp4", 
		poster: "/images/advertising/john.jpg",
		image: "/images/advertising/john.jpg",
		width: 640,
		height: 480,
	},

	'book_fair_ad':{
		file: "/images/advertising/bookfairad.mp4", 
		poster: "/images/advertising/bookfairad.jpg",
		image: "/images/advertising/bookfairad.jpg",
		width: 640,
		height: 360,
	},
	
	'georgia_sagri_1':{
		file: "/images/advertising/sagriad.mp4", 
		poster: "/images/advertising/georgia1.jpg",
		image: "/images/advertising/georgia1.jpg",
		width: 640,
		height: 480,
	},

	'georgia_sagri_2':{
		file: "/images/advertising/sagriad2.mp4", 
		poster: "/images/advertising/georgia2.jpg",
		image: "/images/advertising/georgia2.jpg",
		width: 640,
		height: 360,
	}
};



function shuffle(o){
	for(var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
	return o;
}


var random_html = [
	
	//'<a href="http://1hundobiz.tumblr.com/"><img src="/images/blog_animated.gif" /></a>'

];

for(slug in video_configs){
	if(video_configs[slug]['front_page'] == true){
		random_html.push('<div id="' + slug + '"></div>');
	}
}


random_html = shuffle(random_html);
				


var index = 0;

//equalHeights plugin
(function($) {
	$.fn.equalHeights = function(minHeight, maxHeight) {
		tallest = (minHeight) ? minHeight : 0;
		this.each(function() {
			if($(this).height() > tallest) {
				tallest = $(this).height();
			}
		});
		if((maxHeight) && tallest > maxHeight) tallest = maxHeight;
		return this.each(function() {
			//$(this).height(tallest).css("overflow","auto");
			$(this).height(tallest);
		});
	}
})(jQuery);


function shuffle(o){
	for(var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
	return o;
}


function prev(){
	index -=1;
	if(index < 0){
		index = random_html.length - 1;
	}
	load_random_content();
}

function next(){
	index +=1;
	if(index >= random_html.length){
		index = 0;
	}
	load_random_content();
}

function load_random_content(){
	$('#random_content').html(random_html[index]);

	show_videos();
}


function show_videos(){
	for(div_id in video_configs){
		var config = {
			flashplayer: "/jwplayer/player.swf",
			skin: "/jwplayer/skins/glow/glow.zip"
		};

		for(field in video_configs[div_id]){
			config[field] = video_configs[div_id][field];
		}
		
		


		if($('#' + div_id)[0]){
			if(config['front_page'] == true){
				config['events'] = {onComplete: function(event){ next(); }};
			}

			jwplayer(div_id).setup(config);
			

		}
	}
}


var clicks = 0;

//redirect to ajax url if someone is coming directly to this page
var url = window.location.href.match(/^http:\/\/[^\/]+([^#]+)/)[1];
if(url != '/') location.href = '/#' + escape(url);


function scan_links(){
	$('a').each(function(){
		var link = $(this);
		var href = link.attr('href');
		
		if(!link.hasClass('new_window')){
			if(href && site_map[href]){
				link.address();
			}
		}
		
		if(link.hasClass('lightbox')){
			link.slimbox();
		}
		
	});
}

function align_catalog_images(){
	$('.catalog_image_row').each(function(i, row){
		var height = 0;
		row = $(row);
		
		row.find('.catalog_item img').each(function(i, img){
			var img_height = $(img).height();
			if(img_height > height) height = img_height;
		});
		
		row.find('.catalog_item').equalHeights(height);
	});
}


function hat_click(){
	if(current_page == '' || current_page == '/'){
		next();
	}else{
		$.address.value('/');
	}
}

$(function(){

	//projekktor('video');
	
	scan_links();

	$.address.change(function(event) {
		current_page = event.value;
		
		if(clicks == 0 && event.value == '/'){
			//don't do anything, content already loaded!
			load_random_content();
		}else{
			$('#random_content').html('');
			var new_page = event.value;
			$("#header a").removeClass('active');
			$('#content').load('site.php?inline=1&url=' + escape(new_page), function(){
				if(site_map[new_page]){
					if(site_map[new_page]['title']){
						document.title = "100% " + site_map[new_page]['title'];
					}else{
						document.title = "100%";
					}
					
					if(site_map[new_page]['nav_link']){
						$(site_map[new_page]['nav_link']).addClass('active');	
					}					
					
					scan_links();
					$('img').load(function(){ align_catalog_images();})
					align_catalog_images();
				
					show_videos();

					if(event.value == '' || event.value == '/'){
						//load_random_content();
						next();
					}
				}
				
			});
		}


		clicks++;
	});  
	

	//load_random_content();


	$(function(){
		$("#ticker_list").liScroll({travelocity: 0.06});
	});
	
});

