/* dopmenu.js */
var MaxLevel=20;
var oldBottomLayers=new Array(MaxLevel+1);
var oldTopLayers=new Array(MaxLevel+1);
var oldLDropLayers=new Array(MaxLevel+1);

for(i=0;i<=MaxLevel;i++){
   oldTopLayers[i]="";
   oldBottomLayers[i]="";
   oldLDropLayers[i]="";
}

function do_AllOut(){
  do_top_out();
  do_bottom_out();
  do_ldrop_out();
}

if(document.layers){
  window.captureEvents(Event.MOUSEDOWN);
  window.onmousedown=do_AllOut;
}else{
	document.onmouseup=do_AllOut;
}

function show_layer(x){
	if(x){
      x=document.getElementById(x);
      if(x)
         x.style.visibility="visible";
	}
}

function hide_layer(x){
	if(x){
      x=document.getElementById(x);
      if(x)
         x.style.visibility="hidden";
	}
}
/*=======================================================================*/

function do_bottom_menu(id, Level, ShowIt){
	for(i=MaxLevel;i>Level;i--)
	   if(oldBottomLayers[i]){
	     hide_layer(oldBottomLayers[i]);
	     oldBottomLayers[i]="";
	   }
	ShowIt=(oldBottomLayers[0]!="") || (ShowIt==1);
	if(oldBottomLayers[Level] && oldBottomLayers[Level]!=id)
	   hide_layer(oldBottomLayers[Level]);
	div=document.getElementById(id);
	if(Level>0 || ShowIt){
    td=id.substr(1,50);
		cell=document.getElementById(td);
 		if (cell){
 		   isTop=cell.className=="bottomMenuMainItem";
		   if (isTop){
		      offLeft=cell.offsetLeft;
		      offTop =cell.offsetTop - div.offsetHeight;
       }else{
          offTop =cell.offsetTop - div.offsetHeight+cell.offsetHeight;
          offLeft=cell.offsetLeft+div.offsetWidth-5;
       }
		   while(cell.offsetParent){
			   cell=cell.offsetParent;
			   offTop +=cell.offsetTop ;
			   offLeft+=cell.offsetLeft;
	     }
		   sub=document.getElementById(id);
		   if(sub){
         sub.style.left=offLeft+"px";
         sub.style.top =offTop +"px";
       }
    }
		show_layer(id);
		oldBottomLayers[Level]=id;
	}
}

function do_bottom_out(){
	for(i=0;i<=MaxLevel;i++)
	   if(oldBottomLayers[i]){
	     hide_layer(oldBottomLayers[i]);
	     oldBottomLayers[i]="";
	   }
}

/*=======================================================================*/
function do_top_menu(id, Level, ShowIt){
	for(i=MaxLevel;i>Level;i--)
	   if(oldTopLayers[i]){
	     hide_layer(oldTopLayers[i]);
	     oldTopLayers[i]="";
	   }

	ShowIt=(oldTopLayers[0]!="") || (ShowIt==1);
	if(oldTopLayers[Level] && oldTopLayers[Level]!=id)
	   hide_layer(oldTopLayers[Level]);

	if(Level>0 || ShowIt){
		td=id.substr(1,50);
		cell=document.getElementById(td);
 		if (cell){
 			isTop=cell.className=="topMenuMainItem";
			if (isTop){
				offLeft=cell.offsetLeft+ 10;
				offTop =cell.offsetTop + cell.offsetHeight ;
			}else{
				offTop =cell.offsetTop +5;
				offLeft=cell.offsetLeft+ cell.offsetWidth-5;
			}
			while(cell.offsetParent){
			   cell=cell.offsetParent;
			   offTop +=cell.offsetTop ;
			   offLeft+=cell.offsetLeft;
			}
			sub=document.getElementById(id);
			if(offLeft+sub.offsetWidth>document.body.offsetWidth)
				offLeft=document.body.offsetWidth-sub.offsetWidth-100;
			if(sub){
				sub.style.left=offLeft+"px";
				sub.style.top =offTop +"px";
			}
		}
		show_layer(id);
		oldTopLayers[Level]=id;
	}
}

