

var TOP_COL_NORMAL_WIDTH = 469;
var TOP_COL_FULL_WIDTH = 630;
var TOP_COL_SHRUNK_WIDTH = 308;
var TOP_COL_FULL_HEIGHT = 295;
var TOP_COL_SHRUNK_HEIGHT = 34;
var TOP_COL_CLOSED_HEIGHT = 1;
var TOP_COL_CLOSED_WIDTH = 308;
var COL_MARGIN_OFFSET = -260;
var COL_MARGIN_OFFSET_CLOSED = -301;

var pod_ANIM_SLIDESPEED = 500;

$(function() {

	$(".column").sortable({
		opacity:0.5,
		connectWith: $(".column"),
		handle: ".title",
		placeholder: "content-movable-ph",
		zIndex:99,
		stop: function() {colresize(); storeSortPrefs();},
		appendTo: 'body',
		tolerance: 'intersect',
		revert: true
	});


	

	$('.pod .title').hover(
		function () {
			$(this).addClass('title-over');
			//$(this).css('background-image', $(this).css('background-image').replace('.gif', '-over.gif'));
			//$(this).find('h2').css('background-image', $(this).find('h2').css('background-image').replace('.gif', '-over.gif'));
			//$(this).find('.toggler').css('background-image', $(this).find('.toggler').css('background-image').replace('.gif', '-over.gif'));
		},
		function () {
			$(this).removeClass('title-over');
			//$(this).css('background-image', $(this).css('background-image').replace('-over.gif', '.gif'));
			//$(this).find('h2').css('background-image', $(this).find('h2').css('background-image').replace('-over.gif', '.gif'));
			//$(this).find('.toggler').css('background-image', $(this).find('.toggler').css('background-image').replace('.gif', '-over.gif'));
		});

	$('.columns .pod .toggler').click(function() {
		var $link = $(this);
		var $pod = $link.parent().parent();
		if($pod.hasClass('pod-open')) {
			$pod.find('.content').slideUp(pod_ANIM_SLIDESPEED, function() {
				$(this).parent().removeClass('pod-open').addClass('pod-closed');
				storeSortPrefs();
				//colresize();
			});
		}
		else {
			$pod.find('.content').slideDown(pod_ANIM_SLIDESPEED, function() {
				$(this).parent().removeClass('pod-closed').addClass('pod-open');
				storeSortPrefs();
				//colresize();
			});
		}
	});


	$('.pod .extra-show').click(function() {
		$toggler = $(this);
		$toggler.parent().parent().find('.pod-text-list-item:visible:last').next().slideDown(pod_ANIM_SLIDESPEED, function() {
			$.cookie($toggler.attr('rel'), $toggler.parent().parent().find('.pod-text-list-item:visible').length, { expires: 30 });
		});
	});

	$('.pod .extra-hide').click(function() {
		$toggler = $(this);
		$parent = $toggler.parent().parent();
		if($parent.find('.pod-text-list-item:visible').length > 1) {
			$parent.find('.pod-text-list-item:visible:last').slideUp(pod_ANIM_SLIDESPEED, function() {
				$.cookie($toggler.attr('rel'), $toggler.parent().parent().find('.pod-text-list-item:visible').length, { expires: 30 });
			});
		}
	});
	
	
	
	
	
	


	$('.bigpod .toggler').click(function() {
		var $link = $(this);
		var action = 'auto';

		bigpod_update($link, action);
	});


	// Column height sizer
	function colresize() {
		var maxh = 0;		
		$cols = $('.columns .column');
		$cols.each(function() {
			h = getColHeight($(this)) + asInt($(this).css('margin-top'));
			if(h > maxh) maxh = h;			
		});
		debug(maxh);
		maxh += 30;
		$cols.each(function() {
			//$(this).css('height', maxh - asInt($(this).css('margin-top')));
		});
	}

	function asInt(val) {
		var i = parseInt(val);
		if(isNaN(i)) return 0;
		return i;
	}

	function getColHeight($col) {
		var h = 0;
		$col.children('div').each(function() {
			h += $(this).outerHeight();
		});		
		return h;
	}

	//$(window).resize(colresize);
	//colresize();

});



