// If user doesn't have Firebug, create empty functions for the console to avoid errors.
if (!window.console || !console.firebug){
	var names = ["log", "debug", "info", "warn", "error", "assert", "dir", "dirxml",
	"group", "groupEnd", "time", "timeEnd", "count", "trace", "profile", "profileEnd"];
	window.console = {};
	for (var i = 0; i < names.length; ++i)
		window.console[names[i]] = function() {}
}

/****navigation effect*******************************************************/
window.navappear = {
	appearText: function(){
		// navappear.navHover();
		var timer = 1;
		var navbits = $$('#nav li a');
		
		var slidefxs = [];
		
		navbits.each(function(el, i){
			// el.setStyle('margin-top', '-27px');
			slidefxs[i] = new Fx.Tween(el, {
				duration: 200,
				// transition: Fx.Transitions.backOut,
				wait: false// ,
				// onComplete: this.createOver.pass([el, i])
			});
			
			el.addEvent('mouseenter', function(){
				slidefxs[i].start('padding-top', '15px');
			});
			el.addEvent('mouseleave', function(){
				slidefxs[i].start('padding-top', '5px');
			});
			
			// slidefxs[i].start('margin-top', -27, -5); //.delay(timer, null, ['margin-top', -27, -5]);
			
			timer += 150;
		}.bind(this));
	}// ,
	// 	
	// 	createOver: function(){
	// 		el.addEvent('mouseenter', function(){
	// 			window.slidefxs[i].start('padding-top', '15px');
	// 		});
	// 	
	// 		el.addEvent('mouseleave', function(){
	// 			window.slidefxs[i].start('padding-top', '5px');
	// 		});
	// 	},
	// 	
	// 	navHover: function(){
	// 		var list = $$('#nav li a');
	// 		list.each(function(element) {
	// 		 
	// 			var fx = new Fx.Styles(element, {duration:200, wait:false});
	// 		 
	// 			element.addEvent('mouseenter', function(){
	// 				fx.start({
	// 					'padding-top':'15px'
	// 				});
	// 			});
	// 		 
	// 			element.addEvent('mouseleave', function(){
	// 				fx.start({
	// 					'padding-top':'5px'
	// 				});
	// 			});
	// 		});
	// 	}
}
window.addEvent('domready', navappear.appearText );


