/**
 * Javascripts for interactivity on the press page.
 * Note: This script requires the presence of jquery prior to execution
 * @copyright 2009 Agnetha Sjögren
 */


/**
 * Container object for the press page's behaviors
 * @var Object
 */
var Press = {
	/**
	 * The opacity of elements while they are being hidden
	 * @var Double
	 */
	_hideOpacity : 0.4,
	/**
	 * Returns the press class name of the passed entry
	 * Press class names appear like 'press-[somename]'
	 * @param jQuery
	 * @return String
	 */
	getPressClass : function (element) {
		/* Get all the classes involved and make an expression to check them */
		var classes_string = element.attr('class'),
			classes = classes_string.split(' '),
			class_check = new RegExp("^press-[a-zA-Z0-9\-]{3,}$");
		/* Loop through all the set classes */
		for (var inc = 0; inc < classes.length; inc++) {
			var the_class = classes[inc];
			if (class_check.test(the_class) == true) {
				return the_class;
			}
		}
		/* Return nothing to be sure some value is returned */
		return '';
	},
	/**
	 * Runs when one of the gallery images is clicked
	 * @param Object
	 */
	onGalleryItemClick : function (event) {
		
	},
	/**
	 * Runs when one of the gallery images is mouseover'd
	 * @param Object
	 */
	onGalleryItemOver : function (event) {
		/* Find which class is affected */
		var press_class = Press.getPressClass($(this));
		/* Do effects on the thumbnails */
		$('td:not(.' + press_class + '):not(.column-left)')
			.stop(true)
			.animate({
				'opacity' : Press._hideOpacity
			}, 600);
		$('td.' + press_class).addClass('item-highlight');
		$(this).addClass('item-hover');
	},
	/**
	 * Runs when one of the mouse leaves one of the gallery images
	 * @param Object
	 */
	onGalleryItemOut : function (event) {
		/* Find which class is affected */
		var press_class = Press.getPressClass($(this));
		/* Do effects on the thumbnails */
		$('td:not(.' + press_class + '):not(.column-left)')
		.stop(true)
		.animate({
			'opacity' : 1
		}, 600);
		$('td.' + press_class).removeClass('item-highlight');
		$(this).removeClass('item-hover');
	},
	/**
	 * Runs when one of the list entries is clicked
	 * @param Object
	 */
	onListItemClick : function (event) {
		/* Find which class is affected */
		var press_class = Press.getPressClass($(this));
		/* Run the click method on the gallery item */
		$('div#press-gallery div.gallery-line a.' + press_class).click();
	},
	/**
	 * Runs when one of the list items is mouseover'd
	 * @param Object
	 */
	onListItemOver : function (event) {
		/* Find which class is affected */
		var press_class = Press.getPressClass($(this));
		/* Do effects on the thumbnails */
		$('div#press-gallery div.gallery-line a:not(.' + press_class + ') img')
			.stop(true)
			.animate({
				'opacity' : Press._hideOpacity
			}, 600);
		$('div#press-gallery div.gallery-line a.' + press_class).addClass('item-highlight');
		$(this).addClass('item-hover');
	},
	/**
	 * Runs when the mouse leaves one of the list items
	 * @param Object
	 */
	onListItemOut : function (event) {
		/* Find which class is affected */
		var press_class = Press.getPressClass($(this));
		/* Do effects on the thumbnails */
		$('div#press-gallery div.gallery-line a:not(.' + press_class + ') img')
			.stop(true)
			.animate({
				'opacity' : 1
			}, 600);
		$('div#press-gallery div.gallery-line a.' + press_class).removeClass('item-highlight');
		$(this).removeClass('item-hover');
	},
	/**
	 * Method to attach listeners to DOM elements and set everything else up.
	 */
	setup : function () {
		/* Firtly, hook the list items */
		$('td.list-item')
			.css({
				'cursor' : 'hand',
				'cursor' : 'pointer'
			})
			.hover(Press.onListItemOver, Press.onListItemOut)
			.click(Press.onListItemClick);
		/* Secondly, hook the gallery items */
		$('div#press-gallery div.gallery-line a')
			.hover(Press.onGalleryItemOver, Press.onGalleryItemOut)
			.click(Press.onGalleryItemClick)
			.fancybox({
				'padding' : 0,
				'imageScale' : true,
				'overlayShow' : true,
				'overlayOpacity' : 0.5,
				'hideOnContentClick' : true,
				'zoomSpeedIn' : 500,
				'zoomSpeedOut' : 500
			});
	}
}


/* Listen for page load */
if (!$) {
	throw('press.js; Jquery could not be found.');
};
$(function () {
	Press.setup();
});

