var nodes = new Array();

$(document).ready(function(){
	
	document.current_id = '#home';
	
	setup_boxes();
	
	$('.overlay').hover(function(){
		$('.hidden', this).show();
		$('.hide', this).fadeOut('fast');
	}, function(){
		$('.hide', this).fadeIn('fast');
		$('.hidden', this).fadeOut('fast');
	});
	
	$('img.has-alt').each(function(){
		var ele = this;
		var randomnumber=Math.floor(Math.random()*10);
		ele.orig = $(this).attr('src');
		ele.alti = $(this).attr('data-alt-img');
		if (randomnumber > 5) $(ele).attr('src', ele.alti);
	});
	
	$('#nav-selector').hover(function(){
		$('ul', this).removeClass('closed');
	}, function(){
		$('ul', this).addClass('closed');
	});
	$('#nav-selector').click(function(){
		$('ul', this).addClass('closed');		
	});
	
	set_background_for_res();
	
	$(window).resize(set_background_for_res);
	
	$('.has-info').enable_info_box();
	
	$(window).hashchange(function(){
		$('iframe.youtube-player').attr('src', '');
		$('.hidden').hide();
		switch(location.hash)
		{
			case '#!/book':
				go_to_book_info();
				$(document.current_id+' .zoomable').zoom_element(100);
				document.current_id = '#book';
				break;
			case '#!/author':
				go_to_author_info();
				$(document.current_id+' .zoomable').zoom_element(100);				
				document.current_id = '#author';
				break;
			case '#!/community':
				go_to_community();
				$(document.current_id+' .zoomable').zoom_element(100);				
				document.current_id = '#community';
				break;
			case '#!/downloads':
				go_to_downloads();
				$(document.current_id+' .zoomable').zoom_element(100);				
				document.current_id = '#downloads';
				break;
			case '#!/keeper':
				go_to_keeper_level();
				$(document.current_id+' .zoomable').zoom_element(100);
				document.current_id = '#keeper';
				break;
			case '#!/shipper':
				go_to_shipper_level();
				$(document.current_id+' .zoomable').zoom_element(100);
				document.current_id = '#shipper';
				break;
			case '#!/feeder':
				go_to_feeder_level();
				$(document.current_id+' .zoomable').zoom_element(100);
				document.current_id = '#feeder';
				break;				
			default:
				go_to_home();
				$(document.current_id+' .zoomable').zoom_element(100);
				document.current_id = '#home';
				break;
		}
	});	
	
	$(window).hashchange();	
	$('.zoom-frame').enable_pan();
	$('#zoom-selector').make_zoomer();
	bound = true;
	
	$('#book-select #book-1-link').click(function(){
		$('.active-book').removeClass('active-book');
		$(this).parent().addClass('active-book');			
		$("#book-info").addClass("book-1").removeClass("book-2");
		$("#book-purchase").addClass("book-1").removeClass("book-2");
		$("div#book.section .element-box h1").removeClass("book-2");
		$("#book-reviews").addClass("book-1").removeClass("book-2")
		$("#book-preview").addClass("book-1").removeClass("book-2");
	
		
		return false;
	});
	$('#book-select #book-2-link').click(function(){
		$("div#book.section").addClass("book-2");
		$('.active-book').removeClass('active-book');
		$(this).parent().addClass('active-book');
		$("#book-info").addClass("book-2").removeClass("book-1");
		$("#book-purchase").addClass("book-2").removeClass("book-1");
		$("div#book.section .element-box h1").addClass("book-2");
		$(".trailer-headline").css("color","#c3a5d6");
		$("#book-reviews").addClass("book-2").removeClass("book-1");
		$("#book-preview").addClass("book-2").removeClass("book-1");
	
		
		return false;
	});

	
});

$.fn.enable_info_box = function()
{
	return this.each(function(){
		var ele = this;
		ele.infod = $(ele).attr('id')+'-info-text';
		$(ele).add('h1', ele).click(function(){
			$('.has-open-popup').removeClass('has-open-popup');
			$(this).addClass('has-open-popup');
			$(document.current_id+' .overlay:not(.has-open-popup) .label').css('opacity', 0.6);
			$(document.current_id+' ..has-open-popup .label').css('opacity', 1);
			$('.pop-up li.active').removeClass('active');
			$('#'+ele.infod).addClass('active');
			if ($('.label', ele).length > 0) var lbl_position = $('.label', ele).offset();
			var parent = $(ele).parent().offset();
			if ($('.pop-up:visible').length > 0) {
				$('.pop-up').animate({'left':(lbl_position.left-parent.left)+146, 'top':lbl_position.top-parent.top});
			} else {
				$('.pop-up').css({'left':(lbl_position.left-parent.left)+146, 'top':lbl_position.top-parent.top});
			}
			$('.pop-up').fadeIn();
			$('body').click(function(){
				$('.pop-up').hide();
				$('.has-open-popup').removeClass('has-open-popup');
				$(document.current_id+' .label').css('opacity', 1);				
				$('body').unbind('click');
				return false;
			});
			return false;
		});
	});
};

