//
// int13h Common Javascript
//
var __SITEROOT__;
function setSiteRoot (siteroot){__SITEROOT__=siteroot;}
function setGameSiteRoot (gamesiteroot){__GAMESITEROOT__=gamesiteroot;}

$(document).ready(function() {
	// Buttons
	$("button").button();

	// Comment
	$('#b_addnewcomment').click(function(){
		$("#newcomment")[0].reset();
		$("#commentdialog").dialog({
			title: 'Add Comment',
			resizable: false,
			closeOnEscape: false,
			height: 350,
			width: 600,
			autoOpen: true,
			modal: true,
			buttons: {
				"Cancel": function() { $(this).dialog("close");},
				"Add": function() {
					if($.trim($('#name','#commentform').val()).length > 0 &&
						$.trim($('#comment','#commentform').val()).length > 0){
					
						var url=window.location.pathname.split('/');
						$.post(__SITEROOT__+url[url.length-1]+'/Comment.itf',
							$("#newcomment").serialize(),function(data){
							window.location=__SITEROOT__+url[url.length-1];
						},'json');
						$(this).dialog("close");
					}else{
						if($.trim($('#name','#commentform').val()).length < 1)
							$("#dialogtext").html('<br><center>Name too short</center>');
						else
							$("#dialogtext").html('<br><center>Comment too short</center>');
						$("#dialog").dialog({
							title: 'Add Comment',
							resizable: false,
							closeOnEscape: false,
							height: 150,
							width: 300,
							autoOpen: true,
							modal: true,
							buttons: {"Ok": function() { $(this).dialog("close");}}
						});
					}
				}
			}
		});
		return false;
	});

	// JUI Dialog
	$('#dialog').dialog({
		autoOpen: false,
		modal: true,
		width: 300,
		title: 'Notice',
		buttons: {
			"Ok": function() { $(this).dialog("close");}
		},
		open: function() {
	        $(this).closest('.ui-dialog').find('.ui-dialog-buttonpane button:eq(0)').blur();
		}
	});

	// Search Form
	$('#searchform').submit(function(){
		if($('#searchtext').val().length < 2)
			return false;
	});
		
	// Syntax Highlighting
	SyntaxHighlighter.autoloader(
		'applescript            '+__SITEROOT__+'scripts/syntax/shBrushAppleScript.js',
		'actionscript3 as3      '+__SITEROOT__+'scripts/syntax/shBrushAS3.js',
		'bash shell             '+__SITEROOT__+'scripts/syntax/shBrushBash.js',
		'coldfusion cf          '+__SITEROOT__+'scripts/syntax/shBrushColdFusion.js',
		'cpp c                  '+__SITEROOT__+'scripts/syntax/shBrushCpp.js',
		'c# c-sharp csharp      '+__SITEROOT__+'scripts/syntax/shBrushCSharp.js',
		'css                    '+__SITEROOT__+'scripts/syntax/shBrushCss.js',
		'delphi pascal          '+__SITEROOT__+'scripts/syntax/shBrushDelphi.js',
		'diff patch pas         '+__SITEROOT__+'scripts/syntax/shBrushDiff.js',
		'erl erlang             '+__SITEROOT__+'scripts/syntax/shBrushErlang.js',
		'groovy                 '+__SITEROOT__+'scripts/syntax/shBrushGroovy.js',
		'java                   '+__SITEROOT__+'scripts/syntax/shBrushJava.js',
		'jfx javafx             '+__SITEROOT__+'scripts/syntax/shBrushJavaFX.js',
		'js jscript javascript  '+__SITEROOT__+'scripts/syntax/shBrushJScript.js',
		'perl pl                '+__SITEROOT__+'scripts/syntax/shBrushPerl.js',
		'php                    '+__SITEROOT__+'scripts/syntax/shBrushPhp.js',
		'text plain             '+__SITEROOT__+'scripts/syntax/shBrushPlain.js',
		'py python              '+__SITEROOT__+'scripts/syntax/shBrushPython.js',
		'ruby rails ror rb      '+__SITEROOT__+'scripts/syntax/shBrushRuby.js',
		'sass scss              '+__SITEROOT__+'scripts/syntax/shBrushSass.js',
		'scala                  '+__SITEROOT__+'scripts/syntax/shBrushScala.js',
		'sql                    '+__SITEROOT__+'scripts/syntax/shBrushSql.js',
		'vb vbnet               '+__SITEROOT__+'scripts/syntax/shBrushVb.js',
		'xml xhtml xslt html    '+__SITEROOT__+'scripts/syntax/shBrushXml.js'
	);
	SyntaxHighlighter.all();

	// Tips
	$('div').tipTip({maxWidth: "auto", edgeOffset: 5});
	$('#content a').tipTip({maxWidth: "auto", edgeOffset: 5, defaultPosition: 'right'});
	$('#content img').tipTip({maxWidth: "auto", edgeOffset: 5, defaultPosition: 'right'});
	$('#sidebar li').tipTip({maxWidth: "auto", edgeOffset: 5});
	$('#links a').tipTip({maxWidth: "auto", edgeOffset: 5});
	$('#footer img').tipTip({maxWidth: "auto", edgeOffset: 5});

	// Sidebar Toggle
	$('#sb_toggle').click(function(){
		if($('#sidebar').is(":visible")){
			$('#sidebar').hide();
			$('#content').css('width','858px');
			$('#sb_toggle').css('background-position','-34px');
		} else {
			$('#sidebar').show();
			$('#content').css('width','580px');
			$('#sb_toggle').css('background-position','0px');
		}
	});
	if(!$('#sidebar').is(":visible")){
		$('#content').css('width','858px');
		$('#sb_toggle').css('background-position','-34px');
	}

	$('#img_404').mouseover(function(){
		$(this).attr('src',__SITEROOT__+'images/404.png');
	}).mouseout(function(){
		$(this).attr('src',__SITEROOT__+'images/404.jpg');
	});
});

