var NL_DSGIRLS_WP_UTILS = {
	resizePhoto: function(image, maxwidth, maxHeight){
		var currentWidth, currentHeight;
		
		currentWidth = image.width;
		currentHeight = image.height;
		
		if (currentWidth > maxwidth || currentHeight > maxHeight) {
			if (currentWidth > maxwidth) {
				currentHeight = Math.floor(currentHeight * (maxwidth / currentWidth));
				currentWidth = maxwidth;
			}
			
			if (currentHeight > maxHeight) {
				currentWidth = Math.floor(currentWidth * (maxHeight / currentHeight));
				currentHeight = maxHeight;
			}
		}
		
		image.width = currentWidth;
		image.height = currentHeight;
		
		YAHOO.util.Dom.setStyle('rightsidebar', 'height', 'auto');
		YAHOO.util.Dom.setStyle('leftsidebar', 'height', 'auto');
		WORDPRESS_DS_UTILS.adjustHeight();
	}
};

var NL_DSGIRLS_WP_AGENDA = {
	names: null,
	
	resizePhoto: function(image, maxwidth, maxHeight) {
		NL_DSGIRLS_WP_UTILS.resizePhoto(image, maxwidth, maxHeight);
				
		image.parentNode.style.width = image.width + 2 + 'px';
		image.parentNode.style.visibility = 'visible';
		image.parentNode.style.position = 'relative';
	},
		
	initTooltips: function() {
		var elements = YAHOO.util.Dom.getElementsByClassName('name-marker');
		var tips = new YAHOO.widget.Tooltip('tooltip', {
				context: elements,
				autodismissdelay: 60000,
				effect: {
					effect: YAHOO.widget.ContainerEffect.FADE,
					duration: 0.20
				}
		});
		
		tips.contextTriggerEvent.subscribe(NL_DSGIRLS_WP_AGENDA.showTip);
	},
	
	showTip: function(type, args) {
		var context = args[0];
		var name = context.innerHTML;
		
		for (var i = 0; i < NL_DSGIRLS_WP_AGENDA.names.length; i++) {
			if (NL_DSGIRLS_WP_AGENDA.names[i].name === name) {
				text = NL_DSGIRLS_WP_AGENDA.names[i].time;
				break;	
			}
		}
		
		this.cfg.setProperty('text', text);
	}
};

