/* ------------------------------------------------------------------------
	Class: prettyPhoto
	Use: Lightbox clone for jQuery
	Author: Stephane Caron (http://www.no-margin-for-errors.com)
	Version: 3.1.3
------------------------------------------------------------------------- */
(function($) {
	$.prettyPhoto = {version: '3.1.3'};
	
	$.fn.prettyPhoto = function(pp_settings) {
		pp_settings = jQuery.extend({
			animation_speed: 'fast', /* fast/slow/normal */
			opacity: 0.80, /* Value between 0 and 1 */
			default_width: 843,
			default_height: 333,
			horizontal_padding: 15, /* The padding on each side of the picture */
			markup: '<div class="pp_pic_holder"> \
						<div class="pp_content_container"> \
							<div class="pp_content"> \
								<div class="pp_loaderIcon"></div> \
								<div class="pp_fade"> \
									<div id="pp_full_res"></div> \
									<a class="pp_close" href="#">Close</a> \
									<div class="pp_details"></div> \
								</div> \
							</div> \
						</div> \
					</div> \
					<div class="pp_overlay"></div>',
			inline_markup: '<div class="pp_inline">{content}</div>'
		}, pp_settings);
		
		// Global variables accessible only by prettyPhoto
		var matchedObjects = this, percentBased = false, pp_dimensions, pp_open,
		
		// prettyPhoto container specific
		pp_contentHeight, pp_contentWidth, pp_containerHeight, pp_containerWidth,
		
		// Window size
		windowHeight = $(window).height(), windowWidth = $(window).width(),
		
		doresize = true, scroll_pos = _get_scroll();
	
		// Window/Keyboard events
		$(window).unbind('resize.prettyphoto').bind('resize.prettyphoto',function(){ _center_overlay(); _resize_overlay(); });
		
		/**
		* Initialize prettyPhoto.
		*/
		$.prettyPhoto.initialize = function() {
			settings = pp_settings;
			theRel = $(this).attr('rel');
			galleryRegExp = /\[(?:.*)\]/;
			isSet = (galleryRegExp.exec(theRel)) ? true : false;
			
			pp_images = (isSet) ? jQuery.map(matchedObjects, function(n, i){ if($(n).attr('rel').indexOf(theRel) != -1) return $(n).attr('href'); }) : $.makeArray($(this).attr('href'));
			set_position = jQuery.inArray($(this).attr('href'), pp_images);
			rel_index = (isSet) ? set_position : $("a[rel^='"+theRel+"']").index($(this));
			
			_build_overlay(this);
			
			$(window).bind('scroll.prettyphoto',function(){ _center_overlay(); });
			$.prettyPhoto.open();
			
			return false;
		}

		/**
		* Opens the prettyPhoto modal box.
		* @param image {String,Array} Full path to the image to be open, can also be an array containing full images paths.
		* @param title {String,Array} The title to be displayed with the picture, can also be an array containing all the titles.
		* @param description {String,Array} The description to be displayed with the picture, can also be an array containing all the descriptions.
		*/
		$.prettyPhoto.open = function(event) {
			if($.browser.msie && $.browser.version == 6) $('select').css('visibility','hidden');
			$('object,embed,iframe[src*=youtube],iframe[src*=vimeo]').css('visibility','hidden');

			$('.pp_loaderIcon').show();
			$pp_overlay.show().fadeTo(settings.animation_speed,settings.opacity);
			
			percentBased=false;
			
			$pp_pic_holder.fadeIn(function(){
				imgPreloader = "";
				skipInjection = false;
				
				myClone = $(pp_images[set_position]).clone().append('<br clear="all" />').css({'width':settings.default_width}).wrapInner('<div id="pp_full_res"><div class="pp_inline"></div></div>').appendTo($('body')).show();
				doresize = false;
				pp_dimensions = _fitToViewport($(myClone).width(),$(myClone).height());
				doresize = true;
				$(myClone).remove();
				toInject = settings.inline_markup.replace(/{content}/g,$(pp_images[set_position]).html());

				if(!imgPreloader && !skipInjection){
					$pp_pic_holder.find('#pp_full_res')[0].innerHTML = toInject;
					_showContent();
				};
			});
			return false;
		};

		/**
		* Closes prettyPhoto.
		*/
		$.prettyPhoto.close = function(){
			if($pp_overlay.is(":animated")) return;
			$pp_pic_holder.stop().find('object,embed').css('visibility','hidden');
			$('div.pp_pic_holder,.pp_fade').fadeOut(settings.animation_speed,function(){ $(this).remove(); });
			
			$pp_overlay.fadeOut(settings.animation_speed, function(){
				if($.browser.msie && $.browser.version == 6) $('select').css('visibility','visible');
				$('object,embed,iframe[src*=youtube],iframe[src*=vimeo]').css('visibility','visible');
				
				$(this).remove();
				$(window).unbind('scroll.prettyphoto');
				doresize = true;
				pp_open = false;
				delete settings;
			});
		};
	
		/**
		* Set the proper sizes on the containers and animate the content in.
		*/
		function _showContent(){
			$('.pp_loaderIcon').hide();

			projectedTop = scroll_pos['scrollTop'] + ((windowHeight/2) - (pp_dimensions['containerHeight']/2));
			if(projectedTop < 0) projectedTop = 0;

			$pp_pic_holder.find('.pp_content')
				.animate({
					height:pp_dimensions['contentHeight'],
					width:pp_dimensions['contentWidth']
				},settings.animation_speed);
			
			if (jQuery.browser.msie) {
			projectedTop = projectedTop-settings.default_height/2;
			}
			
			$pp_pic_holder.animate({
				'top': projectedTop,
				'left': (windowWidth/2) - (pp_dimensions['containerWidth']/2),
				width:pp_dimensions['containerWidth']
			},settings.animation_speed,function(){
				$pp_pic_holder.find('.pp_fade').fadeIn(settings.animation_speed);
				pp_open = true;
			});
		};
		
		/**
		* Resize the item dimensions if it's bigger than the viewport
		* @param width {integer} Width of the item to be opened
		* @param height {integer} Height of the item to be opened
		* @return An array containin the "fitted" dimensions
		*/
		function _fitToViewport(width,height){
			resized = false;

			_getDimensions(width,height);
			
			// Define them in case there's no resize needed
			imageWidth = width, imageHeight = height;

			if( ((pp_containerWidth > windowWidth) || (pp_containerHeight > windowHeight)) && doresize && !percentBased) {
				resized = true, fitting = false;
			
				while (!fitting){
					if((pp_containerWidth > windowWidth)){
						imageWidth = (windowWidth - 200);
						imageHeight = (height/width) * imageWidth;
					}else if((pp_containerHeight > windowHeight)){
						imageHeight = (windowHeight - 200);
						imageWidth = (width/height) * imageHeight;
					}else{
						fitting = true;
					};

					pp_containerHeight = imageHeight, pp_containerWidth = imageWidth;
				};
			
				_getDimensions(imageWidth,imageHeight);
				
				if((pp_containerWidth > windowWidth) || (pp_containerHeight > windowHeight)){
					_fitToViewport(pp_containerWidth,pp_containerHeight)
				};
			};
			
			return {
				width:Math.floor(imageWidth),
				height:Math.floor(imageHeight),
				containerHeight:Math.floor(pp_containerHeight),
				containerWidth:Math.floor(pp_containerWidth) + (settings.horizontal_padding * 2),
				contentHeight:Math.floor(pp_contentHeight),
				contentWidth:Math.floor(pp_contentWidth),
				resized:resized
			};
		};
		
		/**
		* Get the containers dimensions according to the item size
		* @param width {integer} Width of the item to be opened
		* @param height {integer} Height of the item to be opened
		*/
		function _getDimensions(width,height){
			width = parseFloat(width);
			height = parseFloat(height);
			
			// Get the details height, to do so, I need to clone it since it's invisible
			$pp_details = $pp_pic_holder.find('.pp_details');
			$pp_details.width(width);
			detailsHeight = parseFloat($pp_details.css('marginTop')) + parseFloat($pp_details.css('marginBottom'));
			
			$pp_details = $pp_details.clone().width(width).appendTo($('body')).css({
				'position':'absolute',
				'top':-10000
			});
			detailsHeight += $pp_details.height();
			//detailsHeight = (detailsHeight <= 34) ? 36 : detailsHeight; // Min-height for the details
			if($.browser.msie && $.browser.version==7) detailsHeight+=8;
			$pp_details.remove();
			
			// Get the container size, to resize the holder to the right dimensions
			pp_contentHeight = height + detailsHeight;
			pp_contentWidth = width;
			pp_containerHeight = pp_contentHeight;
			pp_containerWidth = width;
		}
	
		function _center_overlay(){
			if(doresize && typeof $pp_pic_holder != 'undefined') {
				scroll_pos = _get_scroll();
				contentHeight = $pp_pic_holder.height(), contentwidth = $pp_pic_holder.width();
				projectedTop = (windowHeight/2) + scroll_pos['scrollTop'] - (contentHeight/2);
				if(projectedTop < 0) projectedTop = 0;
				
				if(contentHeight > windowHeight)
					return;

				$pp_pic_holder.css({
					'top': projectedTop,
					'left': (windowWidth/2) + scroll_pos['scrollLeft'] - (contentwidth/2)
				});
			};
		};
	
		function _get_scroll(){
			if (self.pageYOffset) {
				return {scrollTop:self.pageYOffset,scrollLeft:self.pageXOffset};
			} else if (document.documentElement && document.documentElement.scrollTop) { // Explorer 6 Strict
				return {scrollTop:document.documentElement.scrollTop,scrollLeft:document.documentElement.scrollLeft};
			} else if (document.body) {// all other Explorers
				return {scrollTop:document.body.scrollTop,scrollLeft:document.body.scrollLeft};
			};
		};
	
		function _resize_overlay() {
			windowHeight = $(window).height(), windowWidth = $(window).width();
			if(typeof $pp_overlay != "undefined") $pp_overlay.height($(document).height()).width(windowWidth);
		};
	
		function _build_overlay(caller){
			$('body').append(settings.markup);
			$pp_pic_holder = $('.pp_pic_holder'), $pp_overlay = $('div.pp_overlay');
			
			$pp_overlay
				.css({
					'opacity':0,
					'height':$(document).height(),
					'width':$(window).width()
					})
				.bind('click',function(){
					$.prettyPhoto.close();
				});
			$('a.pp_close').bind('click',function(){ $.prettyPhoto.close(); return false; });
			_center_overlay();
		};
		return this.unbind('click.prettyphoto').bind('click.prettyphoto',$.prettyPhoto.initialize);
	};
})(jQuery);


