var curnode;
var cursubnode;
var lowcolor = '';
var menuid;
var sb;
var sbcolor = '';

/* Onload function
*/
function addLoadEvent(func) { 
	var oldonload = window.onload; 
	if (typeof window.onload != 'function') { 
		window.onload = func; 
	} else { 
		window.onload = function() { 
			if (oldonload) { 
				oldonload(); 
			} 
			func(); 
		} 
	} 
} 

/* Menu functions 
 *
*/
function contains (container, containee) {
	while (containee) {
		if (container == containee) {
			return true;
		}
		containee = containee.parentNode;
	}
	return false;
}

function init_menu(parent, child, suffix, lc) {
	curnode = document.getElementById('up'+parent+suffix);
	cursubnode = document.getElementById('sub'+child+suffix);
	lowcolor = lc;	
	menuid = suffix + '_menu';
	sb = document.getElementById('forsubmenu');
	if (sb)
		sbcolor = lc;

	switch_menu(parent, menuid, lc);
	select_menu(curnode, lc);
}

function restore_menu(div, e) {
	if (!e) {
		e = window.event;
	}
	if (!contains(div, e.relatedTarget || e.toElement)) {
		if (curnode) {
			switch_menu(curnode, menuid);
			select_menu(curnode, lowcolor);
		}
		if (cursubnode) {
			select_submenu(cursubnode);
		}
		if (sb) 
			sb.style.background = sbcolor ? sbcolor : 'none';
	}
}

function switch_menu(id, parent_id, color){
	var parentelem =  document.getElementById(parent_id);
	if (parentelem) {
		var count = parentelem.childNodes.length;
		for(var i=0; i<count; i++){
        		var up_elem = parentelem.childNodes[i];
			var down_elem;
			if (up_elem.nodeType != 1) continue;
			for (var j=0; j<up_elem.childNodes.length; j++) {
				if (up_elem.childNodes[j].nodeType == 1) { 
					down_elem = up_elem.childNodes[j];
				}
			}
			if (up_elem && down_elem) {
				if ('up'+id+'top' == up_elem.getAttribute("id")) {
                				down_elem.style.visibility='visible';
						if (sb) sb.style.background=lowcolor;
            			} else down_elem.style.visibility='hidden';
			}
    		}
	}
}

function select_menu(item, lc) {
	if (!item)
		return;
	if (item.childNodes[0] && item.childNodes[0].style) {
		item.childNodes[0].style.background=lc + ' url(http://fresh-images.brandproduction.ru:80/fon_butt.png)';
		item.childNodes[0].style.color='#000000';
	}
	if (sb)
		sb.style.background=lc;
	if (item.childNodes[1] && item.childNodes[1].style)
		item.childNodes[1].style.visibility='visible';
	if (curnode && item != curnode)
		deselect_menu(curnode);
}

function deselect_menu(item) {
	if (item.childNodes[0] && item.childNodes[0].style) {
		item.childNodes[0].style.color='#ffffff';
		item.childNodes[0].style.background='none';
	}
	if (item.childNodes[1] && item.childNodes[1].style)
		item.childNodes[1].style.visibility='hidden';
}

function select_submenu(item) {
	if (!item)
		return;
	item.style.textDecoration = 'underline';
	if (cursubnode && cursubnode != item)
		deselect_submenu(cursubnode);
}

function deselect_submenu(item) {
	item.style.textDecoration = 'none';
}

function restructure(parent_id){
	var parentelem =  document.getElementById(parent_id);
	if (!parentelem)
		return;

	var count = parentelem.childNodes.length;
	var up_elem;
	var down_elem;
	var field1;
	var field2;
	for(var i=0; i<count; ++i){
        up_elem = parentelem.childNodes[i];
		field1 = 1000 - up_elem.offsetLeft;
	    field2 = up_elem.offsetLeft + up_elem.offsetWidth;
		if (up_elem.nodeType != 1) continue;
		for (var j=0; j<up_elem.childNodes.length; ++j){
			if (up_elem.childNodes[j].nodeType == 1) { 
				down_elem = up_elem.childNodes[j]; 
			}
		}
		if (up_elem && down_elem) {	
			down_elem.style.left = 0 + 'px';
	        if(down_elem.offsetWidth<=field1){
		            down_elem.style.left=up_elem.offsetLeft + 'px';
	        }
	        else if((down_elem.offsetWidth<=field2)&&(down_elem.style.textAlign=='')){
		            down_elem.style.left = 0 + 'px';
		            down_elem.style.width = field2 + 'px';
		            down_elem.style.textAlign = 'right';
	        }
	        else if(down_elem.style.textAlign==''){
		            down_elem.style.width = 1000 + 'px';
		            down_elem.style.textAlign = 'right';
	       }	
		}
	}	
}
	
/* Other functions 
 *
*/

