var GUI = (function() {
	function initGUI() {
		initPlugins();
		applySpotBoxStructure();
		miscGUIFixes();
		
		if ( $.browser.msie ) {
			IEfixes();
		}
	}
	
	function tblFixes(){
		$('.tableList, .tblist').each( function() {
			var table = $(this);
			if ( !table.is('.tblist') ) {
				table.wrap( $('<div></div>').addClass('tbwrapper').addClass( table.attr('class') ) );
				table.attr('class', '');
			}

			/* Dessa rader fram till nästa kommentar kan tas bort
			   ifall man väljer att lägga till "even","odd" och "first"
			   css-classerna från PHP istället.
			   Just den här biten är märkligt långsam i IE7, vilket slöar
			   ner sidladdning. */ 
			$('tbody tr', table).each( function(index, val) {
				$(this).addClass( (index%2==0)?'odd':'even' );
			});

			$('td,th', table).filter(':nth-child(1)').addClass('first');
			/* END */		
		});	
	}
	
	function miscGUIFixes() {
		$('.fullinput').each( function() {
			var item = $(this);
			item.wrap( $('<div></div>').addClass('inputwp') );
		});
		
		tblFixes();
	
		$('.lstmetainfo').each(function() {
			var items = $('> li', this);
			var callbackIterator = function(items, col, callback) {
				var itIndex = (col - 1);
				var itemWidth = 0;
				for (var itIndex; itIndex < items.length; itIndex += 2) {
					callback(items, itIndex);
				}
			}

			var subWidth = 0;
			for (var i = 1; i <= 2; i++) {
				var maxWidth = 0;
				var colWidth = 145;
				callbackIterator(items, i, function(items, itIndex) {
					itemWidth = $(items[itIndex]).width();
					if (itemWidth > maxWidth) maxWidth = itemWidth;
				});
				colWidth = (maxWidth > colWidth) ? maxWidth : colWidth;
				callbackIterator(items, i, function(items, itIndex) {
					$(items[itIndex]).css("width", colWidth + 5 + "px");
				});
				subWidth += colWidth;
			}
		});
		
		
		$('input[type=file]').each(function(){
			var item = $(this);
			
			var uploadbuttonlabeltext = item.attr('title'); //get title attribut for languagesettings
			if(uploadbuttonlabeltext == ''){
				var uploadbuttonlabeltext = 'Browse';
			}

			var uploadbutton = '<input type="button" class="smbbtn button_button" value="'+uploadbuttonlabeltext+'" />';
			item.wrap('<div class="fileinputs"></div>');
			item.addClass('file').css('opacity', 0); //set to invisible
			item.parent().append($('<div class="fakefile" />').append($('<input type="text" />').addClass('fakeinput').attr('id',item.attr('id')+'__fake')).append(uploadbutton));

			item.bind('change', function() {
				$('#'+item.attr('id')+'__fake').val(item.val());;
			});
			item.bind('mouseout', function() {
				$('#'+item.attr('id')+'__fake').val(item.val());;
			});
		});
		
		$('form input:checkbox').checkbox({ empty: '/images/empty.png' });
		$('form input:radio').checkbox({ empty: '/images/empty.png' });
		
		$('.openplayer').click( function() {
			var link = $(this).attr('href');
			window.open( link, 'traxxter_player', 'width=581,height=490' );
			return false;
		});
		
		if ( $('body').is('.player') ) {
			$(".tblist").freezeHeaders({height : "100px", width: "581px"});
		}
	}
	
	function initPlugins() {
		$('.tabs:not(.noaction)>ul').tabs( '.tabPanes>.pane', {
			current: 'active'
		} );		
	}
	
	function IEfixes() {
		$('#searchForm').appendTo('#mainContent'); //Fixes IE z-index stacking problem.
	}
	
	function applySpotBoxStructure() {
		$('.spotBox:not(.nogui)').each(function() {
			var item = $(this);

			item.wrapInner(
				$('<div>').addClass('bw').append(
					$('<div>').addClass('bw2').append(
						$('<div>').addClass('content')
					)
				)
			);

			var top = $('<div>').addClass('bt'); if (!item.is('.plain')) top.append('<div>');
			var bottom = $('<div>').addClass('bb'); if (!item.is('.plain')) bottom.append('<div>');
			item.prepend(top);
			item.append(bottom);
		});		
	}
	
	return {
		init: initGUI
	}
})();
jQuery( function() {
	GUI.init();
});