/* =========================================================
// jquery.innerfade.js
// Datum: 2008-02-14
// Firma: Medienfreunde Hofmann & Baldes GbR
// Author: Torsten Baldes
// Mail: t.baldes@medienfreunde.com
// Web: http://medienfreunde.com
// based on the work of Matt Oakes http://portfolio.gizone.co.uk/applications/slideshow/
// and Ralf S. Engelschall http://trainofthoughts.org/
// ========================================================= */
(function($) {
	$.fn.innerfade = function(options) {
		return this.each(function() {
			$.innerfade(this, options);
		});
	};
	
	$.innerfade = function(container, options) {
		var settings = {
			'speed':   'normal',
			'timeout':  2000
		};
		
		if (options)
			$.extend(settings, options);
		
		var elements = $(container).children();
		
		if (elements.length > 1) {
			for (var i = 0; i < elements.length; i++) {
				$(elements[i]).hide();
			}
			
			setTimeout(function() {
				$.innerfade.next(elements, settings, 1, 0);
			}, settings.timeout);
			
			$(elements[0]).show();
		}
	};
	
	$.innerfade.next = function(elements, settings, current, last) {
		$(elements[last]).fadeOut(settings.speed, function () {
			$(elements[current]).fadeIn(settings.speed,function(){ removeFilter($(this)[0]); });
		});
		
		if ((current + 1) < elements.length) {
			current2 = current + 1;
			last2 = current2 - 1;
		} else {
			current2 = 0;
			last2 = elements.length - 1;
		}
		
		setTimeout((function() {
			$.innerfade.next(elements, settings, current2, last2);
		}), settings.timeout);
	};
	
	// **** remove Opacity-Filter in ie ****
	function removeFilter(element) {
		if(element.style.removeAttribute){
			element.style.removeAttribute('filter');
		}
	}
})(jQuery);

