// 2010

$(document).ready(function () {	

		$('#top_menu li').hover(
			function () {
				//show its submenu
				$('ul', this).slideDown(200);

			}, 
			function () {
				//hide its submenu
				$('ul', this).stop(true, true).slideUp(200);			
			}
		);


		initTopMenu();
		
		//open some tab on the page. (searching for ?q=tabNumber
		openSomeTab();
    });

var activeMenuId = "";

function setLanguageToCookie() {
     var href = document.location.href;
     var r_exp = /[\?|&]lng=(\w+)/;
     result = r_exp.exec(href);
   	 if(result != null) {
		if(result[1] == 'en' || result[1] == 'ru') {        
        	document.cookie = "lng=" + result[1] + "; expires=Tue, 01-Jan-2030 00:00:01 GM; path=/; domain=.drweb.kz";
		}
    }
}
setLanguageToCookie();




function openSomeTab() {
	var href = document.location.href;
	var r_exp = /show=(\d+)/;
	result = r_exp.exec(href);
	if(result) {
			var tabNumber = parseInt(result[1]);
			if(tabNumber) { link_view(tabNumber); }
		}
}

function initTopMenu()
{
    
    if( activeMenuId )
	{
	    document.getElementById( activeMenuId ).className = "current";
	    return false;
	}


    var hostName = document.location.host;

    var hostnameRegex = new RegExp( "^(.+)\.drweb\.kz$" );
    var chkResult = hostnameRegex.exec( hostName );

    var idx = 0;
    if( chkResult )
	{
	    hostName = chkResult[ 1 ];

	    var menuElement = document.getElementById( "menu_" + hostName );

	    if( menuElement )
		{
		    activeMenuId = "menu_" + hostName;
		    menuElement.className = "current";
		} else
		{

		    if( hostName == "products" )
			{

			    var locationStr = window.location.pathname;

			    if( locationStr.indexOf( "/home" ) == 0 || locationStr.indexOf( "/win" ) == 0 || locationStr.indexOf( "/mac" ) == 0 || locationStr.indexOf( "/linux" ) == 0 || locationStr.indexOf( "/box" ) == 0 )
				{

				    activeMenuId = "menu_home";
				} else
				{
				    activeMenuId = "menu_business";
				}
			    document.getElementById( activeMenuId ).className = "current";
			}
		}
	}

    return false;
}

function resetMenu( object )
{
    if( activeMenuId && activeMenuId != object.id  )
	{
	    document.getElementById( activeMenuId ).className = "";
	}

    object.className = "current";

    return false;
}

function clearMenu( object )
{

    if( activeMenuId )
	{
	    if( activeMenuId != object.id )
		{
		    object.className = "";
		    document.getElementById( activeMenuId ).className = "current";
		    return false;
		}

	    return false;
	}
    object.className = "";

    return false;
}

function changeLng( l )
{
	var hrefStr = new String( window.location.href );
	
	if( l == "de" )
	{
	
	window.location.href = 'http://www.drweb-av.de/';
	return false;
	
	}
	
	if( l == "fr" )
	{
	
	window.location.href = 'http://www.drweb.fr/';
	return false;
	
	}
	
	var qIdx = hrefStr.indexOf( '?' );

	if( qIdx == -1 )
	{
		hrefStr = hrefStr + '?lng=' + l;
	} else
	{
		var justURL = new String( hrefStr.substr( 0, qIdx ) );
		var nIdx = qIdx + 1;
		var queryStrig = new String( hrefStr.substr( nIdx, hrefStr.length - nIdx ) ); 

		var argsArr = queryStrig.split( '&' );
		var i = 0;
		var foundLng = 0;

		for( i = 0; i < argsArr.length; i ++ )
		{
			var pArr = argsArr[ i ].split( '=' );
			var newArgs = new String( "" );

			if( pArr[ 0 ] == 'lng' )
			{
				foundLng = 1;
				pArr[ 1 ] = l;
			}
			newArgs = pArr.join( '=' );
			argsArr[ i ] = newArgs;
		}
		var newQueryString = argsArr.join( '&' );
		if( !foundLng )
		{
			newQueryString = newQueryString + '&lng=' + l;
		}
		hrefStr = justURL + '?' + newQueryString;
	}
	document.cookie = "lng=" + l + "; expires=Tue, 01-Jan-2030 00:00:01 GM; path=/; domain=.drweb.kz";
	window.location.href = hrefStr;

	return false;
}


function link_view(id) {

	var constId = "page";
	var i = 1;
	var stat = true;
	do
	{
		var newId = constId + i;
		if( document.getElementById( newId ) )
			{
				document.getElementById( newId ).style.display  = (i == id) ? 'block' : 'none';
				i++;
			} else {
				stat = false;
			}
	}
	while (stat);
}

$(document).ready(function(){
   $('.toggle_box').hide();
   $('.toggle_link').removeClass('active');
   $('.toggle_link').bind({
      'click': function(){
          var current_id = $(this).attr('id');
          $('.toggle_link').removeClass('active');
          $(this).toggleClass('active');
          $('.toggle_box').hide();
          $('#toggle_box_'+current_id).show();
          return false;
      }
   });
   $('.toggle_link').first().click();
});