/****zoombox class*************************************************************/
zoomBox = new Class({
	
	Extends: SmoothScroll,	
	
	initialize: function(options, element){
		console.log("zoombox initialised");
		this.parent(options, element);
		this.constructElements();
	},
	
	useLink: function(link, anchor){
		if($(link).hasClass("replyToComment")){ // @Reply hit-switch
			link.addEvent('click', function(event){
				if($('boxTop').getStyle("visibility")!=="hidden"){ 
					console.log("@Replying to comment while zoomBoxed, hiding zoomBox..");
					this.awayTop.start('opacity', 0);
					this.awayBottom.start('opacity', 0);
				}
				this.anchor = anchor;$('boxTop')
				this.toElement("comment");
				if($("comment").value!="") $("comment").value += "\n";
				$("comment").value += '<a href="#' + $(link).parentNode.id + '">@' + $(link).parentNode.getElement("cite").get('text') + ':</a> ';
				window.setTimeout(function(){ $("comment").focus(); }, 600); //$("comment").setSelectionRange($("comment").value.length-10, $("comment").value.length); 
				event.stop();
			}.bind(this));
		}else if(anchor.substr(0,8)=="comment-" && link.title!="permalink"){ //@someone -> scroll to comment 
			link.addEvent('click', function(event){
				this.anchor = $(anchor);
				if($('boxTop').getStyle("visibility")=="hidden"){ //Return to the original comment (not an intermediate one)
					this.originatingComment = $(link).parentNode.parentNode;
				}
				this.go();
				event.stop();
			}.bind(this));
			var noOfReplies = $(anchor).getElement('a.noOfReplies').getElement('strong').get('text').toInt();
			$(anchor).getElement('a.noOfReplies').getElement('strong').set('text', ($type(noOfReplies)?noOfReplies:0)+1 );
		}else{ // keep everything else running smoothly
			link.addEvent('click', function(event){
				this.anchor = anchor;
				this.toElement(anchor);
				event.stop();
			}.bind(this));
		}
	},
	
	constructElements: function(){
		var doc = $(document.body);
		this.box = new Hash({
			top: new Element('div', {id: 'boxTop'}).setStyle("visibility","hidden").inject(doc),
			bottom: new Element('div', {id: 'boxBottom'}).inject(doc),
			right: new Element('div', {id: 'boxRight'}),
			left: new Element('div', {id: 'boxLeft'})
		});
		this.box.right.inject(this.box.bottom);
		this.box.left.inject(this.box.bottom);
		
		this.awayTop = new Fx.Tween($('boxTop'), {duration: 300} );
		this.awayBottom = new Fx.Tween($('boxBottom'), {duration: 300} );
		
		this.mainEl = new Element('div', {id: 'zoomBox_main', 'styles':{
			'display':'none',
			'position':'absolute',
			'left':0,
			'top':0,
			'z-index':200,
			'opacity':0.8,
			'filter':'alpha(opacity=80)',
			'width':'100%',
			'height':'100%',
			'overflow':'hidden'
		}}).inject($(document.body));
		this.subEl = new Element('div', {id: 'zoomBox_sub', 'styles':{
			'border':'0px solid #000'
		}}).inject(this.mainEl);
		this.morphFx = new Fx.Morph(this.subEl, {duration: 600});
		this.scrollFx = new Fx.Scroll(window); //wheelStops: false
	},
	
	go: function(){
		this.pos = this.anchor.getCoordinates();
		// if (this.pos.top>window.getScroll().y+window.getSize().y-100 || this.pos.top<window.getScroll().y+50){
		// 	this.scrollFx.start(0, this.pos.top-window.getSize().y/2+(this.pos.height/2)).chain(function(){ 
		// 		this.morph();
		// 	}.bind(this));
		// } else {
		//		 this.morph();
		// }
		this.scrollFx.start(0, this.pos.top-window.getSize().y/2+(this.pos.height/2)).chain(function(){ 
			this.morph();
		}.bind(this));
	},
	
	morph: function(){
		this.subEl.setStyles({
			'border-bottom-width': window.getSize().y-this.pos.bottom+window.getScroll().y,
			'height': window.getSize().y,
			'border-top': 0+" solid #000", // must set to 0 for safari to work
			'border-right': 0+" solid #000",
			'border-left': 0+" solid #000"
		});
		this.mainEl.setStyles({
			'top': window.getScroll().y,
			'display': 'block'
		});
		this.morphFx.start({
			'height': this.pos.height,
			'border-right-width': window.getSize().x-this.pos.right,
			'border-left-width': this.pos.left,
			'border-top-width': (this.pos.top-window.getScroll().y)
		}).chain(function(){
			this.boxMe();
			// this.morphFx.start({
			// 						'height': window.getSize().y,
			// 						'border-right-width': 0,
			// 						'border-left-width': 0,
			// 						'border-top-width': 0
			// 					}).chain(function(){
			// 						this.mainEl.setStyle('display', 'none');
			// 					}.bind(this));
		}.bind(this));
	},
	
	boxMe: function(){
		var bixSize = new Hash({
				height: this.anchor.getSize().y,
				width: this.anchor.getSize().x,
				x: this.anchor.getPosition().x,
				y: this.anchor.getPosition().y
			});
		var win = new Hash({
				height: window.getSize().y,
				width: window.getSize().x,
				y: window.getPosition().y
			});
			
		this.awayTop.set('visibility', 'visible');
		this.awayBottom.set('visibility', 'visible');
		this.box.top.setOpacity(.8);
		this.box.bottom.setOpacity(.8);
		
		this.box.top.setStyles({'height':bixSize.y}).addEvent('click', function(e){e = new Event(e); this.awayBox(); e.stop();}.bind(this));
		this.box.bottom.setStyles({'height':window.getScrollHeight()-bixSize.y-bixSize.height, 'top':bixSize.y+bixSize.height}).addEvent('click', function(e){e = new Event(e); this.awayBox(); e.stop();}.bind(this));
		this.box.right.setStyles({'width':win.width-bixSize.width-bixSize.x, 'top':-bixSize.height, 'height':bixSize.height });
		this.box.left.setStyles({'width':bixSize.x, 'top':-bixSize.height, 'height':bixSize.height });
		
		this.box.top.setStyle('display','block');
		this.box.bottom.setStyle('display','block');
		this.mainEl.setStyle('display','none');
	},
	
	awayBox: function(){
		this.awayTop.start('opacity', 0);
		this.awayBottom.start('opacity', 0);
		var position = this.originatingComment.getCoordinates();
		this.scrollFx.start(position.x, position.top-window.getSize().y/2+(position.height/2));
		this.initialBgColor = $(this.originatingComment).getStyle("background-color");
		// if(this.initialBgColor=="transparent"){ this.initialBgColor=="#1D1D20"; }
		this.highlightFx = new Fx.Morph($(this.originatingComment), {duration: 800}).start({"background-color":"#4e4e54"}).chain(function(){ 
			this.highlightFx.start({"background-color":this.initialBgColor});
			this.awayTop.set('height', 0);
			this.awayBottom.set('height', 0);
		}.bind(this));
	}
	
});

