var windowObserver = application.Class.extend({
	init: function(){
		var self = this;
		self.topSpace = 200;
		self.render();
		self.events();
	},
	render : function(){
		var self = this;
		self.adjust();
	},
	events : function(){
		var self = this;
		$j(window).resize(function(){
			self.adjust();
		});
	},
	adjust : function(){
		var self = this;
		var minHeight = $j('.content-container').height() + self.topSpace;
		$j('.container-0').css({ height: $j('.content-container').height() + self.topSpace });
		if ( $j(window).height() > minHeight ){
			$j('.container-0').css({ height: $j(window).height() });
		}
	}
});
var gallery = application.Class.extend({
	init: function(){
		var self = this;
		self.e = $j('#Gallery');
		self.intro = $j('#Introduction',self.e);
		self.introOpen = true;
		self.introCloseButton = $j('.close-button-intro',self.e);
		self.thumbs = $j('.gallery-thumbnails');
		self.thumbsOpen = false;
		self.thumbsCloseButton = $j('.gallery-thumbnails .close-button');
		self.thumbLinks = $j('.gallery-thumbnails ul li a');
		self.nav1 = $j('.navigation-content-1',self.e);
		self.nav2 = $j('.navigation-content-2',self.e);
		self.render();
		self.events();
	},
	render : function(){
		var self = this;
		$j.easing.easeOutExpo = function (x, t, b, c, d) {
			return (t===d) ? b+c : c * (-Math.pow(2, -10 * t/d) + 1) + b;
		};
		$j(".scrollable").scrollable({
			items: ".items",
			easing: "easeOutExpo",
			speed: 500
		});
		self.thumbLinks.click(function(){
			self.thumbLinks.removeClass('a');
			$j(this).addClass('a');
		});
		self.thumbsPosition();
	},
	events : function(){
		var self = this;
		$j('.gallery-thumbnails ul li a').live('click',function(){
			application.ui.fullScreenImages.selectImage( $j(this).attr('href') );
			return false;
		});
		self.introCloseButton.click(function(){
			if (self.introOpen){
				self.intro.slideUp({ complete: function(){
					self.introCloseButton.hide();
					self.nav2.show();
					self.introOpen = false;
					self.e.animate({ height: '30px' });
				}});
			}
			return false;
		});
		$j('a:first',self.nav2).click(function(){
			if (!self.introOpen){
				self.e.css({ height: 'auto'});
				self.intro.slideDown();
				self.introCloseButton.show();
				self.nav2.hide();
				self.introOpen = true;
			}
			return false;
		});
		// Thumbnails
		$j('a:first',self.nav1).click(function(){
			if (!self.thumbsOpen){
				$j('.c-arrow-full-screen').hide();
				self.thumbs.slideDown();
				self.thumbsOpen = true;
			} else {
				self.thumbs.slideUp();
				self.thumbsOpen = false;
			}
			return false;
		});
		self.thumbsCloseButton.click(function(){
			if (self.thumbsOpen){
				$j('.c-arrow-full-screen').show();
				self.thumbs.slideUp();
				self.thumbsOpen = false;
			}
			return false;
		});
	},
	thumbsPosition : function(){
		var self = this;
		var thumbsTimer = window.setInterval(function() { self.thumbs.css({bottom: self.e.parent().innerHeight() }); },1);
	}
});
var closeContent = application.Class.extend({
	init: function(){
		var self = this;
		self.e = $j('#Content');
		self.content = $j('.content-body',self.e);
		self.contentOpen = true;
		self.contentCloseButton = $j('.close-button-content',self.e);
		self.nav = $j('.navigation-content-label',self.e);
		self.events();
	},
	events: function(){
		var self = this;
		self.contentCloseButton.click(function(){
			if (self.contentOpen) {
				self.content.slideUp({
					complete: function(){
						self.contentCloseButton.hide();
						self.nav.show();
						self.contentOpen = false;
						self.e.animate({
							height: '30px'
						});
					}
				});
			}
			return false;
		});
		$j('a:first', self.nav).click(function(){
			if (!self.contentOpen) {
				self.e.css({
					height: 'auto'
				});
				self.content.slideDown();
				self.contentCloseButton.show();
				self.nav.hide();
				self.contentOpen = true;
			}
			return false;
		});
	}
});


