/*
Copyright (c) 2011 Paris Holley
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation;
version 3.0.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, visit
http://www.gnu.org/licenses/lgpl.html
*/

if( typeof jQuery != "undefined"){
	jQuery(function(){
		var $ = jQuery;

		$('.nm-table').each(function(){
			var columns = $(this).attr('nm-columns') || $(this).children().length;

			var roll = 0;
			var row = 1;

			var children = $(this).children();

			var rows = Math.ceil(children.length / columns);

			children.each(function(){
				var el = $(this);

				if( row == rows ){
					el.addClass('nm-row-last');
				}

				if( row == 1 ){
					el.addClass('nm-row-first');
				}

				if( roll++ == 0 || roll % columns == 1 ){
					el.addClass('nm-column-first');
				}else if( roll % columns == 0 || roll == children.length){
					el.addClass('nm-column-last');
					row++;
				}
			});
		});
		
		$('.nm-leftright, .nm-leftright-child > *').each(function(){
			$(this).prepend('<span class="nm-left"></span>').append('<span class="nm-right"></span>');
		});
		
		$('.nm-box').each(function(){
                var el = $(this);

                var wrapper = $('<div class="nm-box-wrapper nm-hoverable"></div>');
                wrapper.attr('id', el.attr('id'));
                
                el.attr('id','');

                var classes = el.attr('class').split(' ');

                var append = "";
		
		$.each(classes, function(){
                        var css = this;
			
			if( css.indexOf('nm-box-') > -1 ){
				el.removeClass(css);
				wrapper.addClass(css);
			}else{
				append += " " + css;
			}
                });
                
		wrapper.addClass(append);

                el.wrap(wrapper);

                el.wrap($('<div class="nm-box-top"></div>'));
                el.wrap($('<div class="nm-box-right"></div>'));
                el.wrap($('<div class="nm-box-bottom"></div>'));
                el.wrap($('<div class="nm-box-left"></div>'));

                var parent = $(el.parent());

                var wrap = $('<div class="nm-box-content"></div>');
                wrap.data(el.data());
                wrap.append(el.contents());
                
		parent.append($('<div class="nm-box-top-left"></div>'));
                parent.append($('<div class="nm-box-top-right"></div>'));
		parent.append($('<div class="nm-box-bottom-left"></div>'));
                parent.append($('<div class="nm-box-bottom-right"></div>'));
               
		el.replaceWith(wrap);
        });
        		
		$('.nm-hoverable, .nm-hoverable-child > *').mouseenter(function(){
			$(this).addClass('nm-hover');
		});
		
		$('.nm-hoverable, .nm-hoverable-child > *').mouseleave(function(){
			$(this).removeClass('nm-hover');
		});
	});
}

