// == All pages related ==
function getMenuLink($menu_item) {
	var $link = $($menu_item).children('a:first');
	if ($link.length) {
		return $link.attr('href');
	}

	return false;
}

function initSidebarMenu() {
	$('div.menu-item', 'div.menu-container').each(
		function() {
			// select current menu item
			var $url_match = false;
			$(this).children('a').each(
				function() {
					$(this).click(
						function ($e) {
							// click on link won't raise click on it's container
							$e.stopPropagation();
						}
					);


					var $url = $(this).attr('href');
					if ( window.location.href.match($url) ) {
						// false - don't go to next link
						$url_match = true;
						return false;
					}

					return true;
				}
			)

			if ($url_match || $(this).children('strong.selflink').length) {
				$(this).addClass('selected');
			}

			// click on div clicks on 1st link inside it
			$(this).click(
				function($e) {
					var $url = getMenuLink(this);
					if ($url === false) {
						$url = window.location.href;
					}
					window.location.href = $url;

					$e.stopPropagation();
				}
			);
		}
	);

	$('div.menu-item:last', 'div.menu-container').addClass('last');
}

/** Collapsible tables *********************************************************
 *
 *  Description: Allows tables to be collapsed, showing only the header. See
 *               [[Wikipedia:NavFrame]].
 *  Maintainers: [[User:R. Koot]]
 */

var collapseCaption = 'hide';
var expandCaption = 'show';

function collapseTable($index)
{
    var Button = $('#collapseButton' + $index);
    var Table = $('#collapsibleTable' + $index);

    if (!Table || !Button) {
        return false;
    }

    if (Button.html() == collapseCaption) {
    	$('tr', Table).each(
			function ($index) {
				if ($index == 0) {
					return ;
				}

	    		$(this).hide();
	    	}
	    );

        Button.html(expandCaption);
    } else {
        $('tr', Table).each(
			function ($index) {
				if ($index == 0) {
					return ;
				}

	    		$(this).show();
	    	}
	    );

        Button.html(collapseCaption);
    }
}

function collapseDiv($index, $fast)
{
    var Button = $('#collapseButton' + $index);
    var Div = $('#collapsibleDiv' + $index);

    if (!Div || !Button) {
        return false;
    }

    if (Button.html() == collapseCaption) {
    	if ($fast) {
			$('div:first', Div).hide();
    	}
    	else {
    		$('div:first', Div).slideUp('slow');
    	}
        Button.html(expandCaption);
    } else {
        $('div:first', Div).slideDown('slow');
        Button.html(collapseCaption);
    }
}

function initCollapseButtons()
{
	// collapsable tables
	var NavigationBoxes = new Array();

	$('table.collapsible').each(
		function($index) {
			/* only add button if there is a header row to work with */
			var HeaderRow = $('tr:first', this).get(0);
			if (!HeaderRow) {
				return ;
			}

			var Header = $('th:first', HeaderRow).get(0);
			if (!Header) {
				return ;
			}

			var $table = $(this);
			NavigationBoxes.push($table);
			$table.attr('id', 'collapsibleTable' + $index);

			$('<span class="collpase-toggle">[<a href="javascript:collapseTable(' + $index + ');" id="collapseButton' + $index + '">' + collapseCaption + '</a>]</span>')
			.prependTo( Header );
		}
	);

	var $i = 0;
	while ($i < NavigationBoxes.length) {
		var $table = NavigationBoxes[$i];

		if ( $table.hasClass('collapsed') ) {
			collapseTable($i);
		}
		else if ( $table.hasClass('innercollapse') ) {
			if ($table.parents('.outercollapse').length) {
				collapseTable($i);
			}
		}

		$i++;
	}

	// collapsable divs
	$('div.collapsible').each(
		function ($index) {
			var $header = $(':header:first', this).get(0);
			if (!$header) {
				return ;
			}

			$(this).attr('id', 'collapsibleDiv' + $index);

			$('<span class="collpase-toggle">[<a href="javascript:collapseDiv(' + $index + ');" id="collapseButton' + $index + '">' + collapseCaption + '</a>]</span>')
			.prependTo( $header );

			if ($(this).hasClass('collapsed')) {
				collapseDiv($index, true);
			}
		}
	);
}

function isEditable() {
	if (wgAction == 'edit' || wgAction == 'submit') {
		return $("#wpTextbox1").length > 0;
	}

	return false;
}

function fixWebKitCss() {
	if (!$.browser.safari) {
		return ;
	}

	$('head:first').append('<style type="text/css">code, pre { font-size: 127%; }</style>');
}

$(document).ready(
	function() {
		initSidebarMenu();
		initCollapseButtons();
		fixWebKitCss();

		if ( isEditable() ) {
			// install User:MarkS/Extra edit buttons
			XEBOrder = 'V,K,O,S,B,H,SRC,E,L,T,W,X'; // добавить кнопки
			rmEditButtons = [6, 7, 10]; // убрать кнопки

			// собственные кнопки
			myButtons = {
				'SRC': [
					wgServer + wgScriptPath + '/images/1/11/Button_source_jv.png',
					'Добавить исходный код',
					'<source lang="language">' + "\n",
					"\n" + '</source>' + "\n",
					'source code'
				]
			};

			$('<script type="text/javascript" src="' + wgServer + wgScriptPath + '/extensions/IntechnicWiki/extraeditbuttons.js"><\/script>').appendTo('body');
			$('<script type="text/javascript" src="' + wgServer + wgScriptPath + '/extensions/IntechnicWiki/editpage.js"><\/script>').appendTo('body');
		}
	}
)