$(function() {
	$("#topicsArea dd").empty()
	$("#topicsArea dd").append("<ul></ul>");
	$("body").append('<div id="topics" style="display:none;"><ul></ul></div>');
	
	var minCount = 9;
	var contCount, loopCount;
	
	
	$.get("topics.xml",function(d){
		$(d).find("contents").each(function(){
			var contents = $(this);
			var text = $(this).find("text").text();
			var link = $(this).find("link").text();
			var image = $(this).find("image").text();
			var target = $(this).find("target").text();
			
			$("#topicsArea dd ul").append("<li><a href='"+link+"' target='"+target+"'>"+text+"</a></li>");
			$("#topics ul").append("<li><a href='"+link+"' target='"+target+"'><img src='"+image+"' alt='"+text+"' width='280' height='111' /></a></li>");
		});
		
		contCount = $(d).find("contents").length;
		loopCount = (contCount <= 9) ? minCount - contCount: 3 - (contCount % 3);
		
		for(var i = 0; i < loopCount; i++){
			$("#topics ul").append("<li>&nbsp;</li>");
		}
		$("#topics ul li:odd").addClass("bgBlue");
		
		$('#topicsArea dd ul').innerfade({
			speed: 1000,
			timeout: 5000
		});
	});

	$("#topicsArea dt a").prettyPhoto();
});