var footerPopup = application.Class.extend({
		
		init: function(){
			var self = this;
			self.e = $j('#Foot.desktop-only');
			self.inner = $j('.foot-content',self.e);
			self.open = false;
			self.render();
			self.events();
		},
		
		render : function(){
			var self = this;
			self.e.css({cursor: 'pointer'});
		
		},
		events : function(){
			var self = this;
		
				self.e.click(function(){
				self.inner.slideToggle();
			});

		}
});
var fullScreenImages = application.Class.extend({
	init : function(){
		var self = this;
		self.buttons = {
			both : $j('.c-arrow-full-screen'),
			prev : $j('.c-arrow-full-screen-prev'),
			next : $j('.c-arrow-full-screen-next')
		};
		self.backgroundImages = $j('.full-screen-image');
		if (typeof full_screen_images === 'undefined') {
			self.defImages = null;
		} else {
			self.defImages = full_screen_images;
		}
		self.currentImage = 0;
		self.treashedImage = self.currentImage;
		self.render();
		self.events();
	},
	render : function(){
		var self = this;
		self.scaleImages();
		self.backgroundImages.eq(0).show();
		if (self.backgroundImages.size() > 1 ){
			self.buttons.both.show();
			if (self.defImages) {
				self.backgroundImages.eq(1).attr('src', self.defImages[1]);
				self.backgroundImages.eq(self.defImages.length - 1).attr('src', self.defImages[self.defImages.length - 1]);
			}
		}		
	},
	events : function(){
		var self = this;
		// Scale images on window resize
		$j(window).resize(function(){	self.scaleImages(); self.positionArrows(); });
		// Previous/Next buttons
		self.buttons.prev.click(function(){ self.prevImage(); });
		self.buttons.next.click(function(){ self.nextImage(); });
		
		self.buttons.prev.hover(function(){ $j(this).addClass('hover')}, function(){ $j(this).removeClass('hover')});
		self.buttons.next.hover(function(){ $j(this).addClass('hover')}, function(){ $j(this).removeClass('hover')});
	},
	positionArrows : function(){
		var self = this;
		
		if ($j(window).width() < 650) {
			self.buttons.both.css({ top:200 });
		} else {
			self.buttons.both.css({ top: ($j(window).height() / 2) - 15 });
		}
	},
	selectImage : function(a){
		var self = this;
		a = parseInt(a,10);
		self.trashedImage = self.currentImage;
		if (self.currentImage !== a){
			self.currentImage = a;
			self.changeImages();
		}
	},
	scaleImages : function(){
		var self = this;

		$j(self.backgroundImages).each(function(){
			  var wWidth = $j(window).width();
		      var wHeight = $j(window).height();
		
		      var iWidth = 2000;
		      var iHeight = 1334;
		      
		      var wRatio = wHeight / wWidth;
		      var iRatio = iHeight / iWidth;
			  
			  var l = 0;
			  var h = 0;
			  var posType ="";
			  		
		      if (wWidth < 650) {
		      	//fixed image size for mobile screen 
		      	iHeight= 213;
		      	iWidth=320;
		      	h=105;
		      	posType ="absolute";
		      	
		      } else if (wRatio > iRatio) {
		          iHeight = wHeight;
		          iWidth = wHeight / iRatio;
		          var height = -Math.round((iHeight-wHeight)/2);
		          h = height + 77;
		          posType ="fixed";
		          
		      } else {
		          iHeight = wWidth * iRatio;
		          iWidth = wWidth;
		          var height = -Math.round((iHeight-wHeight)/2); 
		          h = height + 77;
		          posType ="fixed";
		      }
		      
		      l = -Math.round((iWidth-wWidth)/2);	
				
		      var cssObj = {
		          height: iHeight,
		          left: l,
		          position:posType,
		          top:h,
		          width: iWidth
		      };
		      $j(this).css(cssObj);
		});
	},
	prevImage : function(){
		var self = this;
		if (self.currentImage === 0){
			// Update current image
			self.trashedImage = self.currentImage;
			self.currentImage = self.backgroundImages.length - 1;
			// Change images
			self.changeImages();
		} else {
			// Update current image
			self.trashedImage = self.currentImage;
			self.currentImage = self.currentImage - 1;
			// Change images
			self.changeImages();
		}
	},
	nextImage : function(){
		var self = this;
		if (self.currentImage === (self.backgroundImages.length - 1)){
			// Update current image
			self.trashedImage = self.currentImage;
			self.currentImage = 0;
			// Change images
			self.changeImages();
		} else {
			// Update current image
			self.trashedImage = self.currentImage;
			self.currentImage = self.currentImage + 1;
			// Change images
			self.changeImages();
		}
	},
	changeImages : function(){
		var self = this;
		if (self.defImages) {
			var next = self.currentImage + 1;
			var prev = self.currentImage - 1;
			if (self.currentImage == 0) {
				var prev = self.defImages.length - 1;
			} else if (self.currentImage == (self.defImages.length - 1)) {
				var next = 0;	
			}
			self.backgroundImages.eq(prev).attr('src', self.defImages[prev]);
			self.backgroundImages.eq(next).attr('src', self.defImages[next]);
			
		}
		self.backgroundImages.stop(true,true);
		self.backgroundImages.eq( self.trashedImage ).animate({opacity: 'hide'}, 1000);
		self.backgroundImages.eq( self.currentImage ).animate({opacity: 'show'}, 1000);
	}
});
var friends = application.Class.extend({
	init: function(){
		var self = this;
		self.e  = $j('#Friends');
		self.links = $j('ul li a',self.e);
		self.friendDetails = $j('.friend-detail');
		self.container = $j('#FriendsDetailContainer');
		self.render();
		self.events();
	},
	render : function(){
		var self = this;
	},
	events : function(){
		var self = this;
		self.links.click(function(){
			self.link = $j(this);
			self.newPanel = $j( self.link.attr('href') );
			// Add and remove active state
			self.links.removeClass('a');
			self.link.addClass('a');
			// Check if any other panels are open and close them first
			if ( self.container.hasClass('open') === false ){
				self.showNewPanel();
				self.container.slideDown({
					duration: 200,
					easing: 'swing',
					complete: function(){
						self.container.addClass('open');
					}
				});
			} else {
				self.friendDetails.hide();
				self.showNewPanel();
			}
			return false;
		});
		$j('.close-button',self.container).click(function(){			
			
			self.links.removeClass('a');
			self.container.slideUp({
				duration: 200,
				easing: 'swing',
				complete: function() {
					$j('.friend-detail').hide();
				}
			}).removeClass('open');
		});
	},
	showNewPanel : function(){
		var self = this;
		self.newPanel.show();
	}
});
var pressGallery = application.Class.extend({
	init: function(){
		var self = this;
		
		self.e  = $j('#PressGallery');
		self.scroller = $j('.scrollable',self.e);
		self.scrollerItems = $j('ul li',self.scroller);
		self.buttons = {
			both : $j('.c-arrow-full-screen'),
			prev : $j('.c-arrow-full-screen-prev'),
			next : $j('.c-arrow-full-screen-next')
		};
				
		self.photos = [];
		for (i=0; i<press_albums.length; i++) {
			var album = press_albums[i];
			for (j=0; j<album.images.length; j++) {
				var photo = album.images[j];
				photo.album = album;
				self.photos.push(photo);
			}
		}
		
		self.render();
		self.events();
	},
	populate : function(url){
		var self = this;
		$j.getJSON(url, function(data){
			if (data) {
				$j('#PressItem-Title').html(data.title);
				$j('#PressItem-Detail').html(data.detail);
				$j('#PressItem-CoverShot').attr('src',data.cover_shot_image);
				var imagesHTML = '';
				$j(data.images).each(function(a,b){
					imagesHTML += '<li><img src="'+b.url+'" alt=""/></li>';
					//console.log(imagesHTML);
				});
				$j('#PressItem-ImagesList').html(imagesHTML);
				self.render();
				Cufon.refresh();
			}
		});
	},
	render : function(){
		var self = this;
		$j.easing.easeOutExpo = function (x, t, b, c, d) {
			return (t===d) ? b+c : c * (-Math.pow(2, -10 * t/d) + 1) + b;
		};
		self.scroller.scrollable({
			items: ".items",
			easing: "easeOutExpo",
			speed: 800,
			next: '.c-arrow-full-screen-next',
			prev: '.c-arrow-full-screen-prev',
			onSeek: function() {
				var api = $j(self.scroller).data("scrollable");
				var update = function(){
					var photo = self.photos[api.getIndex()];
					var album = photo.album;
					//console.log(album);
					$j('#PressItem-Title').html(album.title);
					$j('#PressItem-Detail').html(album.detail);
					$j('#PressItem-CoverShot').attr('src', album.cover_shot_image);
					$j('#PressItem-Caption').html(photo.caption);
				};
				$j(update);
			}
		});
		
		// causes problems in IE < 9
		//self.scroller.data("scrollable").begin(0);
		
		self.scrollerItems = $j('ul li',self.scroller);
		if (self.scrollerItems.size() > 1 ){
			// If more than one image show arrows
			self.buttons.both.show();
		} else {
			self.buttons.both.hide();
		}
	},
	events : function(){
		var self = this;
		$j('a.press-link').live('click',function(){
			/*$j('a.press-link').removeClass('a');
			$j(this).addClass('a');
			self.populate( $j(this).attr('href') );*/
			return false;
		});
	}
});
