$(
	function(){
	
		$("#field_language").change(function(){
			top.location = 'browse.php?lang='+this.value;
		});

		$('.navigationPane li').addClass('rounded').append('<span class="tl"></span><span class="bl"></span><span class="tr"></span><span class="br"></span><span class="r"></span>');
		
		$('.navigationPane li:not(.current)').hover(
			function(){
				$(this).stop(true).animate(
					{
						width:285
					},
					200
				);
			},
			function(){
				$(this).stop(true).animate(
					{
						width:"100%"
					},
					200
				);
			}
		);
		
		setInterval(
			function(){
				$('.navigationPane li.rounded').each(
					function(){
						var h = $('a', this).height();
						$('.r', this).height(h-5);
					}
				);
			},
			30
		);

		update();
		
		$('.aboutTab').click(
			function(){
				$('.AboutPopup').toggle();

				var hide_selects = false;
				if ($('.AboutPopup').is(':visible')) {
					hide_selects = true;
				}

				$('form select').each(
					function(){
						if (hide_selects) {
							$(this).css('visibility', 'hidden');
						} else {
							$(this).css('visibility', '');
						}
					}
				);
			}
		);
		
		$('textarea[maxlength]').keyup(function(){  
			var limit = parseInt($(this).attr('maxlength'));  
			var text = $(this).val();  
			var chars = text.length;  
			if(chars > limit){  
				var new_text = text.substr(0, limit);  
				$(this).val(new_text);  
			}  
		});  
		

		
	}
);

$(window).load(
	function(){
		$('ul.postits li.postit a')
			.css('height', 'auto')
			.filter(
				function(){
					var hasImg = $('img', this).length > 0 ? false : true;
					return hasImg;
				}
			)
			.each(
				function(){
					var aObj = $(this);
					var changed = false;
					var sanity = 0;
					while(aObj.height() > 78){
						changed = true;
						aObj.html(aObj.html().substr(0, aObj.html().lastIndexOf(' ')));
					}
					if(changed){
						aObj.html(jQuery.trim(aObj.html().substr(0, aObj.html().length - 5)) + '...');
					}
				}
			);
	}
);

function update(){

		// Opera and IE don't support rounded corners.. so lets add them with JS and additional CSS
		if($.browser.opera || $.browser.msie){
			$('head').append('<link rel="stylesheet" media="screen" href="style/rounded.css" />');
			
			//About Tab
			$('a.aboutTab:not(.aboutTab-rounded)').addClass('aboutTab-rounded').append('<span class="bl"></span><span class="br"></span>');
			//About Popup
			$('.AboutPopup:not(.AboutPopup-rounded)').addClass('AboutPopup-rounded').append('<span class="bl"></span><span class="br"></span><div class="b"></div>').prepend('<span class="tl"></span><span class="tr"></span><div class="t"></div>');
			
						//Filter menu, navigation column, contentpane 
			$('.filterForm:not(.rounded-columns), .navigationPane:not(.rounded-columns), .contentPane-inner:not(.rounded-columns)')
				.addClass('rounded-columns')
				.wrapInner('<div class="rounded-wrapper clearfix"></div>')
				.append('<span class="b"></span>')
				.prepend('<span class="t"></span>');
				
			$('ul.contentPane-tabs li:not(.rounded)').addClass('rounded').prepend('<span class="t"></span>');
			
			// buttons
			$('button:not(.rounded)').addClass('rounded').append('<span class="tl"></span><span class="bl"></span><span class="tr"></span><span class="br"></span>');
		}

		setTimeout(
			function(){

				if($.browser.opera || $.browser.msie){
					//	var paneHeight = $('.contentPane').height() > 300 ? $('.contentPane').height() : 300;
		
					var h = Math.max($('.navigationPane .rounded-wrapper').height(), $('.contentPane-inner .rounded-wrapper').height());
		
					if (h>270) {
						
						var hdiff = $('.contentPane-inner .rounded-wrapper').height() - $('.navigationPane .rounded-wrapper').height();

						if(hdiff >= 0){
							
							hdiff = hdiff + 30;
							if($('.contentPane-tabs li').length){
								hdiff = hdiff - $('.contentPane-tabs li').height() + 60;
							}
							$('.navigationPane .rounded-wrapper').height($('.navigationPane .rounded-wrapper').height() + hdiff);
						} else {
							
							hdiff = hdiff + 30;
							if($('.contentPane-tabs li').length){
								// hdiff += $('.contentPane-tabs li').height();
								hdiff = hdiff - $('.contentPane-tabs li').height() + 60;
							}
							$('.contentPane-inner .rounded-wrapper').height($('.contentPane-inner .rounded-wrapper').height() + (hdiff*-1));
						}
		
					} else {
						$('.navigationPane').height(300);
						$('.contentPane-inner .rounded-wrapper').height(270);
					}
					
				} else {
					
					var h = Math.max($('.contentPane').height(), $('.navigationPane').height());
					if (h>270) {
						var hdiff = $('.contentPane').height() - $('.navigationPane').height();
						if(hdiff > 0) {
							$('.navigationPane').height($('.contentPane').height());
						} else {
							$('.contentPane-inner').height($('.contentPane-inner').height() + (hdiff*-1));
						}
					} else {
						$('.contentPane-inner').height(270);
						$('.navigationPane').height(300);
					}
						
				}
				// fix height of comment tabs
				var maxH = 0;
				$('ul.contentPane-tabs li a').each(
					function(){
						maxH = Math.max($(this).height(), maxH);
					}
				);
				$('ul.contentPane-tabs li a').height(maxH);
			},
			100
		)
		
}

