var isIE7 = /MSIE 7/.test(navigator.userAgent);
var isIE6 = /MSIE 6/.test(navigator.userAgent) && !isIE7;

$(document).ready(function(){
	documentReady();
})

function documentReady(){
	fixExtLinks();
	if(isIE6) fixIE6();
	$('#main-pane').attr('scrollable', 'false');
	$('.example .clickable').click(function(){exampleClick(this, 'add')});
	$('#set .clear').click(function(){exampleClick(this, 'clear')});
}

function fixExtLinks(){
	$('a.ext, .ext a').add('#counters a,.buttons a').click(function(){
		window.open(this.href, '_blank').focus();
		return false;
	})
}

function toggleScrollable(){ //		javascript:toggleScrollable()
	var mainPane = $("#main-pane");
	var scrollable = (mainPane.attr('scrollable')=='true');
	//alert(typeof(mainPane.attr('scrollable')) + ': ' + mainPane.attr('scrollable'));
	//alert(typeof(scrollable) + ': ' + scrollable);
	if(!scrollable){ // !!!
		var winHeight = $(window).height();
		var headerHeight = $('#header').height();
		var footerHeight = $('#footer').height();
		var mainPaneFrame = mainPane.outerHeight() - mainPane.height();
		var contentFrame = $('#content').outerHeight() - $('#content').height();
		//alert(mainPaneFrame + ' / ' + contentFrame + ' / ' + footerHeight); // 10 / 65 / 48

		var height = winHeight - headerHeight - footerHeight - mainPaneFrame;
		var width = mainPane.width();
		mainPane.css("height", height + 'px');
		mainPane.css("width", width + 'px');
		mainPane.css("overflow", "auto");
		mainPane.css("position", "absolute");
		mainPane.css("right", "0px");

		mainPane.attr('scrollable', 'true');
		//if(isIE6) {$('body').css('overflow', 'hidden');} HIDE scrollbar!!!
	}else{
		mainPane.css("height", 'auto');
		mainPane.css("width", 'auto');
		mainPane.css("overflow", "visible");
		mainPane.css("position", "static");
		mainPane.css("right", "auto");

		mainPane.attr('scrollable', 'false');
	}
}

function exampleClick(ex, action){
	var input = $('#highlight');
	if(action=='add'){
		var exampleText = $(ex).text();
		var prevText = input.val();
		var newText = (prevText=='')? exampleText : prevText + ' ' + exampleText;
		input.val(newText);
	}else{
		input.val('');
	}
}

function fixIE6(){
	//alert('Do not use fucking Internet Explorer 6.0!!!\r\nUse Opera!!!');
	$('body').css('height', '100%');
	var news = $('#news');
	if(news.length>0){
		news = news[0];
		max_width(news, 750);
		$('#news .note,#news p.links').each(function(){
			this.style.height = this.offsetHeight;
		})
	}
	max_width('#links', 750);
	max_width('#about', 750);
	max_width('#set', 750);

	$('#footer').css('bottom', '-1px'); 
}

function max_width(el, max_width){
	var els = $(el);
	if(els.length==0) return;
	els.each(function(){
		var el = this;
		if(el.offsetWidth > max_width) $(el).css('width', max_width + 'px');
	})
}
