//
//	on load
//
$(document).ready(function() {
	$("#alpha_div").css("opacity", ".5");

	$(window).bind("hashchange",function() {
		var page_name = location.hash;
		if ( page_name == "" || page_name == "#" ) {
			page_name = "news";
		} else {
			page_name = page_name.substring(1);
		}
		show_page( page_name );
		document.title = "Umberto Crenca";
	});


	$(window).resize(function(){
		fix_dims();
	});
	load_page( "bio", $("#page_bio") );
	load_page( "works", $("#page_works") );
	load_page( "video", $("#page_video") );
});
$(window).load(function(){
	$(window).trigger("hashchange");
});

//
//	main site functions
//
window.current_page = null;
function show_page ( page_name ) {
	if ( window.current_page == page_name ) return;
	$(".page_link").removeClass("link_selected");
	$("#link_"+page_name).addClass("link_selected");
	var old_page = window.current_page;
	window.current_page = page_name;
	fix_dims();
	$(".page").hide();
	$("#page_" + page_name).show();
	if ( page_name == "video" ) {
		//show_video( "no_manifesto" );
		show_video( "frenetic_engineering" );
	}
	if ( old_page == "video" ) {
		$("#page_video_player_cell").html("");
	} else if ( old_page == "project" ) {
		$("#carousel_cell").children().remove();
		window.current_image = -1;
		$("#big_image").children().remove();
	}
}
function fix_dims () {
	var $ps = $(".page");
	var $cd = $("#content_div");
	var cdw = $cd.width();
	var cdh = $cd.height();
	$("#alpha_div").width( cdw )
		.height( cdh );
	$ps.width( cdw );
	$ps.height( cdh );

	if ( window.current_page == "bio" ) {
		var $bt = $("#bio_text");
		var h = $("#page_bio").height();
		var w = $("#page_bio").width();
		if ( !$.browser.msie ) {
			h -= 2 * ( parseInt($bt.css("padding-top")) );
			w -= 2 * ( parseInt($bt.css("padding-left")) );
		}
		$bt.height( h );
		$bt.width( w );
	} else if ( window.current_page == "video" ) {
		$("#page_video_inner").width( cdw - 40 ) 
		$("#page_video_inner").height( cdh - 40 ) 
	} else if ( window.current_page == "project" ) {
		if ( window.current_image != -1 ) {
			var $bi = $("#big_image");
			var $bip = $bi.parent();
			var h = $("#page_project").height() - 200;
			$bip.height( h );
			img_list_index = window.current_image;;
			var img_data = img_list[img_list_index];
			if ( img_data.w == 800 ) {
				if ( h > img_data.h ) {
					h = img_data.h;
				}
			} else if ( img_data.h == 800 && h > 800 ) {
				h = img_data.h;
			}
			$bi.children("img").height(h);
		}
	}
}

//
//	works page functions
//
window.img_list = [];
function show_project( proj_name ) {
	var name_split = proj_name.split("_");
	if ( name_split.length > 1 ) {
		name_split.shift();
	}
	var text_name = name_split.join("_");
	$("#page_project_name").html(text_name);
	show_page( "project" );
	$.get( "php/get_img_list.php", { dir: proj_name }, function ( data ) {
		var in_list = eval( data );
		var out_list = [];
		$.each(in_list, function(index, value) { 
			var new_obj = {};
			new_obj.full = "works/" + proj_name + "/" + value.name;
			new_obj.thumb = "works/" + proj_name + "/thumbs/" + value.name;
			new_obj.w = value.width;
			new_obj.h = value.height;
			out_list.push( new_obj );
		});
		window.img_list = out_list;
		var $ul = $('<ul class="jcarousel-skin-bert"></ul>');
		$ul.appendTo("#carousel_cell");
		$ul.jcarousel({
			size: window.img_list.length,
			/*visible: 6,*/ 
			scroll: 5,
			itemLoadCallback: {onBeforeAnimation: carousel_back}
		});
	});
}

function carousel_back(carousel, state) {
	var sel = null;
	$(".img_selected").each(function(){
		sel = $(this)[0];
	});
	for (var i = carousel.first; i <= carousel.last; i++) {
		if (carousel.has(i)) {
			continue;
		}
		if (i > window.img_list.length) {
			break;
		}
		carousel.add(i, '<img id="thumb_' + i + '" src="' + window.img_list[i-1].thumb + '">');
	}
	$(".jcarousel-item").each( function ( index ) {
		$t = $(this);
		$t.unbind("click", image_click);
		$t.bind("click", image_click);
		if ( sel == null && index == 0 ) {
			$t.addClass("img_selected");
		} else if ( sel == $t[0] ) {
			$t.addClass("img_selected");
		}
	});
	if ( state == "init" ) {
		display_image( 0 );
	}
}

