var AJAX_SEP = '#!';

$(document).ready(function() {
	hideDisc();
	bindStuff();
	updatePageInfo();
	
	doRedirection();
});

function doRedirection() {
	var index = location.href.indexOf(AJAX_SEP);
	if (index >= 0) {
		location.href = location.href.substring(index + AJAX_SEP.length);
	}
}

function bindStuff() {
	setNavigable();
	setBack();
	setRadio();
	setTips();
	setCart();
}

function setCart() {
	$('form[action=\'/buy.php\']').unbind('submit').submit(function (e) {
		e.preventDefault();
		
		var cover = $(this).parent().children('.cover');
		var cart = $('.nav .cart');
		
		var smallCover = cover.clone();
		smallCover.appendTo($(this).parent());
		smallCover.attr('id', 'smallCover');
		smallCover.attr('rel', $(this).serialize());
		smallCover.css({
			'position' : 'absolute',
			'top' : cover.offset().top,
			'left' : cover.offset().left - 30
		});
		
		var sizeCart = cart.height() / 2;
		var topCart    = cart.offset().top  + cart.height() / 2;
		var leftCart   = cart.offset().left + cart.width()  / 2 - sizeCart / 2;
		
		smallCover.animate({
			opacity : 0.25,
			top : topCart,
			left : leftCart,
			width : sizeCart,
			height : sizeCart
			
		}, 500, function() {
			$.post('buy.php', $(this).attr('rel'), function (data) {
				$('.nav .cart').html(data);
			});
			
			$(this).remove();
		});
	});
}

function setTips() {
	$('.section:last .discs a[href][title]').qtip({
		content : {
			text : false
		},
		style : 'ctc',
		position: {
   target: 'mouse',
   adjust: { mouse: true }
		}
	});

}

function setRadio()  {
	
	$('input[type=radio]').unbind('click').click(function () {
		refreshSelected();
	});
	
	var ra = $('.withRadio').children('p');
	ra.unbind('click').click(function () {
		$(this).parent().children('input[type=radio]').attr('checked', 'checked');
		refreshSelected();
	});
	ra.css('cursor', 'pointer');

}

function marginDisc() {
	return ($('.section:last').width() / 2 - $('.cover').width() / 2) + 'px';
}

function showDisc() {
	$('.disc .cover').unbind('click').css({
		'float' : 'left',
		'margin-right' : '0',
		'margin-left' : marginDisc()
	}).animate({
		'margin-left' : '30px',
	}, 'fast', function () {
		$('.disc p, .disc ul, .disc span').fadeIn('fast');
		$(this).click(function (ev) {
			hideDiscAnimated();
		});
	});
}

function hideDisc() {
	$('.disc p, .disc ul, .disc span').hide();
	
	$('.disc .cover').css({
		'float' : 'none',
		'margin-right' : 'auto',
		'margin-left' : 'auto'
	}).unbind('click').click(function (ev) {
		showDisc();
	});
}

function hideDiscAnimated() {
	$('.disc p, .disc ul, .disc span').fadeOut('fast', function () {
		$('.disc .cover').css('float', 'none').animate({
			'margin-right' : '0',
			'margin-left' : marginDisc()
		}, 'fast', function () {
			hideDisc();
		});
	});
}

function refreshSelected() {
	$('input[type=radio]').each(function () {
		if ($(this).attr('checked')) {
			$(this).parent().children('p').css('background', '#d7e3cc');
			
		} else {
			$(this).parent().children('p').css('background', '#efefef');
		}
	});
}

function setNavigable() {
	$('.navigable').unbind('click').click(function (ev) {
		ev.preventDefault();
		this.blur();
		
		var href = $(this).attr('href');
		
		analytics(href);
		
		if (href != '/') {
			$('.back').fadeIn();
		} else {
			$('.back').fadeOut();
		}
		
		$('.section:last').animate({
			'margin-left' : '-100%'
		}, 'fast', function () {
			loadSection(href);
		});
	})/*.unbind('mouseenter').mouseenter(function () {
		$(this).parent().children('h1:first').html($(this).attr('title'));
	}).unbind('mouseleave').mouseleave(function () {
		$(this).parent().children('h1:first').html('&nbsp;');
	})*/;
}

function loadSection(href) {
	
	if (href == '/') {
		$('.section').remove();
	} else {
		$('.section:last').hide();	
	}

	$('<div class="section"><img class="load" /></div>')
		.insertBefore('.footer');

	$('.section:last').load(href, null, function (data) {
		bindStuff();
		updatePageInfo();
		
		document.location.href = AJAX_SEP + href;
	});
}

function setBack() {
	$('.back').unbind('click').click(function (ev) {
		ev.preventDefault();
		this.blur();
		
		$('.section:last').remove();
		
		var s = $('.section');
		if (s.length == 1) {
			$('.back').fadeOut();	
		}
		
		$('.section:last').show().animate({
			'margin-left' : ($(window).width() * 0.2)
		}, 'fast', function () {
			$(this).css('margin-left', 'auto');
			updatePageInfo();
		});
	});
}

function updatePageInfo() {
	var i = $('.section:last .info');
	
	$('.nav .hover').removeClass('hover');
	$('.nav [rel=' +  i.children('#tab').html() + ']').addClass('hover');
	
	document.title = i.children('#title').html();
}

function analytics(href) {
        pageTracker._trackPageview(href);
}