function do_top_out(){
	for(i=0;i<=MaxLevel;i++)
	   if(oldTopLayers[i]){
	     hide_layer(oldTopLayers[i]);
	     oldTopLayers[i]="";
	   }
}

/*=======================================================================*/
function do_ldrop_menu(id, Level, ShowIt){
	for(i=MaxLevel;i>Level;i--)
	   if(oldLDropLayers[i]){
	     hide_layer(oldLDropLayers[i]);
	     oldLDropLayers[i]="";
	   }

	ShowIt=(oldLDropLayers[0]!="") || (ShowIt==1);
	if(oldLDropLayers[Level] && oldLDropLayers[Level]!=id)
	   hide_layer(oldLDropLayers[Level]);

	if(Level>0 || ShowIt){
		td=id.substr(1,50);
		cell=document.getElementById(td);
 		if (cell){
 			isTop=cell.className=="ldropMenuMainItem";
			if (isTop){
				offLeft=cell.offsetLeft+ cell.offsetWidth-5;
				offTop =cell.offsetTop + 5;
			}else{
				offTop =cell.offsetTop +5;
				offLeft=cell.offsetLeft+ cell.offsetWidth-5;
			}
			while(cell.offsetParent){
			   cell=cell.offsetParent;
			   offTop +=cell.offsetTop ;
			   offLeft+=cell.offsetLeft;
			}
			sub=document.getElementById(id);
			if(offLeft+sub.offsetWidth>document.body.offsetWidth)
				offLeft=document.body.offsetWidth-sub.offsetWidth-100;
			if(sub){
				sub.style.left=offLeft+"px";
				sub.style.top =offTop +"px";
			}
		}
		show_layer(id);
		oldLDropLayers[Level]=id;
	}
}

function do_ldrop_out(){
	for(i=0;i<=MaxLevel;i++)
	   if(oldLDropLayers[i]){
	     hide_layer(oldLDropLayers[i]);
	     oldLDropLayers[i]="";
	   }
}

/*site.tree.js*/
function tree_toggle( elemName, AParent ){
     $("#"+elemName).slideToggle("slow", function(){
        $("#"+AParent+" img").attr('src', $("#"+elemName).css('display')=='none'?'/design/tree_closed.gif':'/design/tree_open.gif');
     });
}

/* shiftmenu.js */
disp=(document.all&&!window.innerWidth)?'block':'table';
function toggle( elemName ){
var elem=document.getElementById(elemName);
	if ( elem ){
		if ( elem.style.display != disp )
			elem.style.display = disp ;
		else
			elem.style.display = 'none' ;
	}
  if (elem.style.display=='none')
		var cook=elemName+'=0;';
	else
		var cook=elemName+'=1;';
   document.cookie=cook+'path=/;';
	return true;
}

function GetCookie(sName){
  var aCookie = document.cookie.split('; ');
  for (var i=0; i < aCookie.length; i++){
    var aCrumb = aCookie[i].split('=');
    if (sName == aCrumb[0])
       return unescape(aCrumb[1]);
  }
  return null;
}

function CheckSubVisibility(menuId, maxSub){
   for(i=1; i<maxSub; i++){
      eName='sub'+menuId+"_"+i;
      e=elem=document.getElementById(eName);
      if (e){
         vis=GetCookie(eName);
         if (vis && vis==1)
           e.style.display=disp;
         else
           e.style.display='none';
      }
   }
}
/* common */

/* this project */
function highLightMenu(tpl, html, pgid){
    switch(tpl){
        case 'tpl-main':
            switch(html){
                case '/':
                case '/': $("#mli-1").addClass('selected').attr('href','#');
                break;
            }
            break;
        case "tpl-event": $("#mli-2").addClass('selected').attr('href','#'); break;
        case "tpl-gallenter": $("#mli-3").addClass('selected').attr('href','#'); break;
        case "tpl-arena": $("#mli-4").addClass('selected').attr('href','#'); break;
        case "tpl-article": $("#mli-5").addClass('selected').attr('href','#'); break;
        case "tpl-price": $("#mli-6").addClass('selected').attr('href','#'); break;
        case "tpl-cont": $("#mli-7").addClass('selected').attr('href','#'); break;
        case "tpl-equip": $("#mli-16").addClass('selected').attr('href','#'); break;
        case "tpl-people": $("#mli-19").addClass('selected').attr('href','#'); break;
        case "tpl-toys":
        case "tpl-manuf":
        case "prodlist":
             $("#mli-17").addClass('selected').attr('href','#'); break;
    }
}