function set_background_for_res()
{
	var w_width = $(window).width();
	var w_height = $(window).height();
	var d_width = $(document).width();
	var d_height = $(document).height();
	
	if (w_width > d_width) width = w_width;
	else width = d_width;
	
	if (w_height > d_height) height = w_height;
	else height = d_height;

	if (width < 1250 && height < 833) var back = 'assets/img/backgrounds/2.jpg';
	else if (width < 1250 && height < 833) var back = 'assets/img/backgrounds/3.jpg';
	else if (width < 1500 && height < 999) var back = 'assets/img/backgrounds/4.jpg';
	else if (width < 1750 && height < 1166) var back = 'assets/img/backgrounds/5.jpg';
	else if (width < 2000 && height < 1333) var back = 'assets/img/backgrounds/6.jpg';
	else if (width < 2250 && height < 1500) var back = 'assets/img/backgrounds/7.jpg';
	else if (width < 2500 && height < 1667) var back = 'assets/img/backgrounds/8.jpg';
	else var back = 'assets/img/backgrounds/9.jpg';

	$('html').css('background-image', 'url('+back+')');
	
}

function setup_boxes()
{
	$('.element-box ol').enable_list_page();
	$('.element-box:not(.wrapped)').wrapInner("<div class='box-inner-top-left'><div class='box-inner-top-right'><div class='box-inner-bottom-left'><div class='box-inner-bottom-right'><div class='box-inner'></div></div></div></div></div>");	
	$('.element-box').addClass('wrapped');	
}

function go_to_home()
{
	if (document.current_id == 'home') return false;
	$('#nav-selector li.active').removeClass('active');
	$('#nav-selector li.home').addClass('active');
	$('#ship-nav').hide();
	$(document.current_id).fadeOut('fast', function(){
		$('.section.active').removeClass('active');		
		$('#home').fadeIn('fast', function(){
			$('#home').addClass('active');
			$('#zoom-selector').reset_zoomer();
			$('#home .zoomable').add('.zoomed').make_zoomable();			
		});
	});	
}


function go_to_keeper_level()
{
	if (document.current_id == 'keeper') return false;
	$('#ship-nav li.active').removeClass('active');
	$('#ship-nav li.keeper').addClass('active');
	$('#nav-selector li.active').removeClass('active');
	$('#nav-selector li.home').addClass('active');
	$('#ship-nav').fadeIn('fast');
	$(document.current_id).fadeOut('fast', function(){
		$('.section.active').removeClass('active');
		$('#keeper').fadeIn('fast', function(){
			$('#keeper').addClass('active');
			$('#zoom-selector').reset_zoomer();
			$('#keeper .zoomable').add('.zoomed').make_zoomable();
		});
	});
}

function go_to_shipper_level()
{
	if (document.current_id == 'shipper') return false;
	$('#nav-selector li.active').removeClass('active');
	$('#nav-selector li.home').addClass('active');
	$('#ship-nav li.active').removeClass('active');
	$('#ship-nav li.shipper').addClass('active');	
	$('#ship-nav').fadeIn('fast');
	$(document.current_id).fadeOut('fast', function(){
		$('.section.active').removeClass('active');
		$('#shipper').fadeIn('fast', function(){
			$('#shipper').addClass('active');
			$('#zoom-selector').reset_zoomer();
			$('#shipper .zoomable').add('.zoomed').make_zoomable();
		});
	});	
}

function go_to_feeder_level()
{
	if (document.current_id == 'feeder') return false;
	$('#nav-selector li.active').removeClass('active');
	$('#nav-selector li.home').addClass('active');
	$('#ship-nav li.active').removeClass('active');
	$('#ship-nav li.feeder').addClass('active');	
	$('#ship-nav').fadeIn('fast');
	$(document.current_id).fadeOut('fast', function(){
		$('.section.active').removeClass('active');
		$('#feeder').fadeIn('fast', function(){
			$('#feeder').addClass('active');
			$('#zoom-selector').reset_zoomer();
			$('#feeder .zoomable').add('#feeder .zoomed').make_zoomable();	
		});
	});	
}