// Preloader
$(function() {
	var _imgs = [];

	$('h2, .carousel-scroller a, .bigpods div.title:first, .columns div.title:first, .columns div.title a.toggler:first').each(function() {
		var href = $(this).css('background-image');
		if(href) {
			href = href.replace('url(', '').replace(')','').replace('.gif', '-over.gif').replace(/["']/g,'');
			preload(href);		
		}
	});

	function preload(href) {
		var img = new Image();
		img.src = href;
		_imgs.push(img);
		//debug(img);		
	}
	
});


function debug($obj) {
		if (window.console && window.console.log) {
			 window.console.log($obj);
		}
		else {
			//alert($obj);
		}
	}

function storeSortPrefs() {
	var pods = '';
	
	var row = 0;
	var col = 0;

	$('.bigpods .bigpod').each(function() {
		row++;
		if(!$(this).is(':visible')) state = -1;
		else if($(this).hasClass('bigpod-full')) state = 2;
		else if($(this).hasClass('bigpod-closed')) state = 0;
		else state = 1;

		pods += this.id.substr(7) + ',' + col + ',' + row + ',' + state + ';';
	});
	
	for(var col = 1; col<= 3; col++) {
		var row = 0;
		$('#col'+col+' .pod').each(function() {
			row++;
			//pods += this.id.substr(7) + ',' + col + ',' + row + ',' + ($(this).hasClass('pod-open') ? '1' : '0') + ';';
			pods += this.id.substr(7) + ',' + col + ',' + row + ',' + ($(this).is(':visible') ? ($(this).hasClass('pod-open') ? '1' : '0') : '-1') + ';';
		});
	}
	$.cookie('alumasc_home_layout', pods, { expires: 30 });
	debug(pods);
}


	function bigpod_update($link, action) {

		if(!$link) $link = $('.bigpod-left a.toggler');
		if(!action) action = 'open-both';

		$pod1 = $('.bigpod-left');
		$pod2 = $('.bigpod-right');
		$col1 = $('#col1');
		$col2 = $('#col2');
		$col3 = $('#col3');

		if(action == 'auto') {
			if($link.parent().parent().hasClass('bigpod-left')) {
				if($pod1.hasClass('bigpod-open')) {
					action = 'left-min';
				}
				else {
					action = 'normalise';
				}
			}
			else {
				if($pod2.hasClass('bigpod-open')) {
					action = 'right-min';
				}
				else {
					action = 'normalise';
				}			
			}
		}

		debug(action);

		if(action == 'left-off' && $pod2.hasClass('bigpod-off')) {
			action = 'both-off';
		}
		else if(action == 'right-off' && $pod1.hasClass('bigpod-off')) {
			action = 'both-off';
		}
		
		
		

		if(action == 'normalise') {
			$pod1.css('visibility','visible');
			$pod2.css('visibility','visible');
			$pod1.animate({width: TOP_COL_NORMAL_WIDTH, height: TOP_COL_FULL_HEIGHT}, pod_ANIM_SLIDESPEED, null, function() 
				{$(this).removeClass('bigpod-off').removeClass('bigpod-closed').addClass('bigpod-open').removeClass('bigpod-full');});
			$pod2.animate({width: TOP_COL_NORMAL_WIDTH, height: TOP_COL_FULL_HEIGHT}, pod_ANIM_SLIDESPEED, null, function() 
				{$(this).removeClass('bigpod-off').removeClass('bigpod-closed').addClass('bigpod-open').removeClass('bigpod-full');});
			$col1.animate({marginTop: 0}, pod_ANIM_SLIDESPEED);
			$col2.animate({marginTop: 0}, pod_ANIM_SLIDESPEED);
			$col3.animate({marginTop: 0}, pod_ANIM_SLIDESPEED);
		}
		else if(action == 'left-off' || action == 'right-only') {
			$pod2.css('visibility','visible');
			$pod1.animate({width: TOP_COL_CLOSED_WIDTH, height: TOP_COL_CLOSED_HEIGHT}, pod_ANIM_SLIDESPEED, null, function() 
				{$(this).addClass('bigpod-off').removeClass('bigpod-closed').removeClass('bigpod-open').removeClass('bigpod-full');});
			$pod2.animate({width: TOP_COL_FULL_WIDTH, height: TOP_COL_FULL_HEIGHT}, pod_ANIM_SLIDESPEED, null, function() 
				{$(this).removeClass('bigpod-off').removeClass('bigpod-closed').removeClass('bigpod-open').addClass('bigpod-full');});
			$col1.animate({marginTop: COL_MARGIN_OFFSET_CLOSED}, pod_ANIM_SLIDESPEED);
			$col2.animate({marginTop: 0}, pod_ANIM_SLIDESPEED);
			$col3.animate({marginTop: 0}, pod_ANIM_SLIDESPEED);
		}
		else if(action == 'right-off' || action == 'left-only') {
			$pod1.css('visibility','visible');
			$pod1.animate({width: TOP_COL_FULL_WIDTH, height: TOP_COL_FULL_HEIGHT}, pod_ANIM_SLIDESPEED, null, function() 
				{$(this).removeClass('bigpod-off').removeClass('bigpod-closed').removeClass('bigpod-open').addClass('bigpod-full');});
			$pod2.animate({width: TOP_COL_CLOSED_WIDTH, height: TOP_COL_CLOSED_HEIGHT}, pod_ANIM_SLIDESPEED, null, function() 
				{$(this).addClass('bigpod-off').removeClass('bigpod-closed').removeClass('bigpod-open').removeClass('bigpod-full');});
			$col1.animate({marginTop: 0}, pod_ANIM_SLIDESPEED);
			$col2.animate({marginTop: 0}, pod_ANIM_SLIDESPEED);
			$col3.animate({marginTop: COL_MARGIN_OFFSET_CLOSED}, pod_ANIM_SLIDESPEED);
		}
		else if(action == 'both-off') {
			$pod1.animate({width: TOP_COL_CLOSED_WIDTH, height: TOP_COL_CLOSED_HEIGHT}, pod_ANIM_SLIDESPEED, null, function() 
				{$(this).addClass('bigpod-off').removeClass('bigpod-closed').removeClass('bigpod-open').removeClass('bigpod-full');});
			$pod2.animate({width: TOP_COL_CLOSED_WIDTH, height: TOP_COL_CLOSED_HEIGHT}, pod_ANIM_SLIDESPEED, null, function() 
				{$(this).addClass('bigpod-off').removeClass('bigpod-closed').removeClass('bigpod-open').removeClass('bigpod-full');});
			$col1.animate({marginTop: COL_MARGIN_OFFSET_CLOSED}, pod_ANIM_SLIDESPEED);
			$col2.animate({marginTop: COL_MARGIN_OFFSET_CLOSED}, pod_ANIM_SLIDESPEED);
			$col3.animate({marginTop: COL_MARGIN_OFFSET_CLOSED}, pod_ANIM_SLIDESPEED);
		}
		else if(action == 'left-min') {
			$pod1.animate({width: TOP_COL_SHRUNK_WIDTH, height: TOP_COL_SHRUNK_HEIGHT}, pod_ANIM_SLIDESPEED, null, function() 
				{$(this).removeClass('bigpod-off').addClass('bigpod-closed').removeClass('bigpod-open').removeClass('bigpod-full');});
			$pod2.animate({width: TOP_COL_FULL_WIDTH, height: TOP_COL_FULL_HEIGHT}, pod_ANIM_SLIDESPEED, null, function() 
				{$(this).removeClass('bigpod-off').removeClass('bigpod-closed').removeClass('bigpod-open').addClass('bigpod-full');});
			$col1.animate({marginTop: COL_MARGIN_OFFSET}, pod_ANIM_SLIDESPEED);
			$col2.animate({marginTop: 0}, pod_ANIM_SLIDESPEED);
			$col3.animate({marginTop: 0}, pod_ANIM_SLIDESPEED);
		}
		else if(action == 'right-min') {
			$pod1.animate({width: TOP_COL_FULL_WIDTH, height: TOP_COL_FULL_HEIGHT}, pod_ANIM_SLIDESPEED, null, function() 
				{$(this).removeClass('bigpod-off').removeClass('bigpod-closed').removeClass('bigpod-open').addClass('bigpod-full');});
			$pod2.animate({width: TOP_COL_SHRUNK_WIDTH, height: TOP_COL_SHRUNK_HEIGHT}, pod_ANIM_SLIDESPEED, null, function() 
				{$(this).removeClass('bigpod-off').addClass('bigpod-closed').removeClass('bigpod-open').removeClass('bigpod-full');});
			$col1.animate({marginTop: 0}, pod_ANIM_SLIDESPEED);
			$col2.animate({marginTop: 0}, pod_ANIM_SLIDESPEED);
			$col3.animate({marginTop: COL_MARGIN_OFFSET}, pod_ANIM_SLIDESPEED);
		}

		// TODO: not 100% reliable this
		window.setTimeout('storeSortPrefs();', pod_ANIM_SLIDESPEED + 500);


		/*
		if(action == 'full-left') {
			$pod1.animate({width: TOP_COL_FULL_WIDTH, height: TOP_COL_FULL_HEIGHT}, pod_ANIM_SLIDESPEED, null, function() {			
				$(this).removeClass('bigpod-closed').removeClass('bigpod-open').addClass('bigpod-full');
				$(this).find('a.toggler').attr('title', 'Close');
				storeSortPrefs();
			});
			$pod2.animate({width: TOP_COL_SHRUNK_WIDTH, height: TOP_COL_SHRUNK_HEIGHT}, pod_ANIM_SLIDESPEED, null, function() {
				$(this).removeClass('bigpod-open').removeClass('bigpod-full').addClass('bigpod-closed');
				$(this).find('a.toggler').attr('title', 'Open');
				storeSortPrefs();
			});	
			$col1.animate({marginTop: 0}, pod_ANIM_SLIDESPEED);
			$col3.animate({marginTop: COL_MARGIN_OFFSET}, pod_ANIM_SLIDESPEED);
		}
		else if(action == 'close-right') {
			$pod1.animate({width: TOP_COL_FULL_WIDTH}, pod_ANIM_SLIDESPEED, null, function() {			
				//$(this).removeClass('bigpod-closed').removeClass('bigpod-open');
				//$(this).find('a.toggler').attr('title', 'Close');
				storeSortPrefs();
			});
			$pod2.animate({width: TOP_COL_CLOSED_WIDTH, height: TOP_COL_CLOSED_HEIGHT}, pod_ANIM_SLIDESPEED, null, function() {
				$(this).removeClass('bigpod-open').removeClass('bigpod-full').addClass('bigpod-closed');
				$(this).find('a.toggler').attr('title', 'Open');
				storeSortPrefs();
			});	
			$col1.animate({marginTop: 0}, pod_ANIM_SLIDESPEED);
			$col3.animate({marginTop: COL_MARGIN_OFFSET_CLOSED}, pod_ANIM_SLIDESPEED);
		}
		else if(action == 'full-right') {
			$pod2.animate({width: TOP_COL_FULL_WIDTH, height: TOP_COL_FULL_HEIGHT}, pod_ANIM_SLIDESPEED, null, function() {			
				$(this).removeClass('bigpod-closed').removeClass('bigpod-open').addClass('bigpod-full');
				$(this).find('a.toggler').attr('title', 'Open');
				storeSortPrefs();
			});
			$pod1.animate({width: TOP_COL_SHRUNK_WIDTH}, pod_ANIM_SLIDESPEED, null, function() {
				$(this).removeClass('bigpod-open').removeClass('bigpod-full').addClass('bigpod-closed');
				$(this).find('a.toggler').attr('title', 'Open');
				storeSortPrefs();
			});	
			$col3.animate({marginTop: 0}, pod_ANIM_SLIDESPEED);
			$col1.animate({marginTop: COL_MARGIN_OFFSET}, pod_ANIM_SLIDESPEED);
		}
		else if(action == 'close-left') {
			$pod2.animate({width: TOP_COL_FULL_WIDTH, height: TOP_COL_FULL_HEIGHT}, pod_ANIM_SLIDESPEED, null, function() {			
				$(this).removeClass('bigpod-closed').removeClass('bigpod-open').addClass('bigpod-full');
				$(this).find('a.toggler').attr('title', 'Open');
				storeSortPrefs();
			});
			$pod1.animate({width: TOP_COL_CLOSED_WIDTH, height: TOP_COL_CLOSED_HEIGHT}, pod_ANIM_SLIDESPEED, null, function() {
				$(this).removeClass('bigpod-open').removeClass('bigpod-full').addClass('bigpod-off');
				$(this).find('a.toggler').attr('title', 'Open');
				storeSortPrefs();
			});	
			$col3.animate({marginTop: 0}, pod_ANIM_SLIDESPEED);
			$col1.animate({marginTop: COL_MARGIN_OFFSET_CLOSED}, pod_ANIM_SLIDESPEED);
		}
		else { // open-both
			$pod1.removeClass('bigpod-off').animate({width: TOP_COL_NORMAL_WIDTH, height: TOP_COL_FULL_HEIGHT}, pod_ANIM_SLIDESPEED, null, function() {			
				$(this).removeClass('bigpod-closed').removeClass('bigpod-full').addClass('bigpod-open');
				$(this).find('a.toggler').attr('title', 'Open');
				storeSortPrefs();
			});
			$pod2.animate({width: TOP_COL_NORMAL_WIDTH, height: TOP_COL_FULL_HEIGHT}, pod_ANIM_SLIDESPEED, null, function() {
				$(this).removeClass('bigpod-closed').removeClass('bigpod-full').addClass('bigpod-open');
				$(this).find('a.toggler').attr('title', 'Open');
				storeSortPrefs();
			});	
			$col1.animate({marginTop: 0}, pod_ANIM_SLIDESPEED);
			$col2.animate({marginTop: 0}, pod_ANIM_SLIDESPEED);
		}		
		*/
		
	}



function togglePod(sender, podid) {
	debug(sender.checked);

	if(podid == 'pod_id_1') {
		if(sender.checked) {
			if($('.bigpod-right').hasClass('bigpod-off')) {
				action = 'left-only';
			}
			else {
				action = 'normalise';
			}
		}
		else {
			action = 'left-off';
		}
		bigpod_update(null, action);
	}
	else if(podid == 'pod_id_2') {
		if(sender.checked) {
			if($('.bigpod-left').hasClass('bigpod-off')) {
				action = 'right-only';
			}
			else {
				action = 'normalise';
			}
		}
		else {
			action = 'right-off';
		}
		bigpod_update(null, action);
	}
	else {
		$pod = $('#'+podid);

		if($pod.hasClass('pod-off')) {
			$pod.slideDown(function() {
				$pod.removeClass('pod-off').removeClass('bigpod-off').addClass('pod-open');
				storeSortPrefs();
			});
		}
		else {
			$pod.slideUp(function() {
				$pod.addClass('pod-off').addClass('bigpod-off');
				storeSortPrefs();
			});
		}
	}
}



// Personalisation management

$(function() {
	$('.personalise').show(0);
	$('.personalise .personalise-toggle').click(function() {
		$('.personalise-options').slideToggle();
	});
});