function create_post(id,xinha_ids) {
	var elem = document.getElementById(id);
	elem.style.display = 'block';
	if (!xinha_editors || xinha_editors.length == 0) {
		call_xinha(xinha_ids,'auto');
	}
}
//данная функция показывает/скрывает элемент с идентификатором=id
function show(id) {
	var elem = document.getElementById(id);
	if(elem){
	  var flag = elem.style.display;
	  elem.style.display = flag == 'none' ? 'block' : 'none';
	}
}
function show_el(elem) {
	if(elem.length){
	  for(var i=0; i<elem.length; ++i){
	    var flag = elem[i].style.display;
	    elem[i].style.display = flag == 'none' ? 'block' : 'none';
	  }
	}
	else{
	  var flag = elem.style.display;
	  elem.style.display = flag == 'none' ? 'block' : 'none';
	}
}
function showblock(id1,id2,src1,src2) {
	show(id1);
	var elem = document.getElementById(id2);
	if (elem.getAttribute('src')==src1) elem.setAttribute('src',src2);
	else elem.setAttribute('src',src1);

}
function subform(id){
	var form = document.getElementById(id);
	if(form){
		form.submit();
	}
}
function resform(id){
	var form = document.getElementById(id);
	if(form){
		form.reset();
	}
}
function focusEl(id){
	var elem = document.getElementById(id);
	if(elem){
		elem.focus();
	}
}
//данная функция располагает элемент с идентификатором=id1 после элемента с идентификатором=id2
function showpost(id1, id2) {
	var elem1 = document.getElementById(id1);
	var elem2 = document.getElementById(id2);
	if(elem1 && elem2){
      elem1.style.top = elem2.offsetTop + elem2.offsetHeight - 30 + 'px';
	  show(id1);
	}
}
//данная функция меняет содержимое элемента с идентификатором=id на текст=text
function chtext(id, text){
	if(id && text){
		var el   = document.getElementById(id);
		var inel = el.childNodes[0];
		el.removeChild(inel);
		inel = document.createTextNode(text);
		el.appendChild(inel);
	}
}
function dark(id) {
  show(id);
  var dark = document.getElementById(id);
  if(dark){
	  dark.style.height = document.documentElement.scrollHeight + 'px';
  }
}

function destroy_other(id) {
	var elems = document.getElementsByTagName("div");
	for (var i=0; i<elems.length; i++) {
		if (elems[i].id.match(/ans/i) && id != elems[i].id) {
				elems[i].innerHTML='';
		}  
	}
}

/* Ротация баннеров
 *	place_id  - идентификатор места
 *	images - массив урлов изображений
 *	urls - массив ссылок
*/
function banner_rotate (place_id, images, urls) {
    var img = document.getElementById(place_id+'_img');
    var url = document.getElementById(place_id+'_href');
    var i = 0;
    img.setAttribute('src', images[i%images.length]);
    url.setAttribute('href', urls[i%urls.length]);
    if (images.length > 1) {
                setInterval(
                        function() {
                                i++;
                                img.setAttribute('src', images[i%images.length]);
                                url.setAttribute('href', urls[i%urls.length]);
                                url.setAttribute('target', '_blank');
                        },
                10000);
   }	 

}

function promo_rotate (paths, names, urls, npaths, ppaths, node_id, bgrcolor) {
	var img_path = document.getElementById('promoimg');
	var img_href = document.getElementById('promohref');
    	var href_prev = document.getElementById('arrow_prev');
    	var href_next = document.getElementById('arrow_next');
    	var i = 0;
	if (paths.length > 1) {
                setInterval(
                        function() {
                                i++;
                                img_path.setAttribute('src', paths[i%paths.length]);
                                img_href.setAttribute('href', urls[i%urls.length]);
				href_next.setAttribute('href', '/db/gallery.html'+'?node_id='+node_id+'&amp;bgcolor='+bgrcolor+'&amp;card_id='+npaths[i%npaths.length]);
				href_prev.setAttribute('href', '/db/gallery.html'+'?node_id='+node_id+'&amp;bgcolor='+bgrcolor+'&amp;card_id='+ppaths[i%ppaths.length]);
                        },
                10000);
   	}	


}

/*
function same_height(){
        var cntrright = document.getElementById('cntrright');
        var cntrleft = document.getElementById('cntrleft');
        if (cntrright && cntrleft) {
                cntrleft.style.height = cntrright.offsetHeight + 'px';
        }
}
*/

function select_tab(id, img_big, arr, open_card) {
                var imgbig = document.getElementById('category');
                imgbig.style.backgroundImage  = 'url('+img_big+')';
		imgbig.setAttribute('onclick', 'window.open(\''+open_card+'\');');
                for (var i=0; i<arr.length; i++) {
                        var tab = document.getElementById('tab'+arr[i]);
                        var imgtab = document.getElementById('imgtab'+arr[i]);
                        var nclass;
                        var img;
                        var width;
                        var height;
                        var leftmargin = '0px';
                        if (i != 0) {
                                leftmargin='-30px';
                        }
                        if (arr[i] == id) {
                                nclass="menu3";
                                img='http://fresh-images.brandproduction.ru:80/fon_menu6.png';
                                width='36px';
                                height='28px';
                        } else {
                                nclass="menu4";
                                img='http://fresh-images.brandproduction.ru:80/fon_menu8.png';
                                width='30px';
                                height='24px';
                        }
                        tab.className=nclass;
                        tab.style.marginLeft = leftmargin;
                        tab.style.zIndex = 6-i;
                        imgtab.setAttribute('src', img);
                        imgtab.style.width = width;
                        imgtab.style.height = height;
                }

}

