Article = function () {
	Article.prototype.fragment		= FragmentParser;
	Article.prototype._ 			= function (text) { return Lang(text) };

	var that = this;
	var gallery = null;
	this.loadingPlaceholder = '<div class="article_single shadow" style="text-align:center;"><img src="/live/wp-content/themes/bon/images/load_spin.gif"><div>';
	
	this.init = function () {
		console.log('Article::init()');
		that.update();
		
		// SET UP LISTENERS
		//$(window).unbind('.ARTICLE');
		$(window).bind('closeAllWindows.BON', that.closeArticle);
		//$(window).bind('hashchange.ARTICLE', that.updateArticle);
		$(window).bind('closeSection.ARTICLE', that.closeArticle);
		$(window).bind('update.ARTICLE', that.update);
	}
	
	this.renderArticle = function (_link) {
		// RENDER ARTICLE PAGE
		$('.article_container_wrap').show();
		$('.article_container').html(that.loadingPlaceholder);
		$('.article_container').load( decodeURIComponent(_link)+' #article', that.articleLoadComplete );
		//ON IPAD, HIDE THE TWITTER FEED
		if($('html').hasClass('ipad')) {
			$('.twtr-tweets').hide();
		}
		// STOP POSTER
		$('.poster_container').trigger('posterAnimStop.LANDING');
		
		// TRIGGER SCROLL UP
		$(window).trigger(Events.Bon.SCROLL_TO_TOP);
	}
	
	this.articleLoadComplete = function () {
		$('.article_container').prepend('<a href="#" class="close" style="color:#000;">X '+that._('close')+'</a>');
		$('.article_container').append('<a href="#" class="close" style="color:#000; top:auto; bottom:8px;">X '+that._('close')+'</a>');
		// MODIFY INTERNAL LINKS FOR REFERENCE
		//$('.main_body a').each(that.updateInternalLinks);
		// IN CASE IS AUTHOR PAGE, USE MASONRY
		$('.author_post_list').masonry({
			itemSelector:'#article .blog_item'
		});
		// IN CASE THERE IS A MINIGALLERY
		$('.article_gallery').each(that.initGallery);
		//AJAX THE COMMENT FORM
		that.initCommentsForm();
		//ANALYTICS
		_gaq.push(['_trackEvent', 'Articles', 'Load', location.hash]);
	}
	
	this.initGallery = function () {
		//console.log('Article::initGallery', this);
		var type	= $(this).find('input[name=gallery_type]').val();
		var id		= $(this).find('input[name=gallery_id]').val();
		that.gallery_link = false;
		that.gallery_link = $(this).find('input[name=gallery_link]').val();
		//console.log('Article::initGallery -> that.gallery_link', that.gallery_link);
		$.getJSON('/data?json&'+type+'='+id, function (data) { that.renderGallery(data); });
		//$(this).bind('nextImage.ARTICLE', that.nextImage());
		//$(this).bind('prevImage.ARTICLE', that.prevImage());
		$(this).bind('updateMiniGallery.ARTICLE', that.updateMiniGallery);
	}
	
	this.renderGallery = function (data) {
		//console.log('Article::renderGallery', data);
		that.gallery = data.mini_gallery.contents;
		that.current = 0;
		$('.image_total').html(that.gallery.length);
		$('.next_image').click(that.nextImage);
		$('.previous_image').click(that.prevImage);
		if(that.gallery_link){
			$('.gallery_link').show();
			$('.gallery_link').find('a').attr('href', that.gallery_link);
		} else {
			$('.gallery_link').remove();
		}
		$('.article_gallery').trigger('updateMiniGallery.ARTICLE');
	}
	
	this.nextImage = function (e) {
		that.navImage(1);
	}
	
	this.prevImage = function (e) {
		that.navImage(-1);
	}
	
	this.navImage = function (val) {
		if(val < 0){
			if(that.current > 0 ){
				that.current -= 1;
			}
		} else if (val > 0) {
			if(that.current < that.gallery.length-1){
				that.current += 1;
			}
		}
		//console.log('Article::navImage()', val, that.current);
		$('.article_gallery').trigger('updateMiniGallery.ARTICLE');
	}
	
	this.updateMiniGallery = function () {
		//console.log('Article::updateMiniGallery()', that.current, that.gallery[that.current]);
		var data = that.gallery[that.current];
		$('.display_area').html('<img src="'+data.imageURL+'" />');
		$('.current_image_count').html(that.current+1);
	}
	
	this.update = function () {
		var frag = that.fragment().get();
		console.log('Article::update()', frag);
		that.renderArticle(frag.link);
	}
	
	this.initCommentsForm = function () {
		var opt = {
			beforeSubmit: that.beforeSubmitForm,
			success: that.successSubmitForm,
			iframe: true
		};
		$('#commentform').ajaxForm(opt);
	}
	
	this.beforeSubmitForm = function (arr, form, options) {
		console.log('Article::beforeSubmitForm()', arr, form, options);
		//return false;
	}
	
	this.successSubmitForm = function (responseText, statusText, xhr, form) {
		//var res = $(responseText);
		//console.log('Article::successSubmitForm()', statusText, xhr, form, res);
		location.reload(true);
	}
	
	this.closeArticle = function () {
		console.log('Article::closeArticle()');
		$('.article_container_wrap').hide();
		$('.article_container').html(that.loadingPlaceholder);
		//ON IPAD, REENABLE THE TWITTER FEED
		if($('html').hasClass('ipad')) {
			$('.twtr-tweets').show();
		}
		$('.poster_container').trigger('posterAnimPlay.LANDING');
		//$(window).unbind('.ARTICLE'); 
	}
	
	this.updateInternalLinks = function () {
		//console.log('Article::updateInternalLinks', this);
		var href = $(this).attr('href');
		if(href.slice(0,1) == '#'){
			//console.log('internal link detected');
		}
	}
	
	
	this.init();
	return this;
}
