/*
 * Возможные настройки
 * 1. Время до вызова следующей фазы смещения
 * 2. Время ( шаг ) анимации движения линии
 * 3. ????
 **/

String.prototype.multiply_string=function(_num)
{
    var str='';

    while(_num--)
    {
        str+=this.toString()
    }
    return str;
};

var phline_сustom=
    {
        time: 0.1, // секунд
        pretime: 5, // секунд
        step: 5 // пикселей
    }
function phline_premove()
{
    //console.debug('-------');
    setTimeout("phline_move()",seconds(phline_сustom.pretime));
}
function
phline_move()
{
/// Движение линии
var tr   = document.getElementById("phline_in_frame");
var pos = parseInt(tr.style.left);
var dist = tr.getElementsByTagName("td")[0].offsetWidth;

if( Math.abs(pos) <= dist )
{
    pos-=phline_сustom.step; tr.style.left=pos+"px";
    //console.debug('pos=%d; dist=%d', Math.abs(pos) , dist);
    setTimeout("phline_move()",seconds(phline_сustom.time));
}
else
{
    phline_refresh();
}

}

function
phline_refresh()
{
/// Обновление линии после фазы сдвига
var main = document.getElementById("phline_cont");
var add  = main.firstChild;

main.appendChild(add);

document.getElementById("phline_in_frame").style.left="0px";
phline_premove();
}

function
phline_start_onload(_obj)
{
    _obj.onload=null;
    phline_premove()
}

function
phline_start()
{
/// запуск
// генерация тегов img
var data=document.getElementById('phline_data').value.split(',');
var strlevel='../'.multiply_string(document.getElementById('phline_lvl').value);
var str='<div id="phline_frame"><div id="phline_in_frame"><table height="67"><tbody><tr id="phline_cont">';
str+='<td><div class="phline_item"><img onload="phline_start_onload(this)" src="'+strlevel+'imgs/menu_ban/'+data.shift()+'" alt="" /></div></td>';

while(data.length)
{
    str+='<td><div class="phline_item"><img src="'+strlevel+'imgs/menu_ban/'+data.shift()+'" alt="" /></div></td>';
}
document.getElementById('phline_insert').innerHTML=str+'</tr></tbody></table></div></div>';
document.getElementById("phline_in_frame").style.left="0px";
//phline_premove();
}
function
seconds(_ii)
{
    return _ii*1000;
}var ie=false;

function init(){
	if(navigator.appName.substring(0,3)=="Mic") {ie=true;}
	
	var selobj = document.getElementById("selectedmenu"), mmenuobj = document.getElementById("mmenu").getElementsByTagName('td'), sumposition=0, optmenuobj = document.getElementById("optmenu");
	for(ii=0;ii < mmenuobj.length;ii++){
		var divobj=document.createElement('div'), widthelement=mmenuobj[ii].scrollWidth, divoptobj=document.createElement('div');
		divobj.innerHTML = "<table><tbody><tr><td style=\"width: 8px; height: 28px; background-image: url('../../imgs/choice_l.gif')\"></td><td style=\"width: "+(widthelement-20)+"px; height: 28px; background-image: url('../../imgs/choice_m.gif')\"></td><td style=\"width: 8px; height: 28px; background-image: url('../../imgs/choice_r.gif')\"></td></tr></tbody></table>";
		divobj.setAttribute('id','selectmenu'+ii); divoptobj.setAttribute('id','optmenu'+ii);
	
		sumposition+=widthelement;
		
		selobj.appendChild(divobj);
		optmenuobj.appendChild(divoptobj);

		document.getElementById('selectmenu'+ii).style.position = 'absolute';
		document.getElementById('selectmenu'+ii).style.visibility = 'hidden';
		document.getElementById('selectmenu'+ii).style.marginLeft = (sumposition+5-widthelement)+'px';
		document.getElementById('optmenu'+ii).style.position = 'absolute';
		document.getElementById('optmenu'+ii).style.cursor = 'pointer';
		document.getElementById('optmenu'+ii).style.marginLeft = (sumposition+5-widthelement)+'px';
		document.getElementById('optmenu'+ii).style.width = (widthelement-4)+'px';
		document.getElementById('optmenu'+ii).style.height = '28px';
		
		try{
			document.getElementById('sm_'+ii).style.marginLeft = (sumposition+5-widthelement)+'px';
			document.getElementById('sm_'+ii).style.zIndex = 1000;
			
			var smdiv=document.getElementById('sm_'+ii).getElementsByTagName('div');
			for(jj=0;jj < smdiv.length;jj++){
				smdiv[jj].onmouseover=function(){
					this.style.border = '1px solid #e59360';
					this.style.backgroundColor = '#9f4c18';
				}
				smdiv[jj].onmouseout=function(){
					this.style.border = '1px solid #c46327';
					this.style.backgroundColor = '#c46327';
				}
				smdiv[jj].onclick=function(){
					document.location.href=this.getElementsByTagName('a')[0].href;
				}
			}
		} catch(e){}
		
		if(ie){ // затычка для тупого IE
			document.getElementById('optmenu'+ii).style.backgroundColor = "#ffecd5";
			document.getElementById('optmenu'+ii).style.filter = 'progid:DXImageTransform.Microsoft.Alpha(opacity=1)';
		}

		var activeobj = optmenuobj.getElementsByTagName('div')[ii];
		activeobj.setAttribute('name',ii);
		
		activeobj.onmouseover=function(){
			document.getElementById("selectmenu"+this.getAttribute('name')).style.visibility = "visible";
			document.getElementById('mmenu').getElementsByTagName('a')[this.getAttribute('name')].style.color = '#ffffff';
			try{clear_all_smenu(); document.getElementById('sm_'+this.getAttribute('name')).style.visibility = 'visible';} catch(e){
				clear_all_smenu();
			}
		}
		activeobj.onmouseout=function(){
			document.getElementById("selectmenu"+this.getAttribute('name')).style.visibility = "hidden";
			document.getElementById('mmenu').getElementsByTagName('a')[this.getAttribute('name')].style.color = '#505050';
		}
		activeobj.onclick=function(){
			if(document.getElementById('mmenu').getElementsByTagName('a')[this.getAttribute('name')].getAttribute('target')=='_blank'){
				window.open(document.getElementById('mmenu').getElementsByTagName('a')[this.getAttribute('name')].href);
			} else {
				document.location.href=document.getElementById('mmenu').getElementsByTagName('a')[this.getAttribute('name')].getAttribute('href');
			}
		}
		mmenuobj[ii].getElementsByTagName('a')[0].onclick=function(ev){
			if(ie){event.cancelBubble = true;} else {ev.cancelBubble = true;}
		}
	}
	phline_start();
}

function clear_all_smenu(){
	var mmenuobj = document.getElementById("mmenu").getElementsByTagName('td');
	for(ii=0;ii < mmenuobj.length;ii++){
		try{
			var smdiv=document.getElementById('sm_'+ii).style.visibility = 'hidden';
		} catch(e){}
	}
}

function openWindow(Wlink, Ht, Wt) {
var sizex="toolbar=auto, menubar=no, Height=" + Ht + ", Width=" + Wt + ", left=130, top=100"
newWindow = window.open(Wlink,"newWindow", sizex);
return false
}

function open_gallery (cat) {
	var url_str="../../view_ph_gal.php?my_dir="+cat;
	window.open(url_str,"view","WIDTH=475, HEIGHT=582, TOOLBAR=NO, STATUS=NO, RESIZEBLE=NO, TITLEBAR=NO");
	return false
}

function jumpmenu(){
	document.getElementById('metro_search').submit();
}

