/** 
	Quick hack to prevent browsers w/o a console, or firebug from generating errors when console functions are called.
	Gets called on post definition.
*/
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() {};
	}
	names = null;
}

var mop = {
	util: {}
};

mop.util.getValueFromClassName = function( key, aClassName ){
//	console.log( "getValueFromClassName", key, aClassName );
	if(!aClassName) return;
	var classNames = aClassName.split( " " );
	var result = null;
	classNames.each( function( className ){
		if( className.indexOf( key ) > -1 ){
			result = className.split("-")[1];
		}
	});
	try{
		return result;
	}finally{
		className = null;
		result = null;
	}
}

var cartwheel = {};

cartwheel.PaginatedCollection = new Class({

	pageIndex: 0,
	limit: 5,

	initialize: function( anElement, aControlElement ){
		this.element = $(anElement);
		this.limit = Number( mop.util.getValueFromClassName( "limit", this.element.get("class") ) );
		this.pages = Math.ceil( this.element.getChildren("li").length / this.limit );
		this.previousPage = aControlElement.getElement(".paging_left");
		if( this.previousPage ){
			this.previousPage.addEvent( "click", this.paginate.bindWithEvent( this, -1 ) )
			this.previousPage.store("enabled", false );
			this.previousPage.addClass("hidden");
		}
		this.nextPage = aControlElement.getElement(".paging_right");
		if( this.nextPage ){
			this.nextPage.addEvent( "click", this.paginate.bindWithEvent( this, 1 ) );
			this.nextPage.store("enabled", true );
			if( this.pages == 1 ) this.previousPage.addClass("hidden");
		}
	},
	
	toString: function(){
		return "[ object, cartwheel.PaginatedCollection ]";
	},
	
	paginate: function( e, modifier ){

		e.preventDefault();

		this.pageIndex += modifier;
		
		if( !e.target.retrieve( "enabled" ) ) return false;

		console.log( "\tpageIndex: ", this.pageIndex, "\tpages: ", this.pages );

		if( this.pageIndex >= 1  ){
			this.previousPage.removeClass("hidden");
			this.previousPage.store("enabled", true );
		}else{
			this.pageIndex = 0;
			this.previousPage.addClass("hidden");
			this.previousPage.store("enabled", false );
		}

		if( this.pageIndex >= this.pages-1 ){
			this.nextPage.addClass("hidden");
			this.nextPage.store("enabled", false );
		}else{
			this.nextPage.removeClass("hidden");
			this.nextPage.store("enabled", true );
		}

		var offset = this.pageIndex * this.limit;
		this.element.getChildren("li").each( function( listItem, index ){
			if( index >= offset && index < offset + this.limit ){
				listItem.removeClass("hidden");
			}else{
				listItem.addClass("hidden");
			}
		}, this );
		
		replaceNewsSifr();

	}

});

cartwheel.VisibilityToggler = new Class({
	initialize: function( anElement, elementToToggle ){
//		console.log( anElement, elementToToggle, elementToToggle.getSize().x );
		this.element = anElement;
		this.element.setStyle("cursor","pointer");
		this.target = elementToToggle;
		this.targetOgHeight = this.target.getSize().y;
		this.target.addClass( "hidden" );
		this.target.set( "morph", { onComplete: this.onTransitionComplete.bind( this ) });
		this.element.addEvent( "click", this.toggleTargetVisibility.bindWithEvent( this ) );
		this.target.setStyle("overflow","hidden");
		this.ogMarginTop = this.target.getStyle("margin-top");
		this.ogMarginBottom = this.target.getStyle("margin-bottom");
		this.ogPaddingTop = this.target.getStyle("padding-top");
		this.ogPaddingBottom = this.target.getStyle("padding-bottom");
	},
	toggleTargetVisibility: function( e ){
		e.preventDefault();
		this.element.removeEvents("click");
		if( this.target.hasClass("hidden") ){
			e.target.getElement("span").set("text","-");
			this.target.setStyle("height",1);
//			this.target.setStyle("opacity",0);
			this.target.removeClass("hidden");
			this.target.morph({
				height: this.targetOgHeight,
				opacity:1,
				"margin-bottom": this.ogMarginBottom,
				"margin-top": this.ogMarginTop,
				"padding-top": this.ogPaddingTop,
				"padding-bottom": this.ogPaddingBottom,
				duration: 1000
			});
		}else{
			e.target.getElement("span").set("text","+");
			this.target.morph({
				height:0,
				"margin-bottom": 0,
				"margin-top": 0,
				"padding-top":0,
				"padding-bottom":0,
				opacity:0,
				duration: 1000
			});
		}
	},
	onTransitionComplete: function(){
		this.element.addEvent( "click", this.toggleTargetVisibility.bindWithEvent( this ) );
		console.log( this.target.getStyle("height") );
		if( this.target.getStyle("height") == "0px" ) this.target.addClass("hidden");
	}
})

