/*
	
	Work section related functions
	
*/

/**
 * Contains the name of the selected tag
 */
var selectedTagName;
/**
 * Tells if the coloured headings will be shown
 */
var showHeadings = true;


function setupAllWork () {
	 var curs = (jQuery.browser.msie) ? 'hand' : 'pointer';
	 var elems = $('li.project-entry div.project-entry-name');
	 elems.css({
	 	'position': 'absolute',
	 	'backgroundColor': '#000000',
	 	'color': '#FFFFFF',
	 	'fontSize' : '12px',
	 	'padding': '5px',
	 	'width': '104px',
	 	'height': '103px',
	 	'opacity': 0,
	 	'cursor': curs
	 });
	 elems.hover(function () {
	 	$(this)
	 		.stop(true, true)
	 		.animate({
	 			'opacity':0.85
	 		}, 500);
	 }, function () {
	 	$(this)
	 		.stop(true, true)
	 		.animate({
	 			'opacity': 0
	 		}, 500);
	 });
	 elems.click(function () {
		var hasTheClass = $(this).hasClass('sorted');
		if (hasTheClass === true) {
			$(this).removeClass('sorted');
			$(this).css('opacity', 0);
		} else {
			window.location = $(this).parent().children('a').attr('href');
		}
	 })
	 elems.css('display', 'inline');
}


function setupTags () {
	var elems = $('li.tag > a');
	elems.mouseover(function () {
		if (selectedTagName == $(this).text()) {
			return false;
		}
		var intendedBgColor = $(this).find('span').css('backgroundColor');
		if (!intendedBgColor) {
			intendedBgColor = '#CCCCCC';
		}
		$(this)
			.stop(true, true)
			.animate({
				'backgroundColor':intendedBgColor,
				'color':'#FFFFFF'
			}, 300);
	});
	elems.mouseout(function () {
		if (selectedTagName == $(this).text()) {
			return false;
		}
		$(this)
			.stop(true, true)
			.animate({
				'backgroundColor':'#FFFFFF',
				'color':'#7D7D7D'
			}, 300);
	});
	elems.click(
		function () {
		var tagTitle = $(this).text();
		if (selectedTagName == tagTitle) {
			selectedTagName = null;
			doTagClear();
		} else {
			selectedTagName = tagTitle;
			$('li.tag > a').mouseout();
			doTagDisplay($(this));
		}
		return false;
	});
}


function setupTagsDetail () {
	var elems = $('li.tag > a,div.tags a.tag');
	elems.mouseover(function () {
		if (selectedTagName == $(this).text()) {
			return false;
		}
		var intendedBgColor = $(this).children('span').css('backgroundColor');
		if (!intendedBgColor) {
			intendedBgColor = '#CCCCCC';
		}
		$(this)
			.stop(true, true)
			.animate({
				'backgroundColor':intendedBgColor,
				'color':'#FFFFFF'
			}, 300);
	});
	elems.mouseout(function () {
		if (selectedTagName == $(this).text()) {
			return false;
		}
		$(this)
			.stop(true, true)
			.animate({
				'backgroundColor':'#FFFFFF',
				'color':'#7D7D7D'
			}, 300);
	});
	elems.click(
		function () {
		window.location = '/work/all#'+encodeURI($(this).text());
		return false;
		});
}


function doTagDisplay (liElement) {
	var tagTitle = liElement.text();
	var tagColor = liElement.children('span').css('backgroundColor');
	var tagClasses = liElement.parent().attr('class');
	var tagClass = tagClasses.split(' ')[1];
	if (!tagTitle || !tagColor || !tagClass) {
		return false;
	}
	if (showHeadings === true) {
		var textAddToH1 = '<span style="backgroundColor:#FFFFFF;color:'+tagColor+';">&nbsp;/'+tagTitle+'</span>';
		$('h1:first').html('All Work');
		$('h1:first').append(textAddToH1);
		$('h1:first > span').hide().fadeIn();
	};
	$('li.project-entry:not(.'+tagClass+') > a > img')
		.stop(true,true)
		.animate({
			'opacity':0.3,
			'borderLeftColor':'#CCCCCC',
			'borderRightColor':'#CCCCCC',
			'borderTopColor':'#CCCCCC',
			'borderBottomColor':'#CCCCCC'
		}, 300);
	$('li.'+tagClass+' > a > img')
		.stop(true,true)
		.animate({
			'opacity':1,
			'borderLeftColor':tagColor,
			'borderRightColor':tagColor,
			'borderTopColor':tagColor,
			'borderBottomColor':tagColor
		}, 300);
	$('li.tag-separator > hr').animate({'borderTopColor':tagColor}, 300);
}


function doTagClear () {
	$('li.project-entry > a > img')
		.stop(true,true)
		.animate({
			'opacity':1,
			'borderBottomWidth':'1px',
			'borderLeftColor':'#CCCCCC',
			'borderRightColor':'#CCCCCC',
			'borderTopColor':'#CCCCCC',
			'borderBottomColor':'#CCCCCC'
		}, 300);
	if (showHeadings === true) {
		$('h1:first').html('All Work');
	}
	$('li.tag-separator > hr').animate({'borderTopColor':'#CCCCCC'}, 300);
}