function go_to_book_info()
{
	$('#nav-selector li.active').removeClass('active');
	$('#nav-selector li.book').addClass('active');
	//x	$('.uk').hide();
	$('#country-select li.us a').click(function(){
		$('.active-country').removeClass('active-country');
		$(this).parent().addClass('active-country');			
		$("#book-info").removeClass("uk").addClass("us");
		$("#book-purchase").removeClass("uk").addClass("us");
		return false;
	});
	$('#country-select li.uk a').click(function(){
		$('.active-country').removeClass('active-country');
		$(this).parent().addClass('active-country');
		$("#book-info").removeClass("us").addClass("uk");
		$("#book-purchase").removeClass("us").addClass("uk");
		return false;
	});	
	

	
	$('#ship-nav').hide();	
	$(document.current_id).fadeOut('fast', function(){
		$('.section.active').removeClass('active');		
		$('#book').fadeIn('fast', function(){
			$('#book').addClass('active');
			$('#zoom-selector').disable_zoomer();
			$('#book iframe').replaceWith('<iframe title="Book Trailer" class="youtube-player" type="text/html" width="631" height="381" src="http://www.youtube.com/embed/vVEaYz4-LdE" frameborder="0"></iframe>');
		});
	});
	

}

function go_to_author_info()
{
	$('#nav-selector li.active').removeClass('active');
	$('#nav-selector li.author').addClass('active');
	$('#ship-nav').hide();
	$(document.current_id).fadeOut('fast', function(){
		$('.section.active').removeClass('active');		
		$('#author').fadeIn('fast', function(){
			$('#author').addClass('active');
			$('#zoom-selector').disable_zoomer();
			$('#author iframe').replaceWith('<iframe title="Book Trailer" class="youtube-player" type="text/html" width="631" height="381" src="http://www.youtube.com/embed/l3312f9wW8M" frameborder="0"></iframe>');
		});
	});
}

function go_to_community()
{
	$('#nav-selector li.active').removeClass('active');
	$('#nav-selector li.community').addClass('active');
	$('#ship-nav').hide();	
	$(document.current_id).fadeOut('fast', function(){
		$('.section.active').removeClass('active');		
		$('#community').fadeIn('fast', function(){
			$('#community').addClass('active');
			$('#zoom-selector').disable_zoomer();
		});
	});	
}

function go_to_downloads()
{
	$('#nav-selector li.active').removeClass('active');
	$('#nav-selector li.downloads').addClass('active');
	$('#ship-nav').hide();		
	$(document.current_id).fadeOut('fast', function(){
		$('.section.active').removeClass('active');		
		$('#downloads').show();
		$('#downloads').fadeIn('fast', function(){
			$('#downloads').addClass('active');
			$('#zoom-selector').disable_zoomer();
		});
	});	
}

$.fn.enable_list_page = function()
{
	return this.each(function(){
		var list = this;
		list.nav = $('ul', $(this).parent());
		list.count = 0;
		list.total = $('li', list).length;
		$('li.next a', list.nav).click(function(){
			list.count++;
			if (list.count >= list.total) list.count = 0;
			$('li', list).removeClass('active');
			$('li:eq('+list.count+')', list).addClass('active');
			return false;
		});
		$('li.previous a', list.nav).click(function(){
			list.count--;
			if (list.count < 0) list.count = list.total-1;
			$('li', list).removeClass('active');
			$('li:eq('+list.count+')', list).addClass('active');
			return false;
		});		
	});
}

$.fn.make_zoomable = function()
{
	return this.each(function(){
		var ele = this;
		if (this.original_height !== undefined) return;
		this.original_height = this.last_height = $(this).height();
		this.original_width = this.last_width = $(this).width();
		this.original_offset_left = $(this).position().left;
		this.original_offset_top = $(this).position().top;
		this.center_x = this.original_width/2;
		this.center_y = this.original_height/2;
	});
};

$.fn.disable_zoomer = function()
{
	return this.each(function(){
		$(this).fadeTo('fast', 0.2);
		var dragbar = $('#drag-bar');
		$(dragbar).unbind('mousedown');
		$(dragbar).animate({'left':93});
		$(dragbar).mousedown(function(){
			return false;
		});
	});
};

$.fn.reset_zoomer = function()
{
	return this.each(function(){
		var dragbar = $('#drag-bar').get(0);
		$(dragbar).mousedown(dragbar.slide);		
		$(dragbar).animate({'left':93});
		$('.zoomable').each(function(){
			$(this).hide();
			$(this).css({'margin-left':this.original_margin_left, 'margin-top':this.original_margin_top});
			$(this).show();
		});
		$(this).fadeTo('fast', 1);
	});	
};