var NL_DSGIRLS_WP_PHOTOALBUM = {
	popup: null,
	dsWpUrl: null,
	currentIndex: 0,
	urlImages: null,
	urlThumbs: null,
	
	init: function() {
		var self = NL_DSGIRLS_WP_PHOTOALBUM;
		 
		self.popup = new YAHOO.widget.Panel('ds-wp-girl-popup',  
			{width: '900px',
			 height: '500px', 
  			 fixedcenter: true, 
	  		 close: true, 
			 draggable: false, 
	  		 zindex: 4,
	  		 modal: true,
	  		 visible: false,
			 effect:{effect: YAHOO.widget.ContainerEffect.FADE, duration: .5}
			});
				
		YAHOO.util.Event.addListener('ds-wp-girl-popup-up', 'click', self.previousPhoto);
		YAHOO.util.Event.addListener('ds-wp-girl-popup-down', 'click', self.nextPhoto);
	},
	
	nextPhoto: function() {
		var self = NL_DSGIRLS_WP_PHOTOALBUM;
		var selected = YAHOO.util.Dom.get('index-' + self.currentIndex).parentNode;
		if (selected && YAHOO.util.Dom.getNextSibling(selected)) {
			var thumb = YAHOO.util.Dom.getNextSibling(selected);
			var image = YAHOO.util.Dom.getFirstChild(thumb);
			self.updatePolaroidImage(image);
			
			var currentTop = 5 * -85 * (Math.floor(self.currentIndex / 5));
			YAHOO.util.Dom.setStyle('ds-wp-girl-popup-details-thumbs-wrapper', 'top', currentTop + 'px');
		}
	},
	
	previousPhoto: function() {
		var self = NL_DSGIRLS_WP_PHOTOALBUM;
		var selected = YAHOO.util.Dom.get('index-' + self.currentIndex).parentNode;
		if (selected && YAHOO.util.Dom.getPreviousSibling(selected)) {
			var thumb = YAHOO.util.Dom.getPreviousSibling(selected);
			var image = YAHOO.util.Dom.getFirstChild(thumb);
			self.updatePolaroidImage(image);
			
			var currentTop = 5 * -85 * (Math.floor(self.currentIndex / 5));
			YAHOO.util.Dom.setStyle('ds-wp-girl-popup-details-thumbs-wrapper', 'top', currentTop + 'px');
		}
	},
	
	showDetails: function(name) {
		var self = NL_DSGIRLS_WP_PHOTOALBUM;

		YAHOO.util.Dom.setStyle('ds-wp-girl-popup-loading', 'display', 'inline');
		YAHOO.util.Dom.setStyle('ds-wp-girl-popup-details-wrapper', 'display', 'none');

		self.popup.render(document.body);
		self.popup.show();
				
		var sUrl = self.dsWpUrl + 'getDetails.php?name=' + name;
		YAHOO.util.Connect.asyncRequest('GET', sUrl, self.detailsCallback); 
	},
	
	detailsCallback: {
		success: function(o) {
			var self = NL_DSGIRLS_WP_PHOTOALBUM;
	
			YAHOO.util.Dom.setStyle('ds-wp-girl-popup-loading', 'display', 'none');
			YAHOO.util.Dom.setStyle('ds-wp-girl-popup-details-wrapper', 'display', 'block');
			
			if (o.responseText) {
				var response = YAHOO.lang.JSON.parse(o.responseText);
			
				if (response.dsGirl) {
					var dsGirl = response.dsGirl;
					YAHOO.util.Dom.get('ds-wp-girl-popup-details-name').innerHTML = dsGirl.name;
					YAHOO.util.Dom.get('ds-wp-girl-popup-details-description').innerHTML = self.convertLineBreak(self.getValue(dsGirl.shortDescription));
					YAHOO.util.Dom.get('ds-wp-girl-popup-details-extra').innerHTML = self.convertLineBreak(self.getValue(dsGirl.extraInfo));
					
					YAHOO.util.Dom.get('ds-wp-girl-popup-details-stature').innerHTML = self.getValue(dsGirl.looksStature);
					YAHOO.util.Dom.get('ds-wp-girl-popup-details-length').innerHTML = self.getValue(dsGirl.looksLength);
					YAHOO.util.Dom.get('ds-wp-girl-popup-details-hair').innerHTML = self.getValue(dsGirl.looksHairColor);
					YAHOO.util.Dom.get('ds-wp-girl-popup-details-eyes').innerHTML = self.getValue(dsGirl.looksEyesColor);
					YAHOO.util.Dom.get('ds-wp-girl-popup-details-cup').innerHTML = self.getValue(dsGirl.looksCupSize).toUpperCase();
					YAHOO.util.Dom.get('ds-wp-girl-popup-details-nationality').innerHTML = self.getValue(dsGirl.looksNationality);
				
					YAHOO.util.Dom.get('ds-wp-girl-popup-details-thumbs-wrapper').innerHTML = '';
					YAHOO.util.Dom.setStyle('ds-wp-girl-popup-details-thumbs-wrapper', 'top', '0px');

					if (response.photos.length > 0) {
						for (var i = 0; i < response.photos.length; i++) {
							self.addThumb(response.photos[i], i);
						}
						
						self.setPolaroid(self.urlImages + response.photos[0]);
						self.currentIndex = 0;
						
						YAHOO.util.Dom.setStyle('ds-wp-girl-popup-up', 'display', 'block');
						YAHOO.util.Dom.setStyle('ds-wp-girl-popup-down', 'display', 'block');
					}
					else {
						self.setPolaroid(self.dsWpUrl + 'static/images/no-picture.jpg');
						YAHOO.util.Dom.setStyle('ds-wp-girl-popup-up', 'display', 'none');
						YAHOO.util.Dom.setStyle('ds-wp-girl-popup-down', 'display', 'none');
					}
					
				} else {
					self.popup.hide();
				} 
			}
		},
	
		failure: function() {
			NL_DSGIRLS_WP_PHOTOALBUM.popup.hide();
		}
	},
	
	addThumb: function(fileName, index) {
		var self = NL_DSGIRLS_WP_PHOTOALBUM;
		var thumbWrapper = document.createElement('div');
		YAHOO.util.Dom.addClass(thumbWrapper, 'ds-wp-girl-popup-details-thumb-wrapper');
		
		var thumbImage = document.createElement('img');
		thumbImage.id = 'index-' + index;
		thumbWrapper.appendChild(thumbImage);
		YAHOO.util.Dom.get('ds-wp-girl-popup-details-thumbs-wrapper').appendChild(thumbWrapper);						

		var newImage = new Image();
		newImage.onload = function() {
			self.resizeThumb(this, 436, 448);
			
			thumbImage.src = self.urlThumbs + fileName;
			YAHOO.util.Dom.setStyle(thumbImage, 'width', this.width + 'px');
			YAHOO.util.Dom.setStyle(thumbImage, 'height', this.height + 'px');
			if (index > 0) {
				YAHOO.util.Dom.setStyle(thumbImage, 'opacity', '0.5');	
			}
			YAHOO.util.Event.addListener(thumbWrapper, 'click', self.updatePolaroid); 
			YAHOO.util.Event.addListener(thumbImage, 'mouseover', self.highlightThumb); 
			YAHOO.util.Event.addListener(thumbImage, 'mouseout', self.unHighlightThumb); 

		};
		newImage.src = self.urlThumbs + fileName;
	},
	
	highlightThumb: function() {
		YAHOO.util.Dom.setStyle(this, 'opacity', '0.8');
	},
	
	unHighlightThumb: function() {
		var self = NL_DSGIRLS_WP_PHOTOALBUM;

		if (self.getIndex(this.id) !== self.currentIndex) {
			YAHOO.util.Dom.setStyle(this, 'opacity', '0.5');	
		} else {
			YAHOO.util.Dom.setStyle(this, 'opacity', '1');
		}
	},
	
	updatePolaroid: function() {
		var self = NL_DSGIRLS_WP_PHOTOALBUM;

		var image = YAHOO.util.Dom.getFirstChild(this);
		self.updatePolaroidImage(image);
	},
	
	updatePolaroidImage: function(image) {
		var self = NL_DSGIRLS_WP_PHOTOALBUM;

		YAHOO.util.Dom.setStyle('index-' + self.currentIndex, 'opacity', '0.5');
		YAHOO.util.Dom.setStyle(image, 'opacity', '1');
		self.currentIndex = self.getIndex(image.id);
		var imageFilename = image.src.replace(self.urlThumbs, self.urlImages);
		self.setPolaroid(imageFilename);
	},
	
	setPolaroid: function(url) {
		var self = NL_DSGIRLS_WP_PHOTOALBUM;

		YAHOO.util.Dom.setStyle('ds-wp-girl-popup-polaroid-wrapper', 'display', 'none'); 

		var polaroidImage = YAHOO.util.Dom.get('ds-wp-girl-popup-polaroid-image');
		polaroidImage.src = url;

		var newImage = new Image();
		newImage.onload = function() {
			NL_DSGIRLS_WP_UTILS.resizePhoto(this, 436, 476);
			YAHOO.util.Dom.setStyle('ds-wp-girl-popup-polaroid-wrapper', 'width', (this.width + 2) + 'px');
			YAHOO.util.Dom.setStyle('ds-wp-girl-popup-polaroid-wrapper', 'margin-top', ((476 - this.height) / 2) + 'px'); 
			YAHOO.util.Dom.setStyle('ds-wp-girl-popup-polaroid-image', 'width', this.width + 'px');
			YAHOO.util.Dom.setStyle('ds-wp-girl-popup-polaroid-image', 'height', this.height + 'px');
			YAHOO.util.Dom.setStyle('ds-wp-girl-popup-polaroid-wrapper', 'display', 'block'); 
		};
		newImage.src = url;
	},

	resizeThumb : function(image) {
		var currentWidth, currentHeight;
		
		maxwidth = 80;
		maxHeight = 80;
		
		currentWidth = image.width;
		currentHeight = image.height;

		if (currentWidth > currentHeight) {
			currentWidth = Math.floor(currentWidth * (maxHeight / currentHeight));
			currentHeight = maxHeight;
		} else {
			currentHeight = Math.floor(currentHeight * (maxwidth / currentWidth));
			currentWidth = maxwidth;
		}
		
		image.width = currentWidth;
		image.height = currentHeight;
	},
	
	convertLineBreak: function(text) {
		var result = '';
		if (text) {
			result = text.replace(/(\r\n|\r|\n)/g, '<br/>');
		}
		return result;
	},
	
	getValue: function(text) {
		var result = '';
		if (text !== undefined && text !== null) {
			result = text.substr(0, 1).toUpperCase() + text.substr(1);;
		}
		return result;
	},
		
	getIndex: function(id) {
		return parseInt(id.substring(6));
	}
};