/****Resizing TextArea**********************************************************/
ResizingTextArea = new Class(
	{
		iRows: 1,
		initialize: function(element, options)
		{
			this.element = $(element.id);
			this.element.setStyle('overflow','hidden');
			this.element.setStyle('overflow-x','auto');
			this.element.setProperty('wrap','vitual');
			this.iRows = this.getRows();
			this.opt = Object.extend(
				{
					resizeStep: 1
				},
				options || {}
			);
			this.resize(this); // Boot up just in case we used an onload
			this.element.onclick = this.resize.bindAsEventListener(this);
			this.element.onkeyup = this.resize.bindAsEventListener(this);
		},
		getRows: function()
		{
			return Math.max(this.element.getProperty('rows'), 5)
		},
		resize: function()
		{
			var lines = this.element.get('value').split('\n');
			var newRows = lines.length + this.opt.resizeStep;
			var oldRows = this.iRows;
			var cols = this.element.getProperty('cols');
			for (var i = 0; i < lines.length; i++)
			{
				var line = lines[i];
				if (line.length >= cols) newRows += Math.floor(line.length / cols);	
			}
			if (newRows > this.element.rows) this.element.setProperty('rows', newRows);
			if (newRows < this.element.rows) this.element.setProperty('rows', Math.max(this.iRows, newRows));
		}
	}
);


/****Dom Ready******************************************************************/
window.addEvent('domready', function(){
	var zoop = new zoomBox(null, window); // mootools bug - must specify these params
	// var zoop = new SmoothScroll(null, window); // mootools bug - must specify these params
	if(window.isSingle){
		$$('a.noOfReplies').each(function(el, i) {
			el.getElement('strong').get('text')=="1" ? el.getElement('span').set('text', ' reply') : null;
		});
		
		if($('author')){
			$('author').addEvent('focus', function(){
				if(this.value == "Name") { this.value = "" };
			}).addEvent('blur', function(){
				if(this.value == "") { this.value = "Name" };
			});
			$('email').addEvent('focus', function(){
				if(this.value == "Email") { this.value = "" };
			}).addEvent('blur', function(){
				if(this.value == "") { this.value = "Email" };
			});
			$('openid_url').addEvent('focus', function(){
				if(this.value == "Website") { this.value = "" };
			}).addEvent('blur', function(){
				if(this.value == "") { this.value = "Website" };
			});
			$('commentform').addEvent('submit', function(){
				if($('author').value == "Name") { $('author').value = "" };
				if($('email').value == "Email") { $('email').value = "" };
			});
		}
		
		// $$('textarea').each(function(el){
		// 		new ResizingTextArea(el);
		// 	}
		// );
	}
});




