// JavaScript Document
$(document).ready(function()
{
//var path_auth='../';
if ($('div.auth').length>0)
	$('div.auth').load(path_auth+'auth_form.php');
 do_php_lin_cat_nav();
 copy_subnav();										   
 set_event_handlers();
 var mnem = $.getUrlVar('mnem');

 if (!mnem) mnem=cookieGetProf();
 // alert (mnem);
 if (mnem) select_nav(mnem,true);
 //alert ('1');
});

function do_php_lin_cat_nav()//для сайта делаем ссылки меню категорий на php
{
	if ($('div.auth').length>0)// только на сайте есть аутентификация
	$('#catnavholder a').each(function (){$(this).attr('href',str_replace('.html','.php',$(this).attr('href')))});
	}
	
function copy_subnav(){
	$('.subnav').hide();
	$('#nav > tbody > tr > td >  a ').not(':first').each(function(i) {
				$(this).after($('.subnav').eq(0).clone());//$(this).after($(#subnav).get(0).clone());		
			});
	}
	
function set_event_handlers()
{
	$('#nav > tbody > tr > td >  a ').each(function(i) { 							
		$(this).click(function() {
									  select_nav($(this).attr('id'),false);												
		return false; // Prohibit the browser to follow the link address
		});
	});
		
		
	$('.subnav a').each(function(i) { // Check each submenu element:
		$(this).click( function(){
			var mnem=$(this).parent().parent().parent().parent().prev().attr('id');
			var j=1+$(this).parent().parent().parent().find('a').index($(this));
			//alert(mnem+j);
			select_subnav(mnem,j);
			return false;
		});
		});
	}
	
function cookieSetProf(val) {
	$.cookie($('body').attr('id')+'_professiogram', val, {expires: null, path: '/'}); // Set mark to cookie (submenu is shown):
}

function cookieGetProf() {
	return $.cookie($('body').attr('id')+'_professiogram'); // Set mark to cookie (submenu is shown):
}

/*function cookieSet(index) {
	$.cookie('submenuMark-' + index, 'opened', {expires: null, path: '/'}); // Set mark to cookie (submenu is shown):
}
function cookieDel(index) {
	$.cookie('submenuMark-' + index, null, {expires: null, path: '/'}); // Delete mark from cookie (submenu is hidden):
}*/

function select_nav(mnem,force)
{
	//alert("selected "+mnem+' '+'ul#nav li a[id!="'+mnem+'"] ~ ul:visible');
	//alert("close "+$('ul#nav li a[id!="'+mnem+'"] ~ ul:visible').size());
$('#nav a[id!="'+mnem+'"] ~ table:visible').slideUp(600);
if (force || ($('#nav a[id="'+mnem+'"] ~ table:hidden').size()>0))//check opened alredy
	{
	$('#nav  a[id="'+mnem+'"] ~ table:hidden').slideDown(600);
	select_subnav(mnem,1);
	}else
	$('#nav a[id="'+mnem+'"] ~ table:visible').slideUp(600);//collapse if opened alredy
cookieSetProf(mnem);
}

function select_subnav(mnem,index){
$('body').attr('class','body_'+mnem);
$('body').addClass('subnav'+index);
//$('#info').attr('style','position:relative');
$('#infodiv').animate({"left": "750px"}, 200, 'swing', function() {
     $(this).load('data/'+mnem+'/'+index+'',function(){
//$('#info img').fadeTo(0,0.7);
$(this).animate({"left": "0px"}, 400, 'swing',function(){
	//$('#info img').fadeTo(1000,1);
});		

if ($('div.auth').length==0)// только если на сайте нет аутентификации
	$('a.link_testing').bind('click',function (){return false;});//hide();//спрятать ссылки на тесты
//если  на сайте, то заменяем в ссылках расширения
if ($('a.prof_navigator').length>0)
{if ($('div.auth').length>0)// только на сайте есть аутентификация
	$('a.prof_navigator').each(function (){$(this).attr('href',str_replace('.html','.php',$(this).attr('href')))});
	//else 
	 
	//alert ($('a.prof_navigator').attr('href'));
}
});
  });


//протоколируем  посещение
if ($('#logoutForm input[name="user"]').length>0)
if ($('#logoutForm input[name="user"]').attr('value')!='')
	{//$.get('fix_vizit.php?user='+$('#logoutForm input[name="user"]').attr('value')+'&mnem='+mnem+'&subnav='+index);
	var urll=encodeURI('fix_vizit.php?user='+$('#logoutForm input[name="user"]').attr('value')+'&mnem='+mnem+'&subnav='+index);
	//alert( urll);
	$.get(urll);}
if ($('#loginForm').length>0)
	$.get('fix_vizit.php?mnem='+mnem+'&subnav='+index);
}

function str_replace(search, replace, subject) {
    return subject.split(search).join(replace);
} 

$.extend({
  getUrlVars: function(){
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
      hash = hashes[i].split('=');
      vars.push(hash[0]);
      vars[hash[0]] = hash[1];
    }
    return vars;
  },
  getUrlVar: function(name){
    return $.getUrlVars()[name];
  }});

