var $matchClassName = function (node, className){
	try {
		var re = new RegExp ("\\b" + className + "\\b", "gi");
		return ( node.className.match(re));
	} catch ( e ) { return }
}


var $applyClassName = function (node, className, state ){
	try {
		var re = new RegExp ("\\b" + className + "\\b", "gi");
		if ( state ) {
			if ( !node.className.match(re) ) node.className += (" " + className);
		}else{
			node.className = node.className.replace(re, "");
			node.className = node.className.replace(/\s+/gi, " ");
		}
	} catch ( e ) {}
}

var $addEventListener = function ( node, eventType, eventListener ) {
	try {
		if( node.attachEvent ) {
			node.attachEvent( "on" + eventType, eventListener );
		}
		else
		{
			node.addEventListener( eventType, eventListener, false);
		}
	} catch ( e ) {}
}

var $getElementsByClassName = function ( container, className ) {

	var tmpArr = [];

	try {
		var nodeList = container.getElementsByTagName( "*" );

		for ( var i = nodeList.length; i-- >0; ) {
			if ( $matchClassName( nodeList.item( i ), className ) ) tmpArr.push( nodeList.item( i ) );
		}

	} catch ( e ) {};

	return tmpArr;
}

var $switchClassName = function ( id, className ) {
	try {
		var node = document.getElementById( id );
		var re = new RegExp ("\\b" + className + "\\b", "gi");
		if ( !node.className.match(re) ) {
			node.className += (" " + className)
		}else{
			node.className = node.className.replace(re, "");
			node.className = node.className.replace(/\s+/gi, " ");
		}
	} catch ( e ) {}
}

var $dispatchEventForElement = function ( node, eventType ) {
    try {

        if(!node.fireEvent){
            var evt = document.createEvent("MouseEvents");
            evt.initMouseEvent(eventType, true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
            node.dispatchEvent(evt)
        }else{
            node.fireEvent("on"+eventType);
        }
    } catch ( e ) {}
}


var $tabInit = function ( tabBoxID, tabSwitchContainerID, switchID, activityID, initHandler ) {
	try {
		var tabBoxNode = document.getElementById( tabBoxID );
		var tabSwitchContainer = document.getElementById( tabSwitchContainerID );
		var switchNodesList = $getElementsByClassName ( tabBoxNode, switchID );

		var activeTab = switchNodesList[ 0 ];

		for ( var i = switchNodesList.length; i--; ) {

			switchNodesList[ i ].options = {
				linkedNode: switchNodesList[ i ].parentNode,
				tabSwitchContainer: tabSwitchContainer,
				switchID: switchID,
				activityID: activityID
			}

			tabSwitchContainer.appendChild ( switchNodesList[ i ] );

			if ( $matchClassName ( switchNodesList[ i ], activityID ) ) activeTab = switchNodesList[ i ];
		}


		$dispatchEventForElement ( activeTab, "click" );

		if ( initHandler ) initHandler ( switchNodesList )

	} catch ( e ) { }
}

var $switchTab = function ( node ) {
	try {

		if ( !node.options ) {
			do{
				node = node.parentNode;
			} while ( !node.options )
		}

		var opts = node.options;
		var tabSwitchList = $getElementsByClassName( opts.tabSwitchContainer, opts.switchID );

		for ( var i=tabSwitchList.length; i--; ) {
			$applyClassName ( tabSwitchList[ i ], opts.activityID, tabSwitchList[ i ] == node );
			$applyClassName ( tabSwitchList[ i ].options.linkedNode, "hiddenBlock", tabSwitchList[ i ] != node );
		}


	} catch ( e ) {  };
}

var $ieEmulateRollOver = function ( node ) {
	try {
		node.onmouseover = $ieRollOver;
		node.onmouseout = $ieRollOut;
	} catch ( e ) { };
}
var $ieRollOver = function ( ) {
	try {
		$applyClassName ( this, "hover", true );
	} catch ( e ) { };
}
var $ieRollOut = function ( ) {
	try {
		$applyClassName ( this, "hover", false );
	} catch ( e ) { };
}

function bottomClick() {
	if ($("#bmenu_text").text() == 'Розгорнути') {
		$("#bmenu_text").text('Згорнути');
	} else {
		$("#bmenu_text").text('Розгорнути');
	}
	$switchClassName( 'bottomMenu', 'inactive' );
}

function videoLoadPage(page) {
	$("#vg_list").load("/ajaxVideoPlayer.php?action=list&page="+page+"ctg="+ctg);
}

function videoLoadNextPage(page, step) {
	videoLoadPage((page - 0) + step);
}

function videoLoadItem(item) {
	$("#vg_player").load("/ajaxVideoPlayer.php?action=player&id="+item);
}
