/**
 * Principles JavaScript
 * 
 * @author Ollie Maitland
 * @copyright Byng Systems LLP
 * 
 */
 
 
 var Principles = new Class ({
 	
 	initialize : function () {

 		this.panel = $('principles');
 		this.current = this.getChild ( 0 );
		this.articleLoaded = false;
		
		// remove the page links
		$ES('a.link', $('news_articles')).each (
					function (a, index) {
							a.set ({'href' : '#'});						
					});
 	},
 	
 	getChild : function ( seq ) {
 		
 		return this.panel.getChildren()[ seq ];
 	},
 	
 	load : function ( seq ) {
 		
 		var toOpen = this.getChild ( seq );
 		if (toOpen == this.current) return;
 		this.closePane ( this.current );
 		this.current = toOpen;
 		this.current.getChildren().filterByTag('div').each(this.slideIn);
 		this.current.getChildren().filterByClass('row').each(function(item){item.addClass('selected');}); 		
 	},
 	
 	closePane : function ( row ) {
 		
 		row.getChildren().filterByClass('row').each(function(item){item.removeClass('selected');});
 		row.getChildren().filterByTag('div').each(this.slideOut);
 		
 	} ,
 	
 	slideIn : function ( el ) {
 		
		var slide = new Fx.Style(el, 'height', {duration: 300, unit: 'em', fps: 35});
			slide.set(0);
			el.setStyle('display','block');			
			el.setOpacity(0);
			slide.start(0,3);

		var slide2 = new Fx.Style(el, 'opacity', {duration: 300, wait: false});
			slide2.start(0,1); 
 	},
 	
 	slideOut : function ( el ) {

		var slide = new Fx.Style(el, 'height', {duration: 300, unit: 'em', fps: 35});
			slide.start(3,0);
						
		var slide2 = new Fx.Style(el, 'opacity', {duration: 300, wait: false, onComplete: function (el) { el.setStyle('display','none'); }});
			slide2.start(1,0);

 	},

 	fetchArticle : function ( id ) {
 		
 		if (this.articleLoaded == true) {
 			if (this.articleLoaded == id) {
 				return;
 			} else {
	 			this.switchArticle();
 			}
 		}
 		
 		var fadeOut = new Fx.Style(this.panel, 'opacity').start(1,0);
 		
		// hide the loader
		ByngUI.dom.hideLoader();
		 		
 		// show the loading symbol
 		ByngUI.dom.showLoader( $('principles_flash'), '/html/images/icons/loading.gif' );
 		
		var request = new ByngRequest(false, 'news', 'ugc', 'ajax');
			request.addParam ('articleId', id);
			request.setAction ('retrieve_article');
			
		var article = new XmlPost ();
			//article.debug = true;	
			article.fromRequest (request);
			article.fetch (Principles.loadArticle);
			
		// set the article loading
		this.articleLoaded = id

 		 		
 	},
 	
 	loadArticle : function ( xml ) {

 		var resp = new ByngXml (xml);
 		
		if (resp.getCode() == 0) {
 			var fadeOut = new Fx.Style(Principles.panel, 'opacity', { wait: false } ).start(0,1);
 			return;
 		}

 		var body = resp.getBody();
  		
 		// check the response coming back it the correct article
		var articleId = body.getChildTag('article').getAttribute("articleId");
		if (articleId != Principles.articleLoaded) {
			return;
		}
 		
		Principles.panel.setStyle('display','none');
		
		// hide the loader
		ByngUI.dom.hideLoader();

 		if (Principles.article) {
 			// update existing DOM
			$E('dt', $E('a', Principles.article).set({'href': body.getChildContent("permLink")}).setHTML(body.getChildContent("heading")));
			$E('dd', Principles.article).setHTML(body.getChildContent("content"));
			if (Principles.article.getStyle('opacity') == 0) {
				var fadeIn = new Fx.Style(Principles.article, 'opacity', {duration: 300, wait: true} ).start(0,1);
			}
		} else {
			// create the DOM
			Principles.article = new Element('dl');
			var dt = new Element('dt');
				dt.appendChild(new Element('a', {'href': body.getChildContent("permLink")}).addClass('selected').setHTML(body.getChildContent("heading")));
			Principles.article.appendChild(dt);
			Principles.article.appendChild(new Element('dd').setHTML(body.getChildContent("content")));
			Principles.article.appendChild(new Element('dd').setHTML('<a href="javascript:Principles.closeArticle();">back</a>').addClass('footer'));
			$('principles_flash').appendChild (Principles.article);
			
		}
 	},
 	
 	switchArticle : function () {
 		
		var fadeOut = new Fx.Style(this.article, 'opacity', {duration: 300, wait: true} ).start(1,0);	
 		this.articleLoaded = false;	
 	},
 	
 	closeArticle : function ( replacePanel ) {

		// hide the loader if already showing
 		ByngUI.dom.hideLoader();
 		 
 		var fadeOut = new Fx.Style(this.article, 'opacity', {duration: 300, onComplete: function(el) { el.remove(); Principles.article = null; }}).start(1,0);	 
		this.article.setStyle('display','none');

	 	this.panel.setStyle('display','block');
 		var fadeIn = new Fx.Style(this.panel, 'opacity', {duration: 500}).start(0,1);
 		
 		this.articleLoaded = false;
 	}
 	
 	
 	
 });