

var TOP_COL_NORMAL_WIDTH = 470;
var TOP_COL_FULL_WIDTH = 624;
var TOP_COL_SHRUNK_WIDTH = 308;
var TOP_COL_FULL_HEIGHT = 295;
var TOP_COL_SHRUNK_HEIGHT = 34;
var COL_MARGIN_OFFSET = -260;

var pod_ANIM_SLIDESPEED = 500;

// Prefs cookie name
var cookiename = 'marshalls_select';
var qs = window.location.search;
if(qs && qs != '') {
	var key = 'specialist=';
	var p = qs.indexOf(key);
	var q = qs.indexOf('&', p + key.length);
	if (q==-1) q = qs.length;
	cookiename += '_' + qs.substring(p + key.length, q).replace(/\+/g, '_');
}


function storeSortPrefs() {
	var pods = '';

	var row = 0;
	var col = 0;

	$('.bigpods .bigpod').each(function() {
		row++;
		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).is(':visible') ? ($(this).hasClass('pod-open') ? '1' : '0') : '-1') + ';';
		});
	}

	debug(pods);
	
	$.cookie(cookiename, pods, { expires: 30 }); 
}


$(function() {

	$(".column").sortable({
		opacity:0.5,
		connectWith: $(".column"),
		handle: ".title",
		placeholder: "content-movable-ph",
		zIndex:99,
		stop: function() {colresize(); storeSortPrefs();},
		appendTo: 'body',
		revert: true,
		scrollSensitivity: 75
	});


	

	$('.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'));
			$(this).css({ cursor:"move" });
		},
		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'));
			$(this).css({ cursor:"normal" });
		});

	$('.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();
			});
		}
		else {
			$pod.find('.content').slideDown(pod_ANIM_SLIDESPEED, function() {
				$(this).parent().removeClass('pod-closed').addClass('pod-open');
				storeSortPrefs();
			});
		}
	});


	$('.pod .extra-show').click(function() {
		$toggler = $(this);
		$parent = $toggler.parent().parent();
		
		$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 });
		});
		$toggler.parent().find('.extra-hide').text('-').show();
	
		if($parent.find('.pod-text-list-item:visible').length == $parent.find('.pod-text-list-item').length) {
			$toggler.text('');
			$toggler.html('<img src="assets/images/spacer_small.gif" alt="spaer" />');
		}
		
	});

	$('.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 });
			});
		} 
		
		$toggler.parent().find('.extra-show').text('+').show();
		
		if($parent.find('.pod-text-list-item:visible').length == 2) {
			$toggler.text('');
			$toggler.html('<img src="assets/images/spacer_small.gif" alt="spaer" />');
		}
		
	});


	$('.bigpod .toggler').click(function() {
		var $link = $(this);
		var $pod1 = $('.bigpod-left');
		var $pod2 = $('.bigpod-right');
		var $col1 = $('#col1');
		var $col2 = $('#col3');
		var action = 'open-both'

		if($link.parent().parent().hasClass('bigpod-left')) {
			if($pod1.hasClass('bigpod-open')) {
				action = 'full-right';
			}
			else {
				action = 'open-both';
			}
		}
		else {
			if($pod2.hasClass('bigpod-open')) {
				action = 'full-left';
			}
			else {
				action = 'open-both';
			}			
		}

		debug(action);
		
		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);
			$col2.animate({marginTop: COL_MARGIN_OFFSET}, 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, 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();
			});	
			$col2.animate({marginTop: 0}, pod_ANIM_SLIDESPEED);
			$col1.animate({marginTop: COL_MARGIN_OFFSET}, pod_ANIM_SLIDESPEED);
		}
		else { // open-both
			$pod1.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);
		}		
		
	});

	// 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,'');
			//if(!href.indexOf('over-over') == -1) {
				preload(href);		
			//}
		}
	});

	function preload(href) {
		var img = new Image();
		img.src = href;
		_imgs.push(img);
		debug(img);		
	}
	
});


function togglePod(podid) {
	
	$pod = $('#'+podid);

	if($pod.hasClass('pod-off')) {
		$pod.slideDown(function() {
			$pod.removeClass('pod-off');
			storeSortPrefs();
		});
	}
	else {
		$pod.slideUp(function() {
			$pod.addClass('pod-off');
			storeSortPrefs();
		});
	}

}

function debug($obj) {
	if (window.console && window.console.log) {
		 window.console.log($obj);
	}
}

