$(document).ready(function() {
	// Vartical centrin main menu
/*
	var aaa = 0;
	$('ul.main-nav li').each(function() {
		aaa += $(this).outerWidth();
	});
xxx = $('ul.main-nav li').length;
zzz = ((((762-aaa)/xxx))/ 2);
*/
	$('ul.main-nav li span').each(function() {
		var p = ((82 - $(this).height()) / 2)+15;
		//alert ($(this).height());
		$(this).parent().css('height', /*92 - p*/ 50 + 'px');
		$(this).parent().css('padding-top', /*p*/ 42 + 'px');
	});

//	$('ul.main-nav li a').each(function() {
//		$(this).css('padding-left', zzz + 'px');
//		$(this).css('padding-right', zzz + 'px');
//	});

	// Left menu
	$('.leftmenu ul').css('display', 'none');
	$('.leftmenu .current').parent().css('display', 'block');

});

	$('#language').click(function() {
		if ($('#languageSelect').css('display')	== "none") {
			$('#languageSelect').slideDown('slow');
		} else {
			$('#languageSelect').slideUp('slow');	
		}
		return false;
	});


$('.lightcell td').hover(
	function () {
		$(this).css('background','#F2F2F3');
	}, 
	function () {
		$(this).css('background','none');
	}

);

// LavaLamp
$(function() { 
	if( $(".lavaLamp").lavaLamp ) {
		$(".lavaLamp").lavaLamp({ fx: "backout", speed: 700 });
	}
});
// Showing different tabs and their content on click.

$( '.tabs>h2' ).click(function() {
	if( $( this ).parents( '.tabs' ).hasClass( 'tabbed' ) ) {
		return;
	}
	var tabNo = 0;
	var thisHdr = this;
	$('.tabs>h2').each(function() {
		$( '#tab' + tabNo ).css('display', 'none');
		$(this).removeClass("selected");
		if ($(thisHdr).hasClass( 'header-tab' + tabNo )) {
			$('#tab' + tabNo).css('display', 'block');
			$(thisHdr).addClass('selected');
		}
		tabNo++;
	});
});

$('.lavaLamp > li > a').live('click',function() {
	try {
		var urls = $(this).attr('href').match(/\#[a-zA-Z0-9_\-]+/),
			href = ( urls && urls[0] ? urls[0].substr(1) : '' );

        var $parent = $(this).parents('.lavaLamp').parent();
        if( $parent.hasClass( 'headers' ) )
            $parent = $parent.parent().parent();

		$parent.find('.lavaLamp-module').hide();
		$('#'+href).show();
		$(this).parents('.lavaLamp').find('.back').css({"left":this.offsetLeft+"px","width":this.offsetWidth+"px"});
	} catch (e){}
	return false;
});

/* Permalink */

$('.tab-content a.link').each(function() {
	var len = $( '.tab-content' ).length - 1,
		lang = ( window.PageStatus ? PageStatus.Lang() : ( window.lang ? window.lang : 'ru' ) ),
		id = ( ( lang == 'ar' || lang == 'fa' ) ? len - $(this).parents( '.tab-content' ).attr('id').substr(3,1) : $(this).parents( '.tab-content' ).attr('id').substr(3,1) );

	if ($(this).parents( '.lavaLamp-module' ).attr('id')){
		link = '?tab=' + id + '&slider=' + $(this).parents( '.lavaLamp-module' ).attr('id');
		$(this).attr( 'href', link );
	} else{
		link = '?tab=' + id;
		$(this).attr( 'href', link );
	}
});

$('.gradient tr, .block-button.clicked, .tournament-grid ul').click( function() {
	var el = $( '#' + $(this).attr('id') + '_rating' );
	
	if( typeof getPageScroll ==  'function' ) $('.rating_window').css('top', getPageScroll()[1] + 'px' ).hide();
	else $('.rating_window').hide();

	el.show().find( '.p-close' ).unbind('click').click( function() {
		$(this).parent().hide();
	});
	if( typeof getPageSize ==  'function' ) {
		if( el.height() > getPageSize()[3] ) // column height bigger than window
			el.css('top', '');
	}
})

/**
 * Currys a function and arguments. Takes a function as the first argument,
 * and returns a zero-arugument function that calls the provided function
 * with the provided arguments.
 */
function curryFunction() {
	var f = arguments[0];
	var args = [];
	for (i = 1; i < arguments.length; i++) {
		args[i-1] = arguments[i];
	}
	return function() {
		return f.apply(this, args);
	}
}

/* block optimize
$(document).ready(function() {
    $('#why').html( $('#optimize').html() );
    $('#optimize').remove();
    $('#foru').html( $('#optimize2').html() );
    $('#optimize2').remove();
});
*/



if( VisitorStatus ) {
	//VisitorStatus.cookies.switcher_account_info = $('div.msg-ok span');
	$('div.msg-ok > span').click(function () {
		$( this ).parent().find('p, h5, ul, span').toggle();
		if( $( this ).attr( 'id' ) == 'open_account_info' ) {
			if( VisitorStatus.GetCookie( 'open_account_info' ) ) 
				VisitorStatus.SetCookie( 'open_account_info', 'ok',  -1 );
			else 
				VisitorStatus.SetCookie( 'open_account_info', 'ok',  31536000 );
		}
	}); 
	
	if( VisitorStatus.GetCookie( 'open_account_info' ) ) {
		$('div.msg-ok span#open_account_info').click();
		VisitorStatus.SetCookie( 'open_account_info', 'ok',  31536000 );
	}
}

$( document ).keydown(
	function( ev ) {
		ev = ev || window.event;
		var code = ev.keyCode || ev.charCode;
		if( 
			code != 17 &&
			code == 112 &&
			ev.altKey == 1
		) {
			ev.preventDefault ? ev.preventDefault() : ev.returnValue = false;
			(function(){
				window.open( '/ru/chat/','support','toolbar=no,resizable=yes,status=no,menubar=no,location=no,width=600,height=500' );
			}());
		}
	}
);

/* ---------------------------------------------------------------------- */

function my_format_number( value ) {
	var result = value;
	if( !isNaN( Number( value ) ) ) {
		var iP = parseInt( value );
		var fP = ( Math.round( ( Number( value ) - iP ) * 100 ) / 100 + '' ).substring( 2 );
	}
	
	var f = '';
	var s = iP + '';
	var separator = ( window.THOUSAND_SEPARATOR || '&nbsp;' );
	
	if( s.length > 4 ) {
		var frtLen = s.length % 3;
		if ( frtLen != 0 ) f = s.substr( 0, frtLen ) + separator;
		if ( frtLen != s.length ) {
			f += s.substr( frtLen, 3 );
			for ( var i = 1; i < Math.floor( s.length / 3 ); i++ ) {
				f += separator + s.substr( frtLen + i*3, 3 );
			}
		}
	} else {
		f = s;
	}

	result = f + ( window.FLOAT_SEPARATOR || '.' ) + ( fP || '00' ) ;
	return result;
}


