﻿Cufon.replace('h1');

$(document).ready(function() {
	if(screen.width > 1000) {
		var img = $('<img />').attr('src', './img/backgrounds/drummer_highres.jpg');
		img.attr('id', 'background-image');
		img.load(function() {
			$('#background-image').replaceWith($(this));
		});
	}
	
	var pagepos = $('#pageOutOfView').attr('title').split('|');
	if(pagepos[0] == 'reset' && ( $('#page').position().top > $(window).height() || $('#page').position().left > $(window).width())) {
		$('#page').css({top: pagepos[2]+'px', left: pagepos[1]+'px'});
		$.cookie('userposition', pagepos[2]+' '+pagepos[1]+' '+(Math.ceil(new Date().getTime() * 0.001)), {path: '/'});
	}
	else if(pagepos[0] == 'showlabel' && ( $('#page').position().top > $(window).height() || $('#page').position().left > $(window).width())) {
		$('#label').show();
		$('#page').css({top: pagepos[2]+'px', left: pagepos[1]+'px'}).hide();
		$.cookie('userposition', pagepos[2]+' '+pagepos[1]+' '+(Math.ceil(new Date().getTime() * 0.001)), {path: '/'});
	}
	else if(pagepos[0] == false)
		$('html').css({
			overflow: 'auto',
			overflowY: 'scroll'
		});
		
	$('.xtrcontent').height($('#page').outerHeight()-$('#main').outerHeight()+14);
	
	
	$('#pagecloser').click(function(event) {
		event.preventDefault();
		$('#label').toggle();
		$('#page').toggle();
	});
	$('#label').click(function(event) {
		event.preventDefault();
		$('#label').toggle();
		$('#page').toggle();
		$('.xtrcontent').height($('#page').outerHeight()-$('#main').outerHeight()+14);
	});
	
	$('.extern, .follow, .disc').click(function(event) {
		event.preventDefault();
		window.open(this.href);
	});
	
	//ONLY EXECUTE THIS, IF THE COOKIE IS VALID!
	if($.cookie('dragtip') == null) {
		setTimeout(function() {
			$('.topcontent').after($('<div class="dragtop">DRAG ME</div>').hide());
			$('.dragtop').fadeIn(1000, function() {
				var self = this;
				setTimeout(function() {
					$(self).fadeOut(1000, function() {
						$.cookie('dragtip', true, {path: '/'});
					});	
				}, 2000);
			});
		}, 500);
	}
	
	$('#page').draggable({
		handle: '.topcontent, .bottomcontent, .dragtop',
		stop: function() {
			$.cookie('userposition', $(this).position().top+' '+$(this).position().left+' '+(Math.ceil(new Date().getTime() * 0.001)), {path: '/'});
		}
	});
});