function image_click () {
	var list_items = $(".jcarousel-item");
	list_items.removeClass("img_selected");
	var $t = $(this);
	var index = list_items.index( $t );
	$t.addClass("img_selected");
	display_image( index );
}

window.current_image = -1;
function display_image( img_list_index ) {
	if ( img_list_index == window.current_image ) {
		return;
	}
	window.current_image = img_list_index;
	var cell = $("#big_image")
	var img_data = img_list[img_list_index];
	var src = img_data.full;
	var s = cell.children().size();

	var h = cell.height();
	if ( img_data.w == 800 && h > img_data.h ) {
		h = img_data.h;
	} else if ( img_data.h == 800 && h > 800 ) {
		h = img_data.h;
	}

	if ( cell.children().size() > 0 ) {
		var old = cell.children(":eq(0)");
		old.fadeOut("fast",function () {
			old.remove();
			var nw = $('<img src="' + src + '">' )
				.hide()
				.appendTo(cell)
				.bind("load", function () {
					nw.fadeIn("fast");
				});
			fix_dims();
		});
	} else {
		var nw = $('<img src="' + src + '">' )
			.hide()
			.appendTo(cell)
			.bind("load", function () {
				nw.fadeIn("fast");
			});
		fix_dims();
	}
}

function load_page( page_name, elem ) {
	//$.get( "php/load_page.php", { page: page_name }, function ( data ) {
	$.ajax({ async: false, url: "php/load_page.php", data: { page: page_name }, success: function ( data ) {
		elem.html( data );
		fix_dims();
		if ( page_name == "works" ) {
			$(".proj_thumb").hover( function () {
				$(this).addClass("proj_thumb_hover");
			}, function () {
				$(this).removeClass("proj_thumb_hover");
			}).click( function () {
				//show_project( $(this).find("td:eq(1)").html() );
				show_project( $(this).attr("id").substring(11) );
			});

			$("#page_project_back").hover( function () {
				$(this).css("textDecoration", "none");
			}, function () {
				$(this).css("textDecoration", "underline");
			}).click( function () {
				show_page( "works" );
			});
		} else if ( page_name == "video" ) {
			$(".video_link").hover(function () {
				$(this).addClass("video_hover");
			}, function () {
				$(this).removeClass("video_hover");
			}).click( function () {
				var id_split = this.id.split("_");
				var video_name = "";
				for ( var i = 2; i < id_split.length; i++ ) {
					if ( video_name != "" ) {
						video_name += "_";
					}
					video_name += id_split[i];
				}
				show_video( video_name );
			});
		}
	}});
}

var video_info = [];
video_info["no_manifesto"] = [ 720, 400, 419.221343994 ];
video_info["frenetic_engineering"] = [ 640, 480, 1020.07745361 ];
video_info["just_an_artist"] = [ 624, 480, 1559.29882812 ];
function show_video ( video_name ) {
	$(".video_link").removeClass("video_selected");
	$("#video_link_"+video_name).addClass("video_selected");
	var html;
	if ( video_name == "profile" ) {
		html = '<iframe width="640" height="510" src="http://www.youtube.com/embed/f3fqqeBNgOY" frameborder="0" allowfullscreen></iframe>';
	} else {
		var i = video_info[ video_name ];
		html = '<object width="' + i[0] + '" height="' + (i[1]+24) + '" type="application/x-shockwave-flash" data="jwplayer/player.swf">';
		html += '<param name="movie" value="jwplayer/player.swf" />';
		html += '<param name="flashvars" value="file=/~bert/dev/video/' + video_name + '_qtp.mp4&amp;duration=' + i[2] + '&amp;bufferlength=5&amp;provider=video&amp;image=video/' + video_name + '.jpg" />';
		html += '<img src="video/' + video_name + '.jpg" width="' + i[0] + '" height="' + i[1] + '" title="Sorry, it doesn\'t look like you\'re able to play this video." />';
		html += '</object>';
	}
	$("#page_video_player_cell").html(html);
	document.title = "Umberto Crenca";
}