cartwheel.fixIE6 = function(){
	// fix ie6 double margin bug
	// ideon.content.css( "display", "inline" );
	// Fix ie6's lack of png support
	document.styleSheets[0].addRule('* img', 'behavior: url(/iepngfix.htc)');
	document.styleSheets[0].addRule('.call', 'behavior: url(/iepngfix.htc)');
	document.styleSheets[0].addRule('.quote', 'behavior: url(/iepngfix.htc)');
	document.styleSheets[0].addRule('.paging_right', 'behavior: url(/iepngfix.htc)');
	document.styleSheets[0].addRule('.paging_left', 'behavior: url(/iepngfix.htc)');
	document.styleSheets[0].addRule('dt.alert', 'behavior: url(/iepngfix.htc)');
	document.styleSheets[0].addRule('dt.news', 'behavior: url(/iepngfix.htc)');
}


function replaceNewsSifr(){
	
	sIFR.replace(futura, {
	  selector: '#alerts_column h5',
	  ratios: [8, 1.3, 11, 1.21, 12, 1.2, 14, 1.19, 21, 1.16, 28, 1.13, 38, 1.12, 61, 1.11, 94, 1.1, 95, 1.09, 103, 1.1, 107, 1.09, 110, 1.1, 119, 1.09, 120, 1.1, 1.09],
	    wmode: 'transparent',
	    offsetTop: '-2',
	    kerning: 'true',
	    css: [ '.sIFR-root {color: #BF1B5F;letter-spacing: -1;leading: -2;}' 
			  ,'a {color: #BF1B5F; text-decoration: none;}'
			  ,'a:hover {color: #BF1B5F;cursor:pointer;}'		
			  ]
	});

	sIFR.replace(futura, {
	  selector: '#news_column h5',
	  ratios: [8, 1.3, 11, 1.21, 12, 1.2, 14, 1.19, 21, 1.16, 28, 1.13, 38, 1.12, 61, 1.11, 94, 1.1, 95, 1.09, 103, 1.1, 107, 1.09, 110, 1.1, 119, 1.09, 120, 1.1, 1.09],
	    wmode: 'transparent',
	    offsetTop: '-2',
	    kerning: 'true',
	    css: [ '.sIFR-root {color: #014C6C;letter-spacing: -1;leading: -2;}' 
			  ,'a {color: #014C6C; text-decoration: none;}'
			  ,'a:hover {color: #014C6C;cursor:pointer;}'		
			  ]
	});

}


window.addEvent( "domready", function(){
	var news = $("news_column");
	var news_paging = $("news_paging");
	var alerts = $("alerts");
	var alerts_paging = $("alerts_paging");
	var clients = $("clientlist");
	var clients_paging = $("clients_paging");
	if( news && news_paging ) newsPagination = new cartwheel.PaginatedCollection( news, news_paging );
	if( alerts && alerts_paging ) alertsPagination = new cartwheel.PaginatedCollection( alerts, alerts_paging );
	if ( Browser.Engine.trident && Browser.Engine.trident4 ) cartwheel.fixIE6();
	if( clients && clients_paging ) clientsPagination = new cartwheel.PaginatedCollection( clients, clients_paging );
	$$(".toggle").each( function( el ){
		el.store("Class", new cartwheel.VisibilityToggler( el, el.getNext() ) );
	});
});