// Debugging-Function. Only active in Browser who support it
function pr() {
	if (!window.console) {
		return;
	}
	if (typeof window.console !== 'undefined' && typeof window.console.log === 'function') {
		console.log(pr.arguments);
	}
}

var myListener = new Object();
myListener.onInit = function() {};

(function($) {
	// Produces correct URLs relative to Router::url('/)
	$.url = function(url) {
	  return $('meta[name=identifier-url]').attr('content')+url.substr(1);
	};

	$(function() {
		// Generate a Page-Object to bind events on
		window.Page = $('body');
		
		//Take care about the non-js fallback-version of the site 
		$('body').addClass('js').removeClass('no-js');
		 
		// Make external links open in a new window
		$('a[rel=external]').live('click', function() {
			window.open(this.href);
			return false;
		});

		// Fix Alpha-PNG's for IE6
		if ($.browser.msie && $.browser.version < 7) {
			$.ifixpng($.url('/js/jquery/ifixpng/pixel.gif'));
			$('img[src$=.png]').ifixpng();
		}

		// And now all the project-specific stuff to be executed ON READY
		// $.local.func();
		$('ul.expandable').expandable();

		$('ul.slideshow').slideshow();

		// Media-Podcasts
		$('.playercontrol a.play').bind('click.player', function() {
			// Pause all other players
			$('.playercontrol a.pause').trigger('click.player');
			// Now playe
			var $player = $(this).closest('.playercontrol').find('.player');
			if (!$(this).data('url')) {
				$player.get(0).SetVariable("method:setUrl", $(this).attr('href'));
				$(this).data('url', $(this).attr('href'));
			}
			$player.get(0).SetVariable("method:play", "");
			return false;
		});
		$('.playercontrol a.pause').bind('click.player', function() {
			// var $player = $(this).closest('.playercontrol').find('.player');
			// $player.get(0).SetVariable("method:pause", "");
			// Pause all players:
			$('.playercontrol .player').each(function() {
				this.SetVariable("method:pause", "");
			});
			return false;
		});
		$('.playercontrol a.download').bind('click.player', function() {
			window.open(this.href);
			return false;
		});

		$.local.scrollPane();
	});

	// Generate namespace for project-specific functions encapsulated in the jQuery $-namespace
	$.local = function() {};
	// Definition of project-specific function
	$.local.scrollPane = function() {
		$('#text').jScrollPane({scrollbarWidth: 9});
	};
	$.local.fadeSlides = function(from, to) {
		var $to = $('#media li.'+to);
		if (from.length == 0) {
			var $from = $('#media li:visible');
			if ($from.attr('class') == $to.attr('class')) {
				return;
			}
			var $other = $('#media li:not(:visible)');
		} else {
			var $from = $('#media li.'+from);
			var $other = $('#media li:not(.'+from+')');
		}
		$from.css({zIndex: 10});
		$other.hide().css({zIndex: 2});
		$to.css({zIndex: 15});
		$from.fadeOut('slow');
		$to.fadeIn('slow');
	};
	
	$.fn.expandable = function() {
		var $parent = $(this);

		$(this).find('a.expander').die('click.expandable').live('click.expandable', function() {
			if ($parent.find('.expanding:animated').length > 0) {
				return false;
			}
			var $to = $(this).closest('.expandable');
			if ($to.is('.expanded')) {
				$to.find('.expanding').slideUp('slow', function() {
					$(this).closest('.expandable').removeClass('expanded');
				});
				return false;
			}
			var $from = $parent.find('li.expanded');
			$.local.fadeSlides($from.semantic('section'), $to.semantic('section'));
			$from.find('.expanding').slideUp('slow', function() {
				$(this).closest('.expandable').removeClass('expanded');
			});
			window.setTimeout(function() {
				$to.find('.expanding').slideDown('slow', function() {
					$(this).closest('.expandable').addClass('expanded');
					$.local.scrollPane();
				});
			}, 10);
			return false;
		});
		return $(this).each(function() {
			$(this).find('li:first').addClass('expanded');
		});
	};
	
	$.fn.slideshow = function() {
		var directions = [{
			from: 'top left 0.5x',
			to:   'bottom right 1x'
		}, {
			from: 'bottom right 0.5x',
			to:   'top right 1x'
		}, {
			from: 'top right 0.5x',
			to:   'bottom left 1x'
		}, {
			from: 'bottom left 0.5x',
			to:   'top left 1x'
		}];
		return $(this).each(function() {
			var elements = [];
			$(this).find('img').each(function(i) {
				var element = {
					src: 	$(this).attr('src')
					// from: 	directions[i%directions.length].from,
					// to: 	directions[i%directions.length].to,
					// time: 	3
				};
				elements.push(element);
			});
			$(this).crossSlide({
				fade: 1.5,
				sleep: 2
			}, elements);
		});
	};

	$.fn.field = function(field, val, accessor, map) {
		var r = [], one = (this.length == 1), get = true, fields = field, customAccessor = !!accessor, customMap = map;
		var multi = (field.constructor == Array)
			? 'get'
			: (typeof fields == 'object')
				? 'set'
				: false;
		if (multi == 'get') {
			fields = {};
			$.each(field, function() {
				fields[this] = undefined;
			});
		} else if (multi == false) {
			fields = {};
			fields[field] = val;
		}
		this.each(function(i) {
			map = customMap || $.data(this, 'fields');
			var $this = $(this), self = this;
			for (var f in fields) {
				var v = fields[f];
				accessor = accessor || {
					get: function(f) {return map[f].get.call(this);},
					set: function(f, v) {map[f].set.call(this, v);}
				};
				if (get = get && (typeof v == 'undefined')) {
					if (multi == 'get') {
						r[i] = r[i] || {};
						r[i][f] = accessor.get.call($this, f);
					} else {
						r.push(accessor.get.call($this, f));
					}
				} else {
					accessor.set.call($this, f, v);
					if (!customAccessor && 'Page' in window) {
						Page.trigger('changeField', [$this, f, v]);
					}
				}
			}
		});
		return (!get)
			? this
			: (one)
				? r[0]
				: r;
	};

	$.fn.semantic = function(options, key, val) {
		if (typeof options == 'string') {
			val = key;
			key = options;
			options = undefined;
		}

		arguments.callee.helpers = arguments.callee.helpers || {
			get: function(key, options) {
				var m = (this.attr(options.attr) || "").match(options.regex);
				return (m)
					? m[1]
					: false;
			},
			set: function(key, val, options) {
				var attrVal = this.attr(options.attr) || "", newAttrVal;
				if ((newAttrVal = attrVal.replace(options.regex, key+'-'+val)) == attrVal && !options.regex.test(attrVal)) {
					newAttrVal = (attrVal)
						? attrVal+' '+key+'-'+val
						: key+'-'+val;
				}
				this.attr(options.attr, newAttrVal);
			}
		};
		options = $.extend({
			attr: 'class',
			get: arguments.callee.helpers.get,
			set: arguments.callee.helpers.set,
			regex: new RegExp(key+'-([^\\s]+)')
		}, options || {});

		return this.field(key, val, {
			get: function(k) {return options.get.call(this, k, options);},
			set: function(k, v) {options.set.call(this, k, v, options);}
		}, {});
	};
})(jQuery);
