if (!console) {
	var console = {
		log: function(o){ return; }
	};
}

/**
 * handle mouseover, scrolling and display
 */
(function() {
	
	var app = {
		
		sections: [],
		id: 0,
		
		/**
		 * set up the scroll behaviour and register triggers
		 */
		initScroller: function() {
			
			var $i = 0;
			var $tc = $('#thumb-content'); 
			var $conf = { speed: 1000, axis:'y' };
			
			$('#up').click(function() {
				$tc.stop().scrollTo(getscroll('up'), $conf); return false;
			});		
			
			$('#down').click(function() {
				$tc.stop().scrollTo(getscroll('down'), $conf);return false;
			});
			
			var getscroll = function(dir) {
				if (dir == 'up' && $i > 0)
					$i--;
				else if (dir == 'down' && ($i < (app.sections.length-1)))
					$i++;
				return '#section' + $i;
			}
		},
		
		/**
		 * parse press-items and order into sections
		 */
		parseSections: function() {
			
			var a = app.sections;
			var c = 0;
			var s = app.createSection();
			$('.press-item').each(function() {
				s.appendChild(this);
				c++;
				if (c == 12) {
					a.push(s);
					c = 0;
					s = app.createSection();
				}
			});
			a.push(s);
			for(var i=0; i<a.length; i++)
				$('#thumb-content').append(a[i]);
		},
		
		/**
		 * create div container, represents a section
		 * usedby scrollTo jQuery plugin.
		 */
		createSection: function() {
			var s = document.createElement('div');
			s.id = 'section'+app.id++;
			s.className = 'clearfix';
			return s;
		},
		
		/**
		 * apply behaviour to press-item, hover, click magic
		 */
		parseThumbs: function() {
			
			var count = 0;
		
			$('.press-item').each(function() {
				$item = $(this);
				$item.pressimage = $item.find('span:eq(0)').text();
				
				// handle mouseover
				$item.find('img').each(function() {
					var image = this;
					image.overlay = image.cloneNode(true);
					image.overlay.imageLocation = $(image).parent().parent().find('span').text();
					image.overlay.pdflink = '<a href="'+$(this).parent().parent().find('a.dlink').attr('href')+'" target="_blank"><img src="/img/download.gif" alt="Download Press PDF"/></a>'
					
					if (count == 0) {
						$('#image-view').html('<img src="'+image.overlay.imageLocation+'" alt=""/>');
						count++;
					}
						
					$(image.overlay).css({position: 'absolute', 'z-index': 4000});
					$(image).mouseover(function() {
						var me = this;
						var fn = function() {
							var p = $(me).offset();
							document.body.appendChild(me.overlay);
							$(me.overlay).stop().css({ 'top': p.top, 'left': p.left })
								.animate({
									top: '-=2px',
									left: '-=2px',
									width: '+=6px', 
									height: '+=6px'
									}, 150, 'swing');	
						}
						// set timeout for fast hovers
						image.timeout = setTimeout(fn, 100);
					});
					
					$(image).mouseout(function() {
						// don't call hover action
						// assume image was hovered "accidentally"
						clearTimeout(this.timeout);
					});
				
					$(image.overlay).mouseout(function() {
						$(this).css({ 'top':0,'left':0,'width':image.width,'height':image.height });
						document.body.removeChild(this);
					});
					
					$(image.overlay).click(function() {
						$('#download').html(this.pdflink);
						$('#image-view').html('<img src="'+this.imageLocation+'" alt=""/>');
						return false;
					});
				});
			});
		}
	};
	
	$(window).load(function() {
		
		app.parseSections();
		app.initScroller();
		app.parseThumbs();
		
	});
})();