$.fn.enable_zoomer = function()
{
	return this.each(function(){
		var dragbar = $('#drag-bar').get(0);
		$(dragbar).mousedown(dragbar.slide);
		$(this).fadeTo('fast', 1);
	});
};

$.fn.make_zoomer = function()
{
	return this.each(function(){
		
		var zoomer = this;
		var dragbar = $('#drag-bar').get(0);

		dragbar.min = 93;
		dragbar.max = 281;
		dragbar.per = (dragbar.max-dragbar.min)/100;
		dragbar.posi = $(dragbar).offset();
		dragbar.last = parseInt(dragbar.posi.left);
		dragbar.difference = 0;
		
		dragbar.slide = function(e)
		{
			
			if ($('.pop-up:visible').length > 0) $('.pop-up').hide();
			dragbar.mseX = parseInt(e.layerX);

			$('body').mousemove(function(e){
				
				dragbar.difference = parseInt(dragbar.min) + (parseInt(e.pageX) - parseInt(dragbar.last));
				var pixel_diff = (parseInt(e.pageX) - parseInt(dragbar.last)) - parseInt(dragbar.mseX);
				
				if (pixel_diff < 0) pixel_diff = 0;
				if (pixel_diff > 100) pixel_diff = 100;
				
				if (dragbar.difference < dragbar.min) dragbar.difference = dragbar.min;
				if (dragbar.difference > dragbar.max) dragbar.difference = dragbar.max;
				
				$(dragbar).css('left', dragbar.difference+'px');
				$(document.current_id+' .zoomable').zoom_element((dragbar.per*(dragbar.difference-dragbar.min))+100);
				
				return false;
			});
			
			$('body').mouseup(function(){
				$('.zoomable').each(function(){ 
					this.last_width = this.new_width; 
					this.last_height = this.new_height; 
					this.margin_left = parseInt($(this).css('margin-left')); 
					this.margin_top = parseInt($(this).css('margin-top'));      
				});
				$('body').unbind('mousemove');
				$('body').unbind('mouseup');
			});
			
			return false;
		};
		
		$(dragbar).mousedown(dragbar.slide);
		
	});
};

$.fn.enable_pan = function()
{
	return this.each(function(){
		var ele = this;
		
		ele.margin_left = ele.original_margin_left = parseInt($(ele).css('margin-left'));
		ele.margin_top = ele.original_margin_top = parseInt($(ele).css('margin-top'));

		$(ele).mousedown(function(e){
					
			ele.margin_left = parseInt($(ele).css('margin-left'));
			ele.margin_top = parseInt($(ele).css('margin-top'));
					
			ele.msex = e.pageX;
			ele.msey = e.pageY;

			$('body').mousemove(function(e){
				var differencex = (e.pageX-ele.msex)+ele.margin_left;
				var differencey = (e.pageY-ele.msey)+ele.margin_top;
				$(ele).css({'margin-left':differencex+'px', 'margin-top':differencey+'px'});
				return false;
			});
			$('body').mouseup(function(){
				ele.margin_left = parseInt($(ele).css('margin-left'));
				ele.margin_top = parseInt($(ele).css('margin-top'));
				$('body').unbind('mousemove');
				$('body').unbind('mouseup');
			});
			return false;
		});
	});
};

$.fn.zoom_element = function(zoom_perc)
{
	return this.each(function(){
		var ele = this;

		zoom_perc = zoom_perc/100;
		ele.y_center = $(ele).height()/2;
		ele.x_center = $(ele).width()/2;
				
		ele.max_y = 0;
		ele.max_x = 0;

		ele.new_width = ele.original_width*zoom_perc;
		ele.new_height = ele.original_height*zoom_perc;

		ele.new_margin_left = ele.margin_left + ((ele.last_width-ele.new_width)/2);
		ele.new_margin_top = ele.margin_top + ((ele.last_height-ele.new_height)/2);
		
		$(ele).css({'width':ele.new_width, 'height':ele.new_height, 'margin-left':ele.new_margin_left, 'margin-top':ele.new_margin_top});

		$('.zoomed', ele).each(function(){
			var new_height = this.original_height*zoom_perc;
			var new_width = this.original_width*zoom_perc;
			var new_left = this.original_offset_left*zoom_perc;
			var new_top = this.original_offset_top*zoom_perc;

			if (new_top+new_height > ele.max_y) ele.max_y = new_top+new_height;
			if (new_left+new_width > ele.max_x) ele.max_y = new_top+new_height;

			$(this).css({'height':new_height, 'width':new_width, 'left':new_left+'px', 'top':new_top+'px'});
			$('img', this).css({'height':new_height, 'width':new_width});
		});
		
		
	});
};
