var area = {
	setGlobals: function() {
		area.collectionLinkHTML = '<a href="#" class="area-collection-link">view collection</a>';
		area.collectionLinkFlag = 0;
		area.areaPhoto = brizo.areaPhoto.find('img');
		// Account for URL ending in slash rather than index.html
		area.thisUrl = function(){
				var $url = jQuery.url.attr("relative");
				if ($url.indexOf('html') != -1) {
					return $url.replace('html', 'xml');
				} else {
					return $url + 'index.xml';
				}
			}();
		},
	
	addCollectionLink: function() {
		brizo.contentBody.append(area.collectionLinkHTML);
		area.areaPhoto.wrap('<a href="#" class="area-photo-link"></a>');
		area.collectionLinkFlag = 1;
	},
	
	changeCollectionLink: function(collName) {
		if(area.collectionLinkFlag === 0) { area.addCollectionLink(); }
		var cl = jQuery('a.area-collection-link');
		var pl = jQuery('a.area-photo-link');
		var newUrl = '/'+ jQuery.url.segment(0) +'/collection/'+ collName +'.html';
		cl.attr('id', 'link-to-'+ collName)
			.attr('href', newUrl)
			.text('view '+ collName.replace('-', ' ').toUpperCase() +' collection');
		pl.attr('href', newUrl);
	},
	
	changeCollection: function(newSrc, collName) {
		brizo.changePhoto(area.areaPhoto, newSrc);
		area.changeCollectionLink(collName);
	},
	
	parseXmlThumbs: function() {
		var thumbArray = [];
		jQuery.get(area.thisUrl, function(data){
			jQuery(data).find('thumb').each(function(){
				thumbArray.push(jQuery(this).attr(img).replace('thumb', 'large'));
			});
		});
	},
	
	setFirstCollection: function() {
		var newData = '';
		//var thisUrl = jQuery.url.attr("relative").replace('html', 'xml');
		jQuery.get(area.thisUrl, function(data){
			newF = jQuery(data).find('thumb:first').attr('link').replace('javascript:', '').replace(';', '');
			eval(newF);
		});
	},
	
	preloadImages: function() {
		var _xmlImages = [];
		jQuery.get(area.thisUrl, function(data){
			jQuery(data).find('thumb').each(function(){
				_xmlImages.push(jQuery(this).attr('img').replace('thumb', 'large'));
			});
			brizo.preloadImages(_xmlImages);
		});
	}
};

// jQuery(data).find('thumb:first').attr('link').replace('javascript:', '');

// Initialize JS functions
$(document).ready(function(){
	area.setGlobals();
	area.setFirstCollection();
	area.preloadImages();
});