function getHashVariables() {
	var hash = new Object();
	var regex = /\#*((?:\\.|[^=,]+)*)=("(?:\\.|[^"\\]+)*"|(?:\\.|[^,"\\]+)*)/g;
	var match;
	while(match = regex.exec(window.location.hash)) {
		hash[match[1]] = match[2];
	}
	return hash;
}

function getHashVariable(key) {
	keyvalue = getHashVariables();
	return keyvalue[key];
}

function setHashVariable(key, value) {
	hash_pairs = new Array();
	keyvalue = getHashVariables();
	keyvalue[key] = value;
	for(var i in keyvalue) {
		hash_pairs.push(i + "=" + keyvalue[i]);
	}
	window.location.hash = hash_pairs.join(",")
}

$(document).ready(function(){
	Cufon.set('fontFamily', 'SAX').replace('h1')('h2')('#payoff')('.nav a', {hover:true});
	/*$.fn.supersized.options = {  
				startwidth: 1404,  
				startheight: 829,
				vertical_center: 1,
				slideshow: 0,
				navigation: 0,
				transition: 0, //0-None, 1-Fade, 2-slide top, 3-slide right, 4-slide bottom, 5-slide left
				pause_hover: 1,
				slide_counter: 1,
				slide_captions: 1,
				slide_interval: 3000  
			};*/
	$('.scaler').cjObjectScaler({
								method: "fill",
								fade: 800
							});//.supersized();
	var resizeTimer;
	$(window).resize(function() {
		clearTimeout(resizeTimer);
		resizeTimer = setTimeout(function() {
			$('.scaler').cjObjectScaler({
										method: "fill",
										fade: 0
									});
		}, 100);
	});
	
	$refcontainers = $(".reflinks");
	//$references = $refcontainers.find("li");
	$ref_info_container = $("#refs");
	$reference_boxes = $ref_info_container.find(".info");
	$pagination = $(".pagination");
	$page_links = $pagination.find(".page-link");
	$reference_boxes.hide();
	$refcontainers.hide();
	
	var current_page = 0
	if(getHashVariable("page"))
		current_page = getHashVariable("page")
	
	var set_current_page = function(index, tabindex) {
		current_reflinks().unbind('tabsshow');
		current_reflinks().tabs("destroy");
		current_reflinks().hide();
		$('.ui-tabs-panel:not(.ui-tabs-hide)').find('.pictures').cycle("destroy");
		
		current_page = index;
		setHashVariable("page", index);
		$reference_boxes.hide();
		
		current_reflinks().bind('tabsshow', function(event, ui) {
			$target = $(event.target);
			setHashVariable("tab", $target.find('.ui-tabs-selected').index());
			$('.ui-tabs-panel:not(.ui-tabs-hide)').find('.pictures').cycle({ 
			    fx:     'fade', 
			    speed:  'slow', 
			    timeout: 0, 
			    next:   '.next-pic', 
			    prev:   '.prev-pic'
			});
			
			$images = $('.ui-tabs-panel:not(.ui-tabs-hide)').find('.pictures img');
			$('.ui-tabs-panel:not(.ui-tabs-hide) img').each(function(i, el) {
				var $el = $(el);
				$el.attr("src", $el.attr("data-image-src"));
				$el.load(function() {
					$el.addClass("loaded");
					$el.css({position: "absolute", marginTop: -($el.height() / 2) + "px", marginLeft: -($el.width() / 2) + "px"});
				})
			});
		});
		
		if(tabindex)
			current_reflinks().tabs({selected: tabindex});
		else
			current_reflinks().tabs();
		current_reflinks().show();
	}
	
	var current_reflinks = function() {
		return $($refcontainers[current_page])
	}
	
	set_current_page(current_page, getHashVariable("tab"))
	
	$page_links.each(function(i, el) {
		$el = $(el);
		$el.click(function(event){
			set_current_page($(event.target).index()-1);
			event.preventDefault();
			return false;
		});
	});
	
	$pagination.find(".prev").click(function(event) {
		if(current_page-1 >= 0)
			set_current_page(current_page-1);
		event.preventDefault();
		return false;
	});
	
	$pagination.find(".next").click(function(event) {
		if(current_page+1 < $page_links.length)
			set_current_page(current_page+1);
		event.preventDefault();
		return false;
	});
	// var tabs_inited = false;
	// 	
	// 	function getTab() {
	// 		if(!tabs_inited){
	// 			tabs_inited = true
	// 			return getHashVariable("tab")
	// 		}
	// 	}
	
	// if($references.length > max_length_without_paginator) {
	// 		console.log("starting paginating")
	// 		$paginator.pagination($references.length, {
	// 				max_length_with_paginator:9,
	// 				current_page: getHashVariable("page"),
	// 				callback:function(new_page_index, pagination_container) {
	// 					console.log("new page index:" + new_page_index)
	// 					setHashVariable("page", new_page_index)
	// 					$('#refs').tabs("destroy");
	// 					$references.remove();
	// 					$reference_boxes.css("display", "none");
	// 					for(var i=max_length_with_paginator*new_page_index;i<max_length_with_paginator*(new_page_index+1);i++) {
	// 						if($references[i]){
	// 							$refcontainer.append($references[i]);
	// 							box_link = $($references[i]).find("a").attr("href");
	// 							$reference_boxes.siblings("#" + box_link.substring(1, box_link.length)).css("display", "");
	// 						}
	// 					}
	// 					$ref_info_container.tabs();
	// 					$ref_info_container.tabs("select", getTab());
	// 					return false;
	// 				}
	// 			});
	// 	} else {
	//}
	
	var $frontpage_content = $(".frontpage#content p");
	var words = $frontpage_content.text().split(" ");
	var currLine = 1;
	var lastLine = 0;
	var $line;
	$frontpage_content.html("")
	for(var i = 0; i < words.length; i++) {
		if(currLine != lastLine) {
			$line = $("<span class=\"line" + currLine + "\"></span>");
			$frontpage_content.append($line)
			lastLine = currLine;
		}
		word = words[i];
		$line.text($line.text() + " " + word);
		if($line.height() != parseInt($line.css("line-height"))) {
			$line.text($line.text().substring(0, $line.text().length - (" " + word).length));
			currLine++;
		}
	}
	
});

