var Lizzy = new Class({
	initialize:function() {
		this.initCommentForm();
		//this.initPostsLinks();
		this.initTwitter();
		this.initSearchForm();
		this.initMusicIssues();
		this.initBreaks();
		var me = this;
		window.addEvent('load', function() {
			me.initContentHeight();
		});
		
		
	}
	
	,initCommentForm: function() {
		var form = $('comment_form');
		if(!form)
			return;
		var validated = new Form.Validator.Inline(form,{
			useTitles:true
			,errorPrefix:""
		});
	}
	
	,initContentHeight: function() {
		var c = $('page');
		var nav = $('navbar');
		if(!c || !nav) return;
		try{
			var nav_h = nav.getSize().y; 
			var con_h = c.getSize().y;
			if(con_h < nav_h) {
				c.setStyle('height', nav_h-10 +"px");
			}
		}
		catch(err) {
			console.log(err);
		}
	}
	
	// handler for music issues click.
	,initMusicIssues: function() {
		try {$('music_issues').addEvent('click',function(){ this.getElement('a').fireEvent('click');});}catch(oEr){}
	}
	
	// search form: select text on click
	,initSearchForm: function() {
		var input = $('s');
		if(input) {
			input.addEvent('focus',function() {
				this.set('value','');
			});
		}
	}
	
	// makes posts clickable in an overview.
	,initPostsLinks: function() {
		var list = document.getElement('ul.posts');
		if(!list)
			return;
		list.getElements('li').each(function(oEl) {
			var link = oEl.getElement('a.read_more');
			oEl.addClass('linked');
			oEl.store('link',link).addEvent('click',function(){
				document.location = this.retrieve('link');
			});
		})
	}
	
	,initTwitter: function() {
	
		var block = $('twitter');
		if(!block) return;
		/*
		block.addEvent('click', function(oEv) {
			console.log(this.getElement('a'));
			oEv.stop();
			oEv.preventDefault();
			this.getElement('a').fireEvent('click');
			
			//document.location = "http://www.twitter.com/fashionissues";
		});
		*/
		var req = new Request.JSON({
			url:"/fashion/get_tweets"
			,onSuccess:function(oResult) {
				try {
					var msg = oResult[0];
					if(!msg) return;
					var tweet = $('tweet');
					if(!tweet) return;
					var twit = new String(msg.text).truncate(21,30);
					//var twit = new String(msg.text).truncate(140,140);
					tweet.set('text',"“" +twit +"”");
				}
				catch(err) {}
			}
		}).send();
	}
	
	,initBreaks: function() {
		$$('br').each(function(oEl) {
			var span = new Element('span',{'class':'break'});
			span.inject(oEl,'after');
		});
	}
});




String.implement({
	/**
	 * String.truncate(max, atChar, trail)
	 *
	 * ++All parameters are optional.
	 * @param max = (integer) maximum length of truncated string. Defaults to 100 chars.
	 * @param atChar = (string) truncate at the last index of this string. If not found, just truncates to max length.
	 *                 If null, does not search and truncates to max length. Defaults to split at space.
	 * @param trail = (string) what you want appended to the end of the returned string. Defaults to '...'
	 *
	 * @author Michael Fuery, Fuery Solutions, Inc. http://www.fuerysolutions.com/
	 *
	 * +Requires MooTools Core
	 */
	truncate:function(max, atChar, trail){
		var s = this.trim();
		if(s.length<1) return '';
		if(!$defined(max)) var max = 100;
		  else max = max.toInt();
		if(!$defined(atChar)) var atChar=' '; // break at space
		  else if(atChar == null) var atChar=false;
		if(!$defined(trail)) var trail = '...';
	
		if(s.length > max) {
			var i=0;
			if(atChar){
				if((i = s.lastIndexOf(atChar)) != -1){
					s = s.substring(0, i);
				}else{
					s = s.substring(0, max);
				}
			}
	
			s += trail;
		}
	
		return s;
	}
});






window.addEvent('domready',function() {
	// Form validation.
	var lizzy = new Lizzy();
		
	// Custom fonts.
	Cufon.replace('h2', {fontFamily:"Fresco-Bold"});
	Cufon.replace('h2.dash', {
		fontFamily:"Fresco-Bold"
		,letterSpacing:"5px"
	});
	Cufon.replace('h2.submenu', {fontFamily:"Fresco-Normal"});
	Cufon.replace('h2.submenu.dash', {
		fontFamily:"Fresco-Normal"
		,letterSpacing:'-1px'
	});
	Cufon.replace('#gallery_image h2', {fontFamily:"Fresco-Normal"});
		
});

