$(document).ready( function(){q.init();});

var q = {

	/**************************************************************/
	/*                                                            */
	/* Cache                                                      */
	/*                                                            */			
	/**************************************************************/
	
	cache : [],
	
	/**************************************************************/
	/*                                                            */
	/* Text-strings                                               */
	/*                                                            */			
	/**************************************************************/

	text : {
		'Close'				: 'Close',
		'Loading'			: 'Loading…'
	},	
	init : function() {
		q.nav.init();
		q.popup.init();
		q.share.init();
		q.pages.init();		
		
		// Check for iOS
		if( navigator.platform == 'iPad' || navigator.platform == 'iPhone' || navigator.platform == 'iPod' ) {
			q.iOS.init();
		}		
		
		// Init tipTip
		//$('*[title]').tipTip({defaultPosition: 'top'});
		
		// Set target to windows that should open in new window
		$('a.newwin').attr('target','_blank');
		
		// AJAX setup
		$.ajaxSetup ({
    	cache: false,
    	dataType: 'html',
    	complete: function() { $('#loader-ajax').fadeOut(100, function() { $(this).remove(); }); }
		});
		
	},
		
	/**************************************************************/
	/*                                                            */
	/* Common help functions                                      */
	/*                                                            */			
	/**************************************************************/
	
	c : {
		
		flashVersion : swfobject.getFlashPlayerVersion().major,
		
		getBackgroundPos : function(a) {
			var b = $(a).css('backgroundPosition').split(' ');
			var x = 0;
			var y = 0;
			
			// Pixels or percent
			if( b[0].indexOf('px') != -1 ) {
				x = parseFloat(b[0].substring(0, b[0].length-2));
			} else {
				x = parseFloat(b[0].substring(0, b[0].length-1));
			}
			if( b[1].indexOf('px') != -1 ) {
				y = parseFloat(b[1].substring(0, b[1].length-2));
			} else {
				y = parseFloat(b[1].substring(0, b[1].length-1));
			}	
			return {x : x, y : y };
		},
		
		getIdFromHref : function(a) {
			return a.split('#')[1];
		},
		
		getSelectorFromHref : function(a) {
			return '#' + a.split('#')[1];
		},
		
		getQueryString : function(a,b) {
			var a = escape( unescape(a) );
			var regex = new RegExp("[?&]" + a + "(?:=([^&]*))?","i");
			var match = regex.exec(b);
			var value = null;
			if( match != null ) { value = match[1]; }
			return value;
		},
		
		getAjax : function(a, b) {
			if( q.cache[a.url] ) {
				a.success(q.cache[a.url]);
			} else {
			//b;
			$.ajax({
				url: a.url,
				success: function(data) {
					a.success(data);
					q.cache[a.url] = data;
  			}
			});			
			
			}
		}
	},

	/**************************************************************/
	/*                                                            */
	/* Functions to fix position fixed on iOS                     */
	/*                                                            */			
	/**************************************************************/

	iOS : {
		
		is : false,
	
		init : function() {
			q.iOS.is = true;
			$('#nav').css('position', 'absolute');
		}
		
	},

	/**************************************************************/
	/*                                                            */
	/* Handle share functions                                     */
	/*                                                            */			
	/**************************************************************/
		
	share : {
		
		startPos : 0,
		
		init : function() {
			$('#share h3').click(q.share.toggle);
			q.share.startPos = $('#share').css('top')
		},
		
		toggle : function() {
			if( $('#share').hasClass('open') ) {
				$('#share').stop().animate({top: q.share.startPos}, 1000, 'easeOutBounce');
				$('#share').removeClass('open')				
			} else {
				$('#share').stop().animate({top: 0}, 1000, 'easeOutBounce');
				$('#share').addClass('open')
			}
		}
	},	
		
	/**************************************************************/
	/*                                                            */
	/* Handle navigation                                          */
	/*                                                            */			
	/**************************************************************/		

	nav : {
		
		itemHeight : 42,
		isAnimating : false,
		items : [],
		scrollTimer : false,
		
		init : function() {
			$('#nav ul a').click( q.nav.go );
			$('#nav ul a').hover( q.nav.mouseOver, q.nav.mouseOut);			
			$('#nav li:not(.item-active)').css({backgroundPosition: '0 -42px'});
			$('#nav li.item-active').css({backgroundPosition: '0 0px'});
			q.nav.items;
			$('#nav li a').each(function(i){
				q.nav.items[i] = q.c.getSelectorFromHref( $(this).attr('href') );
			});
			
			q.nav.update();
			$(window).scroll( function() {
				clearTimeout(q.nav.scrollTimer);
				q.nav.scrollTimer = setTimeout('q.nav.update()', 250);
			});
		},
		
		update : function() {
			var halfWinHeight = $(window).height() / 2;
			var s = $('html').scrollTop();
			if( s == 0 || typeof(s) == 'undefined' ) { s = $('body').scrollTop(); }
  		
  		// Update nav for iOS
  		if( q.iOS.is ) {
  			s = $(window).scrollTop();
  			$('#nav').animate({top: $(window).scrollTop() + 60}, 250);
  		}

			var activePage = 0;
  		for( i = 0 ; i < q.nav.items.length ; ++i ) {
  			if( q.iOS.is ) {
  				if( $(window).scrollTop() + ( window.innerHeight / 2 )  >= ( $( q.nav.items[i] ).offset().top - $(window).scrollTop() ) ) { activePage = i; } else { break; }
  			} else {
  				if( s + halfWinHeight >= $( q.nav.items[i] ).offset().top ) { activePage = i; } else { break; }
  			}
  		}
  		q.nav.changePage( document.getElementById('nav').getElementsByTagName('ul')[0].getElementsByTagName('a')[activePage] );
		},
		
		go : function() {
			if( !$(this).parent().hasClass('item-active') && !q.nav.isAnimating ) {

				// Special solution for iOS
				if( q.iOS.is ) {
  				$('html, body').stop().animate({scrollTop: $(q.c.getSelectorFromHref(this.href)).offset().top - $(window).scrollTop()}, 1000, 'easeOutCirc', function() {
	  				s = $(window).scrollTop();
  					$('#nav').animate({top: s + 60}, 250);
  				});
  			} else {
					$('html, body').stop().animate({scrollTop: $(q.c.getSelectorFromHref(this.href)).offset().top + 'px'}, 1000, 'easeOutCirc');  			
  			}
				q.nav.changePage(this);
			}
			return false;
		},
		
		mouseOver : function() {
			if( !$(this).parent().hasClass('item-active') ) {
				$(this).css( {backgroundPosition: '-115px '+q.c.getBackgroundPos(this).y+'px'} );
			}
		},
		
		mouseOut : function() {
			if( !$(this).parent().hasClass('item-active') ) {
				$(this).css( {backgroundPosition: '0 '+q.c.getBackgroundPos(this).y+'px'} );
			}
		},		
		
		changePage : function(me) {
			if( !$(me).parent().hasClass('item-active') && !q.nav.isAnimating ) {
				q.nav.isAnimating = true;
		  	var activeBY = q.c.getBackgroundPos('#nav li.item-active a').y + q.nav.itemHeight;
		  	var activeBX = q.c.getBackgroundPos('#nav li.item-active a').x;
		  	var nextBY = q.c.getBackgroundPos(me).y - q.nav.itemHeight;
		  	var nextBX = q.c.getBackgroundPos(me).x;
		  	$('#nav li.item-active a').stop().animate({backgroundPosition: '('+activeBX+' '+activeBY+'px)'}, 250, function() {
		  		$(this).css( {backgroundPosition: '0 '+q.c.getBackgroundPos(this).y+'px'} );
		  	});
		  	
		  	$(me).stop().animate({backgroundPosition: '('+nextBX+' '+nextBY+'px)'}, 250, function() {
		  		$(this).css( {backgroundPosition: '0 '+q.c.getBackgroundPos(this).y+'px'} );
		  	});			
		  	
		  	$('#nav li.item-active').stop().animate({backgroundPosition: '(0 42px)'}, 250, function() {
					if( q.iOS.is ) {
			  		$(this).css( {backgroundPosition: '0 45px'} )
		  		}
		  	});
		  	$('#nav li.item-active').removeClass('item-active');
		  	$(me).parent().css({backgroundPosition: '0 -42px'});
		  	$(me).parent().stop().animate({backgroundPosition: '(0 0)'}, 250, function() {
		  		q.nav.isAnimating = false;
		  	});
		  	$(me).parent().addClass('item-active');

	  	}
		}
	},

	/**************************************************************/
	/*                                                            */
	/* Forms                                                      */
	/*                                                            */
	/**************************************************************/
	
	forms : {
	
		init : function() {
			$('form.ajax').bind('submit', q.forms.submit);	
		},
		
		submit : function() {
			
			var valid = true;
			$(this).find('input.mail').each(function() {
				var regExp = /^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+/;
				if( $(this).val().search(regExp) == -1  ) {
					valid = false;
					$(this).data('value', $(this).val());
					$(this).addClass('error');
					$(this).val('notanemail');
					$(this).one('click', function() {
						$(this).val( $(this).data('value') );
						$(this).removeClass('error');
					});
				}
			});

			$.ajax({
				url: $(this).attr('action'),
				type: $(this).attr('method'),
				data: $(this).serialize(),
				success: function(data) {
					$('#popup-content').html(data);
  			}
			});				
			
			return false;
		}
	},
	
	/**************************************************************/
	/*                                                            */
	/* Popup                                                      */
	/*                                                            */
	/**************************************************************/	
	
	popup : {
	
		html : '<div id="popup"><div id="popup-close"></div><div id="popup-top"></div><div id="popup-middle"></div><div id="popup-bottom"></div><div id="popup-content"></div></div>',
		minHeight : 1,
		heightDif : 340,
		
		init : function() {
			// Set links to use AJAX
			$('a.ajax').live('click', q.popup.show);
			
			// Links in popup that scroll and etc.
			$('#popup a.popup-next, #popup a.popup-prev').live('click', q.popup.switchPage);
			$('#popup ul.images-nav a').live('click', q.popup.switchImage);
		},
		
		show : function() {
			$('#popup').remove();
			$('body').append(q.popup.html);
			
			// Add close-button functionality
			$('#popup-close').text(q.text["Close"]);
			$('#popup-close').click(q.popup.hide);			
			
			// Add loader
			$('#popup-content').html('<p class="loader" id="loader-ajax">' + q.text["Loading"] + '</p>');
			
			// Position and resent the popup
			var startPos = $('html').scrollTop() + $('body').scrollTop() + $(window).height();
			var endPos = $('html').scrollTop() + $('body').scrollTop() + 50;
			
			// For iOS
  		if( q.iOS.is ) {
  			startPos = $(window).scrollTop() + $('#popup').height();
  			endPos = $(window).scrollTop() + 100;
  		}			
			
			$('#popup').css({top: startPos + 'px'});
			$('#popup').animate({top: endPos + 'px'}, 750, 'easeOutBack');
			
			$.ajax({
				url: $(this).attr('href'),
				success: function(data) {
					$('#popup-content').html(data);
				  var endHeight = $('#popup-content').height();
				  $('#popup-middle').animate({height: endHeight - q.popup.heightDif + 'px'}, 1000, 'easeOutBack');
				  
				  // Since live-event wont work in IE we put a check for forms here
				  q.forms.init();
				  
  			}
			});
			return false;
		},
		
		hide : function() {
			$('#popup').stop().animate({top: $('#popup').offset().top + $(window).height()}, 750, 'easeInBack', function() {
				$(this).remove();
			});
		},
		
		switchPage : function() {
			var curPage = $(this).parents('div.popup-page:first');
			var newPos = $(curPage).outerWidth() * $(curPage).next().index();			
			if( $(this).hasClass('popup-prev') ) {
				newPos = $(curPage).outerWidth() * $(curPage).prev().index();
			}
			$('#popup div.popup-pages-wrapper').animate({scrollLeft: newPos }, 500, 'easeOutSine');				
			return false;
		},
		
		switchImage : function() {
			var a = $(this).parent().attr('class');
			var w = $(this).parents('div.images-wrapper:first').find('div.images:first');
			
			// Check which page we're on and update paging
			var p = $(this).parent().siblings('li.page');
					pv = $(p).text();
					pv = pv.split('/');
					pv[0] = parseInt(pv[0]);
					pv[1] = parseInt(pv[1]);
			var scrollStep = 300;
			var scrollPos = $(w).scrollLeft();
			if( a == 'next' && pv[0] < pv[1] ) {
				$(w).animate({scrollLeft: scrollPos + scrollStep }, 500, 'easeOutSine');
				$(p).text( ( pv[0]+1 ) + '/' + pv[1] );
			} else if( a == 'prev' && pv[0] > 1 ) {
				$(w).animate({scrollLeft: scrollPos - scrollStep }, 500, 'easeOutSine');				
				$(p).text( ( pv[0]-1 ) + '/' + pv[1] );
			} else if( a == 'zoom' ) {
				window.open( $(this).parents('div.images-wrapper:first ul.tn li:eq(' + pv[0] + ') a').attr('href') );
			}
			
			return false;
		}
	},
	
	/**************************************************************/
	/*                                                            */
	/* Handle functions for specific pages                        */
	/*                                                            */			
	/**************************************************************/
	
	pages : {
	
		init : function() {
			if( $('#page-glimpse').is('div') ) { q.pages.glimpse.init(); }
			//if( $('#page-app').is('div') ) { q.pages.a.init(); }			
			
			q.pages.archive.init();			
			q.pages.books.init();
			q.pages.places.init();
			q.pages.movies.init();			
			q.pages.now.init();		
			q.pages.prize.init();
			q.pages.norstedts.init();			
		},
		
		app: {
			
			init: function() {
				$('#page-app > div.content-wrapper > div.content').append('<div class="sound"><h3>Listen to a sound sample from the app:</h3><div class="player"><script language="javascript" src="http://www.clipsource.se/soundPlayer/a/EDucCNfnbcZ9yAB43n0L0w/7641f6352c42a97d82dc359683d0022744ad4ac1/d25fa2724e097ab79a676aded30db35a32fa2b23/884ed491a3a1ecb/"></script></div></div>');
			}
		
		},

		prize: {
			init: function () {
				var popup = $('.popup-prize');

				$('body').delegate('.popup-next', 'click', function () {
					var left = parseInt($('.popup-pages').css('marginLeft'));

					$('.popup-pages').animate({
						marginLeft: left - 700
					});
				});

				$('body').delegate('.popup-prev', 'click', function () {
					var left = parseInt($('.popup-pages').css('marginLeft'));

					$('.popup-pages').animate({
						marginLeft: left + 700
					});
				});
			}
		},

		/**************************************************************/
		/*                                                            */
		/* Glimpse                                                    */
		/*                                                            */
		/* Handling the hover effect on links                         */
		/*                                                            */		
		/**************************************************************/	
	
		glimpse : {
		
			init : function() {
				$('#page-glimpse ul li').append('<div class="trigger"></div>');
				$('#page-glimpse ul li').append('<div class="shadow"><div></div></div>');
				$('#page-glimpse ul li div.trigger').click(function() {
					$(this).siblings('a:first').trigger('click');
				});
				
				$('#page-glimpse ul li a').each(function(index) {
					$(this).data('startLeft', $(this).position().left);
				});
				
				$('#page-glimpse ul li div.trigger').hover(
				
					// Mouse-over
					function() {
						var a = $(this).siblings('a:first');
						$(this).siblings('div.shadow').stop().fadeOut(250);
								
						var dir = 1;
						if( $(a).offset().left < 490 ) { dir = -1; }
								
						$(a).stop().animate({
							opacity: 0,
							left: $(a).data('startLeft') + ( 100 * dir ) },
							1000, 'easeOutSine');							
					},
					
					// Mouse-out					
					function() {
						var a = $(this).siblings('a:first');
						$(this).siblings('div.shadow').stop().fadeIn(250, function() {
							$(this).css('opacity', 1);
						});
						$(a).stop().animate({
							opacity: 1,
							left: $(a).data('startLeft') },
							2000, 'easeOutSine', function() { /*this.style.removeAttribute('filter'); */});	
					}
				);
				
				// Add more-arrow
				q.pages.glimpse.arrowMore();
				
			},
			
			arrowMore : function() {
				$('#page-glimpse div.content-wrapper:first').append('<div id="arrow-more"></div>');
				$('#arrow-more').hide();
				$('#arrow-more').delay(1000
				).show('drop', {direction: 'down'}, 750
				).delay(500
				).animate({top: 600}, 250, 'easeOutCirc'
				).animate({top: 650}, 1000, 'easeOutBounce'
				).delay(250
				).animate({top: 600}, 250, 'easeOutCirc'
				).animate({top: 650}, 1000, 'easeOutBounce'								
				).delay(1000
				).hide('drop', {direction: 'down'}, 500, function() {
					$(this).remove();
				});
			}
		},

		/**************************************************************/
		/*                                                            */
		/* Archive                                                    */
		/*                                                            */
		/* Handling regions and categories                            */
		/*                                                            */		
		/**************************************************************/	
	
		archive : {
			
			pressAnimate : false,
			contentStartHeight : 0,
			contentStartWidth : 0,			
			
			init : function() {
				
				// Add round corners in IE
				if( !$.support.opacity ) {
					$('#archive-content').append('<span class="c-ul"></span><span class="c-ur"></span><span class="c-dr"></span><span class="c-dl"></span>');
				}
				
				// Bind show regions
				$('#archive-content h4').one('click', function() {
					$('#archive-content h4').hide('blind', {direction: 'vertical'}, 500);
					$('#archive-categories').show('blind', {direction: 'vertical'}, 500, function() {
						q.pages.archive.contentStartHeight	= $('#archive-content').height();
						q.pages.archive.contentStartWidth		= $('#archive-content').width();
					});
				});
				
				$('#archive-categories').hide();
				$('#archive-content').append('<div id="archive-region"></div>');
				$('#archive-region').hide();
				
				var zIndex = 999;
				$('#archive-categories ul.countries li a').each(function(i) {
					$(this).parent().css('zIndex', zIndex); --zIndex;		// Set zIndex in a descending order
					$(this).css('background-image','url(/images/pages/archive/flags2/' + this.className + '.png)');
					$(this).click( q.pages.archive.loadRegion );
					$(this).parent().append('<div class="archive-country-over"><span class="left"><span class="right">' + $(this).text() + '</span></span></div>');
					$(this).parent().append('<div class="trigger"></div>');
				});
				
				// Link Global information
				$('#archive-categories h5 a').click( q.pages.archive.loadRegion );
				
				$('#archive-categories ul.countries li div.trigger').click(function() {
					$(this).siblings('a:first').trigger('click');
				});				
				
				$('#archive-categories ul.countries li div.trigger').hover( function() {
					if( $.support.opacity ) {
						$(this).siblings('div.archive-country-over:first').fadeIn(250);
					} else {
						$(this).siblings('div.archive-country-over:first').show();
					}
				}, function() {
					if( $.support.opacity ) {				
						$(this).siblings('div.archive-country-over:first').fadeOut(100);				
					} else {
						$(this).siblings('div.archive-country-over:first').hide();
					}
				});
				
				// Navigation for region
				$('#archive-region div.nav:first a').live('click', q.pages.archive.nav.go );
				$('#archive-region a.back:first').live('click', q.pages.archive.nav.goBack );				
				
				// Mouse over animation on press just for fun
				setInterval("q.pages.archive.reachForTheSky()", 15000);
			},		
			
			loadRegion : function() {
				$.ajax({
					url: $(this).attr('href'),
					success: function(data) {
						$('#archive-region').css('visibility', 'hidden');
						$('#archive-content').animate({width: 750}, 500, function() {
							$('#archive-content').css('overflow','hidden');
							$('#archive-content').animate( {height: 20}, 500, function() {
									$('#archive-region').show();
									$('#archive-region').html(data);
									$('#archive-region').css('visibility', 'visible');								
									$('#archive-region div.nav ul li:first').addClass('active');
									q.pages.archive.nav.init();
									var newHeight = $('#archive-region').outerHeight();
									if( newHeight < 500 ) { newHeight = 500; }
									$('#archive-content').animate({height: newHeight}, 750);
							});

							$('#archive-categories').hide('slide', {direction: 'up'}, 500, function() {
								$(this).find('div.archive-country-over').hide();								
							});

						});
	  			}
				});				
				return false;
			},
			
			nav : {
				
				init : function() {
					$('#archive-region-content div.archive-page').wrapAll('<div id="archive-pages-wrapper"></div>');
					$('#archive-pages-wrapper').width( $('#archive-region-content div.archive-page').outerWidth() * $('#archive-region-content div.archive-page').length );					
				},
				
				go : function() {
					$('#archive-region div.nav:first li.active').removeClass('active');
					$(this).parent().addClass('active');
					var page = $('#archive-region-content div.archive-page:eq(' + $(this).parent().index() + ')');
					var newPos = $(page).outerWidth() * $(this).parent().index();
					$('#archive-region-content').animate({scrollLeft: newPos }, 500, 'easeOutSine');
					return false;
				},
				
				goBack : function() {
					$('#archive-region div.nav:first li:first a').trigger('click');
					$('#archive-region').hide('blind', {direction: 'vertical'}, 500, function() {
						$('#archive-content').animate({width: q.pages.archive.contentStartWidth}, 250, function() {
							$('#archive-categories').show('slide', {direction: 'up'}, 750);
							$('#archive-content').css('overflow','visible');
						});
					});
					$('#archive-content').animate({height: q.pages.archive.contentStartHeight}, 500);	
					return false;
				}
			},
			
			reachForTheSky : function () {
		  	var t = $('#page-archive p.press').position().top;
		  	$('#page-archive p.press').animate({top: t -150}, 500, 'easeOutCirc', function() {
		  		$('#page-archive p.press').animate({top: t}, 1000, 'easeOutBounce');
		  	});						
			}
		}, 
	
		/**************************************************************/
		/*                                                            */
		/* Books                                                      */
		/*                                                            */
		/* Handling the hover effect on links                         */
		/*                                                            */		
		/**************************************************************/	
	
		books : {
			
			init : function() {
				$('#page-books div.image-wrapper').height( $('#page-books div.content-wrapper').height());
				$('#page-books ul li a').hover( 
					function() {
						$(this).parent().css({backgroundPosition: '-600px 0', backgroundColor: '#000'});
						$(this).parent().stop().animate({backgroundPosition: '(0 0)'}, 500);
					}, function() {
						$(this).parent().css({backgroundPosition: '0 0', backgroundColor: '#000'});
						$(this).parent().stop().animate({backgroundPosition: '(-600px 0)'}, 500);
					}
				);
			}
		},
		
	/**************************************************************/
	/*                                                            */
	/* Places                                                     */
	/*                                                            */		
	/**************************************************************/		
		
		places : {
			
			isAnimating : false,
			
			images : Array(),
			
			init : function() {
				$('#places-nav a').click(q.pages.places.newSlide);
				$('#places-nav li:not(.prev):not(.next):not(:first)').fadeTo(0, 0.2);
				$('#places-nav li:not(.prev):not(.next):first').addClass('active');
				$('#places-list li:not(:first)').hide();
				
				// Fix spacing betwwen bullets in image-nav
				var bullets = $('#places-nav li:not(.prev):not(.next)');
				var numOfBulls = $(bullets).length;
				var bulletWidth = $(bullets).filter(':first').width();
				var widthOfNav = $('#places-nav ul:first').width() - $('#places-nav li.prev').width() - $('#places-nav li.next').width();
				
				var spaceLeft = widthOfNav - ( bulletWidth * numOfBulls);
				var margin = Math.floor( ( spaceLeft / numOfBulls ) / 2);
				$(bullets).find('a').css({marginLeft: margin + 'px', marginRight: margin + 'px'})

				//q.pages.places.getImages();
				
			},
			
			getImages : function() {
				$('#places-list li').each(function(i) {
					q.pages.places.images[i] = new Image();
					q.pages.places.images[i].src = $(this).find('.img img').attr('alt');
					q.pages.places.images[i].rel = i;
					q.pages.places.images[i].onload = q.pages.places.imageLoaded;
				});
			},
			
			imageLoaded : function() {
				var img = $('#places-list li div.img img:eq('+this.rel+')');
				$(img).after('<img src="'+this.src+'" alt="" />');
				var ni = $(img).siblings('img');
				$(ni).hide();
				$(ni).fadeIn(1500, function() { $(img).remove(); });
			},
			
			newSlide : function() {
				if( q.pages.places.isAnimating || $(this).parent().hasClass('active') ) { return false; } else { q.pages.places.isAnimating = true; }

				var documentWidth = $(document).width();
				var ul = $('#places-list ul');
				var me = this;
				var as = $('#places-list li.active');
				var ns = $( q.c.getSelectorFromHref($(this).attr('href')) );

				// check which direction we should move. This can be overwritten by click on the arrows
				var dir = 'left';
				if( $(as).index() > $(ns).index() ) { dir = 'right'; }

				if( $(this).parent().hasClass('prev') ) {
					dir = 'right';
					ns = $(as).prev();
					if( !$(ns).is('li') ) {
						ns = $('#places-list li:last');
						dir = 'left';
					}
					me = $('#places-nav li:not(.prev):not(.next) a:eq('+$(ns).index()+')');
				} else if( $(this).parent().hasClass('next') ) {
					dir = 'left';
					ns = $(as).next();
					if( !$(ns).is('li') ) {
						ns = $('#places-list li:first');
						dir = 'right';
					}
					me = $('#places-nav li:not(.prev):not(.next) a:eq('+$(ns).index()+')');					
				}
				
				$('#places-nav li:not(.prev):not(.next)').delay(10).fadeTo(500, 0.2, function() {
					$('#places-nav li:not(.prev):not(.next)').removeClass('active');
					$(me).parent().addClass('active').stop().fadeTo(500, 1);
				});
				$(as).removeClass('active');
				$(ns).addClass('active');
				
				$(ul).width( ( documentWidth * 2 ) + 10);
				$('#places-list ul li').width( documentWidth );
				$(ns).show();
				var newPos = documentWidth * -1;
				if( dir == 'right' ) {
					$(ul).css({marginLeft: newPos})
					newPos = 0;
				}
				
				$(ul).animate({marginLeft: newPos }, 1000, function() {
					$(as).hide();
					$(ul).css({width: '100%', marginLeft: 0})
					$('#places-list ul li').css({width: '100%'})
					q.pages.places.isAnimating = false;
				});

				return false;
			}
		},
		
	/**************************************************************/
	/*                                                            */
	/* Movies                                                     */
	/*                                                            */		
	/**************************************************************/				
		movies : {
			
			tnWidth : 0,
			perPage : 4,
			numOfMovies : 0,
			pagingAnimating : false,
			
			init : function() {
			
				// Init sIFR
				if(q.c.flashVersion >= 6 ) {
					sIFR.replaceElement('#page-movies h2, #page-movies h3', named({sFlashSrc: '/js/sIFR/helvetica_cb.swf', sColor: '#f1c241', sCase: 'upper', sBgColor: '#000000', sWmode: 'transparent', sFlashVars: ''}));
				}
				
				// Fix the paging wrapper
				q.pages.movies.numOfMovies = $('#page-movies .movies-thumbnails ul.movies li').length;
				var rest = q.pages.movies.numOfMovies % q.pages.movies.perPage;
				q.pages.movies.tnWidth = $('#page-movies .movies-thumbnails ul.movies li').width();
				var wrapperWidth = q.pages.movies.numOfMovies * q.pages.movies.tnWidth;
				$('#page-movies .movies-thumbnails ul.movies').width( wrapperWidth + ( ( q.pages.movies.perPage - rest ) * q.pages.movies.tnWidth ) );
				
				// More stuff to init
				q.pages.movies.YouTube.init();
				q.pages.movies.imdbLink();				
				
				// Add paging
				if( q.pages.movies.numOfMovies > q.pages.movies.perPage ) {
			  	var HTML = '<ul class="paging">';
			  			HTML+= 	'<li class="prev"><a href="#Prev">Previous</a></li>';
			  			HTML+= 	'<li class="next"><a href="#Next">Next</a></li>';
			  			HTML+= '</ul>';
			  	$('#page-movies .movies-thumbnails').append(HTML);
			  	$('#page-movies .movies-thumbnails .paging li.prev').addClass('disabled');
			  	$('#page-movies .movies-thumbnails .paging a').click(q.pages.movies.switchPage);
				}
			},
			
			switchPage : function() {
				if( !q.pages.movies.pagingAnimating && !$(this).parent().hasClass('disabled') ) {
					q.pages.movies.pagingAnimating = true;
					var cp = $('#page-movies .movies-wrapper').scrollLeft();
					var pw = q.pages.movies.tnWidth * q.pages.movies.perPage;
					var newPos = cp - pw;
					if( $(this).parent().hasClass('next') ) { newPos = cp + pw; }
					$('#page-movies .movies-wrapper').animate({ scrollLeft: newPos }, 500, function() { q.pages.movies.pagingAnimating = false; });					
					q.pages.movies.updatePaging( newPos );
				}
				return false;
			},
			
			updatePaging : function( cp ) {
				var ms = ( q.pages.movies.numOfMovies * q.pages.movies.tnWidth ) - ( q.pages.movies.tnWidth * q.pages.movies.perPage );
				$('#page-movies .movies-thumbnails ul.paging li').removeClass('disabled');				
				if( cp == 0 ) {
					$('#page-movies .movies-thumbnails ul.paging li.prev').addClass('disabled');
				} else if( cp >= ms ) {
					$('#page-movies .movies-thumbnails ul.paging li.next').addClass('disabled');
				}			
			},
			
			YouTube : { 
			
				init : function() {
					$('#movie-large').append('<div id="movie-large-swf-container"><div id="movie-large-swf"></div></div>');
					$('#movie-large a').hide();
					$('#page-movies a.youtube').click( q.pages.movies.YouTube.switchMovie );
					$('#page-movies .movies-thumbnails a.youtube:first').triggerHandler('click');					
				},
				
				switchMovie : function() {
					var swf = 'http://www.youtube.com/v/' + q.c.getQueryString('v', this.href) + '&fs=1';
					var newHeader = $(this).find('strong').text();
					$('#page-movies h2:first').html(newHeader);
					$('#page-movies h2:first').removeAttr('class');
					if(q.c.flashVersion >= 6 ) {
						setTimeout("sIFR.replaceElement('#page-movies h2', named({sFlashSrc: 'js/sIFR/helvetica_cb.swf', sColor: '#f1c241', sCase: 'upper', sBgColor: '#000000', sWmode: 'transparent', sFlashVars: ''}))", 500);
					}
					$('#movie-large-swf').empty();
					if( q.c.flashVersion >= 9 ) {
						swfobject.embedSWF(swf, 'movie-large-swf', '755', '454', '9.0.0', '', {}, {bgcolor:'#000000'});
					} else {
						
						// Fallback mainly for iPad and iPhone and iWhatever
						var HTML = '<object width="755" height="454">';
								HTML+= '<param name="movie" value="' + swf + '"></param>';
								HTML+= '<param name="wmode" value="transparent"></param>';
								HTML+= '<embed src="' + swf + '" type="application/x-shockwave-flash" wmode="transparent" width="755" height="454"></embed>';
								HTML+= '</object>';
						$('#movie-large-swf').html(HTML);
						
					}
					$('#page-movies a.youtube.active').removeClass('active');
					$('#page-movies .movies-thumbnails li.active').removeClass('active');
					$(this).parent().addClass('active');
					return false;
				}
				
			},
			
			imdbLink : function() {
				
				// Add the cool glow to the IMDb link
				$('#page-movies .imdb-link span.imdb-btn').append('<span class="glow"></span><span class="corners"></span>');
				
				$('#page-movies a.imdb-link').hover(
					function() {
						$(this).find('span.imdb-btn span.glow').css({backgroundPosition: '-60px -40px'});
						$(this).find('span.imdb-btn span.glow').animate({backgroundPosition: '(40px -40px)'}, 750, 'easeInOutSine');
					}, function() {}
				);			
			}
		},
		
	/**************************************************************/
	/*                                                            */
	/* Now                                                        */
	/*                                                            */		
	/**************************************************************/		
		
		now : {
			
			flickrUrl : 'http://api.flickr.com/services/feeds/photos_public.gne',			
			flickrTags : 'stig larsson,The Girl with the Dragon Tattoo,Mikael Blomkvist,Lisbeth Salander,The Girl Who Kicked the Hornet\'s Nest,The Girl Who Played with Fire',
			pageWidth : 0,

			init : function() {
				q.pages.now.flickr();
				q.pages.now.pageWidth = $('#page-now div.page:first').outerWidth();
				$('#page-now div.page').wrap('<div class="pages"></div>');
				$('#page-now ul.paging li.next a').live('click', q.pages.now.pageForward);
				$('#page-now ul.paging li.prev a').live('click', q.pages.now.pageBack);				
			},

			pageBack : function() {
				var p = $('#page-now div.pages');		
				var newPos = $(p).css('marginLeft');
				newPos = parseInt( newPos.substring(0, newPos.length-2) ) + q.pages.now.pageWidth;
				$('#page-now div.pages').animate({ marginLeft: newPos }, 500);			
				return false;	
			},

			pageForward : function() {
				var p = $('#page-now div.pages');			
				var newPos = $(p).css('marginLeft');
						newPos = parseInt( newPos.substring(0, newPos.length-2) ) - q.pages.now.pageWidth;
				if( $(this).parents('div.page:first').next('div.page').is('div') ) {
					$('#page-now div.pages').animate({ marginLeft: newPos }, 500);
				} else {
					$.ajax({
						url: $(this).attr('href'),
						success: function(data) {
							$(p).width( $(p).width() + q.pages.now.pageWidth );
							$(p).append(data);
							$('#page-now div.pages').animate({ marginLeft: newPos }, 500);
		  			}
					});
				}
				return false;	
			},

			flickr : function() {
				var url = q.pages.now.flickrUrl + '?tags=' + q.pages.now.flickrTags;
						url+= '&tagmode=any&format=json&jsoncallback=?';

				$('#page-now div.content').append('<div id="now-flickr"><div class="col"></div><div class="col"></div></div>');
				$.ajax({
					url: url,
					dataType: 'json',
					type: 'get',
					success: function(data) {
	
						$.each(data.items, function(i,item) {
							sel = ' div:eq(' + (i & 1) + ')';
							$('#now-flickr ' + sel).append('<a href="'+ item.link +'" target="flickr"><img src="'+ item.media.m +'" /></a>');
							//$('<img/>').attr('src', item.media.m).appendTo('#now-flickr ' + sel);
							if ( i == 16 ) { return false; }
						});
					}				
				});				
			}
		},
		
	/**************************************************************/
	/*                                                            */
	/* Norstedts                                                  */
	/*                                                            */		
	/**************************************************************/			
		
		norstedts : {
			
			init : function() {
				$('#page-norstedts div.more-books li').each(function(i) {
					$(this).click(function() { window.open( $(this).find('a').attr('href') ); });
					$(this).hover(
						function() { $(this).find('img:first').stop().animate({top: -10}, 250); },
						function() { $(this).find('img:first').stop().animate({top: 0}, 250); }
					);
				});
			}
		}
	}
}
