var ReferencePopUp = (function(){
	function constructor(){
		this.currentReferences = null;
		this.bg;
		this.content;
		this.list;
		this.findReferences();
	}
	
	constructor.prototype.findReferences = function(){
		$('a.ref').live('click',(function(e){return function(){e.loadReferences(this);return false;}})(this));
	}
	
	constructor.prototype.sizeBoxen = function(){
		if(this.content.height() > $(window).height()){
			this.content.css('top',50+$(document).scrollTop()+'px');											
		} else {
			this.content.css('top',($(window).height()-this.content.height())/2+$(document).scrollTop()+'px');	
		}
	}
	
	constructor.prototype.loadReferences = function(ref){
		//load references!
		this.currentReferences = $(ref);
		if(!this.content){
			$.ajax({
				   url: '/includes/references-popup.html',
				   success : (function(e){return function(data){e.buildPopup(data);}})(this),
				   dataType : 'html'
			});
		} else {
			this.clearHighlights();
			this.displayBG();	
		}
		
	}
	
	constructor.prototype.buildPopup = function(content){
		this.content = $(content);
		this.bg = $('<div id="references-popup-bg"></div>');
		this.bg.hide();
		this.bg.css('display','none');//wtf safari
		this.bg.height($(document).height());
		this.content.hide();
		$('body').prepend(this.content).prepend(this.bg);
		this.bg.click((function(e){return function(){e.remove();}})(this));
		$('#references-box,#reference-cancel-btn,#reference-close-btn').click((function(e){return function(){e.remove();}})(this));
		$('#references-container').click(function(evt){evt.stopPropagation();})
		this.displayBG();
	}
	
	constructor.prototype.displayBG = function(){
		this.sizeBoxen();
		/*
		if(jQuery.browser.version <=6 && jQuery.browser.msie){
			this.bg.stop().fadeTo(400,.75,"easeInQuint");
		}else{
			this.bg.stop().fadeIn(400,"easeInQuint");
		}
		*/
		// 8-11-11
		this.bg.stop().fadeTo(400,.75,"easeInQuint");
		this.content.stop().fadeIn(300,"easeInQuint",(function(e){return function(){e.highlightReferences();}})(this));
	}
	
	constructor.prototype.displayBox = function(){
		
	}
	
	constructor.prototype.highlightReferences = function(){
		this.list = this.currentReferences.text();
		this.list = this.list.replace(/[a-z],?/g,"");
		this.list = this.list.replace(/,$/,"");
		var groups = this.list.match(/\d+-\d+/g);
		for(group in groups){
			var insert = "";
			for(var i = ~~groups[group].match(/^\d+/);i<=~~groups[group].match(/\d+$/);i++){
				insert += i+",";
			}
			this.list = this.list.replace(new RegExp(groups[group]+",?"),insert);
		}
		this.list = this.list.replace(/,$/,"");
		if(this.list.length){
			this.list = this.list.split(',');
			this.highlight();
			/*
			$.each(this.list.split(','),function(i,v){			 
				$($('#ref-list li').get(v-1)).addClass('active');
				$({'bgopacity':0}).animate({'bgopacity':1},{//if broswer supports rgba
					duration:700,
					easing:"easeInQuint",
					step:(function(e){return function(){e.css('background-color','rgba(248,217,180,'+parseFloat(this.bgopacity)+')');}})($($('#ref-list li').get(v-1)))
				});
				
			});
			*/
		}
	}
	// RGBtoHex algorithm function from http://www.javascripter.net/faq/rgbtohex.htm
	function rgbToHex(R,G,B) {return toHex(R)+toHex(G)+toHex(B)}function toHex(n) {n = parseInt(n,10);if (isNaN(n)) return "00";n = Math.max(0,Math.min(n,255));return "0123456789ABCDEF".charAt((n-n%16)/16)+ "0123456789ABCDEF".charAt(n%16);}
	// added 8-11-11 -ML
	constructor.prototype.highlight = function() {
		var a = this.list;
		if(typeof a == 'object' && a.length > 0) {
			for(i in a) {
				toOrange($($('#ref-list li')[a[i]-1]));
			}
		}
	}
	// ease to orange
	var toOrange = function(o) {
		for(i=0,r=246,g=246,b=246;b>=180;--b,r=(r+.03),g=(g-.439),++i) {
			(function(_r,_g,_b,_i) {
				setTimeout(function() {
					o.css("background","#"+rgbToHex(_r,_g,_b));
				},i*5);
			})(r,g,b,i);
		}
	}
	
	constructor.prototype.remove = function(){
		this.content.stop().fadeOut(150,"easeOutQuint");
		this.bg.stop().fadeOut(200,"easeOutQuint",(function(e){return function(){e.destroy();}})(this));
	}
	
	constructor.prototype.clearHighlights = function(){
		$('#ref-list li').removeClass('active');
		$('#ref-list li').css('background-color','transparent');
	}
	
	constructor.prototype.destroy = function(){
		this.bg.hide();
		this.content.hide();
		this.clearHighlights();
	}
					 
	return constructor;
})();