/*====== форма поиска ========*/
function getPrompt(e){

    var exp = new Date();
    exp.setTime(exp.getTime() + (60*60*1000));
    setCookie('lastSearch', encodeURIComponent($('#searchStr').val()), exp, '/'); /*encodeURIComponent*/
    if(27==e.which){
        $('#prompt').fadeOut('slow');
        $('#searchForm').focus();
        return;
    }
    if((40==e.which) && ('none'!=$('#prompt').css('display'))){ /*down*/
        $('#prompt').focus();
        return;
    }
    if('none'==$('#prompt').css('display')){
        $('#prompt').css({  'left':$('#searchStr').offset().left-$('#prompt').width()+$('#searchStr').width()+4+'px',
                            'top' :$('#searchStr').offset().top+$('#searchStr').height()+4+'px'});
        $('#prompt').fadeIn('slow');
    }
    $('#prompt').load('/tpl.search.x.php', {'string':$('#searchStr').val()},
        function(){
            $('#prompt')[0].selectedIndex=-1;
        }
    );
}

function doSelect(){
    $('#searchStr').val($('#prompt').val());
    $('#prompt').fadeOut('slow');
}

$(document).ready(function(){

    $('#searchStr').keyup(function(e){getPrompt(e)});
    $('#searchForm').submit(function(){var exp = new Date();exp.setTime(exp.getTime() + (60*60*1000));setCookie('lastSearch', encodeURIComponent($('#searchStr').val()), exp, '/');})
    $('#prompt').keyup(function(e){
        if (13==e.which){
            $('#searchStr').val($('#prompt').val());
            $('#prompt').fadeOut('slow', function(){
                $('#searchForm').submit();
            });
        }
        if (27==e.which){
            $('#prompt').fadeOut('slow');
            $('#searchForm').focus();
        }
    });
});


/* напоминалка пароля */
function doReminder(){
var mailRegex = new RegExp("^[_\\.0-9a-z-]+@([0-9a-z][0-9a-z_-]+\\.)+[a-z]{2,4}$");
var email='';
    if(email=prompt('Введите адрес электропочты указанный при регистрации', email)){
        $().ajaxStop(function(){$.unblockUI();});
        $.blockUI.defaults.overlayCSS={backgroundColor: '#555',opacity:'0.5'}
        $.blockUI("<h1 style='text-align:center'><img src='/design/ext/lb.loading.gif' alt='ожидаем'><br>ожидаем...</h1>");

        $('#reminder').load("/tpl.basket.x.php", {"email":email}, function(){
            $('#reminder').show("slow");
        });
    }
}
/* eof напоминалка пароля */

/*====== покупка ========*/
function doBuy( pageId ){
    $().ajaxStop(function(){$.unblockUI();});
    $.blockUI.defaults.overlayCSS={backgroundColor: '#555',opacity:'0.5'}
    $.blockUI("<h1 style='text-align:center'><img src='/design/ext/lb.loading.gif' alt='ожидаем'><br>покупаем...</h1>");
    $("#basket").load("/tpl.basket.x.php",{"buyid":pageId});
}

/*====== голосование за перца ========*/
function doVote( o_kom, ratio, kto ){
    $().ajaxStart(function(){$.blockUI("<h1 style='text-align:center'><img src='/design/ext/lb.loading.gif' alt='ожидаем'><br>выносим<br>приговор</h1>");});
    $().ajaxStop(function(){$.unblockUI();});
    $.get("/tpl.people.x.php", {"action":"vote", "o_kom":o_kom, "ratio":ratio},
        function(data){
            resp=eval(data);
            $('#graph'+o_kom).css('width', resp.voteResult+'px');
            $('#summ'+o_kom).html(resp.sumRating );
            $('#voters'+o_kom).html(resp.voterCnt );

    });
}
