ProfileSwap = Class.create();
Object.extend(ProfileSwap.prototype, AC.ContentSwap.prototype);
Object.extend(ProfileSwap.prototype, {
	cs1IsAnimating: false,
	cs2IsAnimating: false,
	lastSelectorIndex: false,

	
	initialize: function(selectorClass, contentClass, eventStr) {
		this.eventStr = eventStr;
		this.previousHeight = null;
		this.currentHeight = null;
		
		// get lists of selectors and content in order
		this.selectorList = document.getElementsByClassName(selectorClass);
		this.contentList = document.getElementsByClassName(contentClass);

		for(i=0; i<this.contentList.length; i++) {
			this.contentList[i].style.display = 'none';
		}

		this.setMouseover();
	},
	
	swapContent: function(selectorIndex) {
		
		if(this.cs1IsAnimating == false && this.cs2IsAnimating == false) {
			
			this.cs1IsAnimating = true;
			this.cs2IsAnimating = true;
			
			var selector = this.selectorList[selectorIndex];
			var content = this.contentList[selectorIndex];
			
			// add 'on' class
			if(!Element.hasClassName(selector, 'active')) Element.addClassName(selector, 'active');
			
			//  if this is the first one
			if(!this.lastSelectorIndex && typeof(this.lastSelectorIndex) == 'boolean') {
				if(!AC.Detector.isiPhone()) {
					this.afterFade(content);
				} else {
					
					content.style.display = block;
					
					this.resetFlag('cs1IsAnimating');
					this.resetFlag('cs2IsAnimating');
				}
			} else if (selectorIndex != this.lastSelectorIndex) {
				var lastSelector = this.selectorList[this.lastSelectorIndex];
				var lastContent = this.contentList[this.lastSelectorIndex];

				// remove the 'on' class
				if(Element.hasClassName(lastSelector, 'active')) Element.removeClassName(lastSelector, 'active');
				
				//swap
				if(!AC.Detector.isiPhone()) {
					new Effect.Fade(lastContent, { duration: .25, queue:{position:'end', scope:'cs1'},
						afterFinish: this.afterFade.bind(this, content)
					});
				} else {
					
					lastContent.style.display = 'none';
					content.style.display = 'block';
					
					this.resetFlag('cs1IsAnimating');
					this.resetFlag('cs2IsAnimating');
				}
			} else {
					this.resetFlag('cs1IsAnimating');
					this.resetFlag('cs2IsAnimating');
			}
			this.lastSelectorIndex = selectorIndex;
		}
		
		return false;
	},
	
	afterFade: function(content) {
		this.resetFlag('cs1IsAnimating');
		new Effect.Appear(content, { duration:.25, queue: { position:'end', scope:'cs2' },
			beforeStart:this.beforeAppear.bind(this, content),
			afterFinish:this.afterAppear.bind(this, content)
		});
	},
	
	beforeAppear: function(content) {
	},
	
	afterAppear: function(content) {
		this.previousHeight = content.getHeight();
		this.resetFlag('cs2IsAnimating');
	},
	
	resetFlag: function(flagName) {
		if(flagName == 'cs1IsAnimating') this.cs1IsAnimating = false;
		if(flagName == 'cs2IsAnimating') this.cs2IsAnimating = false;
	}
	
});

Profiles = Class.create();
Profiles.prototype = Object.extend(ShowProfiles.prototype, {
	initialize: function(sArchiveWrapper) {
		this.archiveWrapper = $(sArchiveWrapper);
		
		try {
			var request = new Ajax.Request(this.XmlUrl, { method: 'get', onSuccess: function(response) { this.handleRequest(response) }.bind(this) });
		} catch(e) { }
	},
	
	handleRequest: function(response) {
		// get the response
		this.xml = response.responseXML.getElementsByTagName('profiles')[0];

		// store the results as an array of objects in this.results
		this.results = [];

		var xmlResults = this.xml.getElementsByTagName('item');

		for(i=0; i<xmlResults.length; i++) {
			var result = xmlResults[i];
			var category = (result.getElementsByTagName('category')[0]) ? result.getElementsByTagName('category')[0].firstChild.nodeValue : false;
			category = (category) ? category.split(',') : false;	
			var date = (result.getElementsByTagName('date')[0]) ? result.getElementsByTagName('date')[0].firstChild.nodeValue : false;
			var result = {
				title: (result.getElementsByTagName('title')[0]) ? result.getElementsByTagName('title')[0].firstChild.nodeValue : false,
				category: (category) ? (category.length>1) ? category : category[0] : false,
				date: (date) ? date.split('/') : false,
				description: (result.getElementsByTagName('description')[0]) ? result.getElementsByTagName('description')[0].firstChild.nodeValue : false,
				url: (result.getElementsByTagName('url')[0]) ? result.getElementsByTagName('url')[0].firstChild.nodeValue : false,
				thumb: (result.getElementsByTagName('thumb')[0]) ? result.getElementsByTagName('thumb')[0].firstChild.nodeValue : false
			};
			this.results.push(result);
		}

		this.parseResults();
		this.clearDivs();
		var swapper = new ProfileSwap('swapnav', 'swapcategory', 'click');
		swapper.swapContent(0);
	},

	parseResults:function() {
		this.results.sort(this.sortByDate);
		var tempDiv = document.createElement('div');
		for (category in this.categories) {
			this.archiveWrapper.appendChild(Builder.node('div', {'class':'swapcategory', id: category}));
		}
		
		
		this.results.each(function(result, i) {
			if(result.date) result.date = result.date[1]+'/'+result.date[2]+'/'+result.date[0].substr(2);
			var multipleCategories = typeof(result.category) != 'string';
			var div = document.createElement('div');
			$(div).addClassName('profile');
			
			var tProfile = new Template(						
				'<a href="#{url}"><img src="#{thumb}" height="52" width="52" alt="icon"><\/a>'+
				'<h4><a href="#{url}">#{title}<\/a><small>#{date}<\/small><\/h4>'+
				'<p>#{description} <a class="more" href="#{url}">Learn More<\/a><\/p>'
			);
			div.innerHTML += (tProfile.evaluate(result));
			$('all').appendChild(div.cloneNode(true));
			if(multipleCategories) {
				result.category.each(function(cat, i) {
					$(cat).appendChild(div.cloneNode(true));
				});
			} else {
				$(result.category).appendChild(div.cloneNode(true));
			}
		});
	},

	clearDivs:function() {
		var categories = this.archiveWrapper.getElementsBySelector('.swapcategory');
		categories.each(function(category) {
			var divs = category.getElementsBySelector('.profile');
			divs.each(function(div, i) {
				if (i%2 == 0) div.addClassName('clear')
			});
		});
	}			
});

