function init()
{
	// quit if this function has already been called
	if (arguments.callee.done) return;

	// flag this function so we don't do the same thing twice
	arguments.callee.done = true;

	// kill the timer
	if (_timer)
	{
		clearInterval(_timer);
		_timer = null;
	}

	start();
}


 /* for Mozilla */
if (document.addEventListener)
{
	document.addEventListener("DOMContentLoaded", init, false);
}

/* for Safari */
if (/WebKit/i.test(navigator.userAgent))
{
	// sniff
	var _timer = setInterval(function()
	{
		if (/loaded|complete/.test(document.readyState))
		{
			init(); // call the onload handler
		}
	}, 10);
}

/* for other browsers */
window.onload = init;

var n=1;

function getN()
{
return n;
}

function incN()
{
n = n+1;
if(n == 21)
	{
	n=1;
	}
return n;
}

function change(img, ref)
{
  document.images[img].src = ref;
}

function setGlobalOnLoad(f)
{
var root = window.addEventListener || window.attachEvent ? window : document.addEventListener ? document : null;
if (root)
	{
	if (root.addEventListener) root.addEventListener("load", f, false);
	else if (root.attachEvent) root.attachEvent("onload", f);
	}
else
	{
	if(typeof window.onload == 'function')
		{
		var existing = window.onload;
		window.onload = function() {
			existing();
			f();
			}
		}
	else
		{
		window.onload = f;
		}
	}
}

var ua = navigator.userAgent.toLowerCase();
isIE = (ua.indexOf("msie") != -1 && ua.indexOf("opera") == -1 && ua.indexOf("webtv") == -1);

function change_href()
{
var elem = document.getElementById("nav_up").getElementsByTagName("a");
var elem_left = document.getElementById("nav_left").getElementsByTagName("a");
var loc = location.href.toString();
var loc_home = "http://www.d-green.ru";
var href_loc = loc.substr(loc_home.length);
var search;

if(isIE)
{
	for (var i=0, len=elem.length; i<len; i++)
	{
	      if(elem[i].getAttribute("href") == "http://www.d-green.ru/uslugi/blagoustrojstvo/")
		{
		elem[i].href ="#null";
		}
	      if(elem[i].getAttribute("href") == "http://www.d-green.ru/uslugi/ozelenenie/")
		{
		elem[i].href ="#null";
		}
	      if(elem[i].getAttribute("href") == "http://www.d-green.ru/prajs_list/")
		{
		elem[i].href ="#null";
		}
	}
	for (var j=0, len=elem_left.length; j<len; j++)
	{
	      if(elem_left[j].getAttribute("href") == "http://www.d-green.ru/uslugi/blagoustrojstvo/")
		{
		elem_left[j].href ="#null";
		}
	      if(elem_left[j].getAttribute("href") == "http://www.d-green.ru/uslugi/ozelenenie/")
		{
		elem_left[j].href ="#null";
		}

	search = elem_left[j].getAttribute("href").substr(loc_home.length);
	if(href_loc == search)
		{
		elem_left[j].setAttribute('className', 'active');
		}
	}
}
else
{
	for (var i=0, len=elem.length; i<len; i++)
	{
	      if(elem[i].getAttribute("href") == "/uslugi/blagoustrojstvo/")
		{
		elem[i].removeAttribute("href");
		elem[i].setAttribute("href", "#null");
		}
	      if(elem[i].getAttribute("href") == "/uslugi/ozelenenie/")
		{
		elem[i].removeAttribute("href");
		elem[i].setAttribute("href", "#null");
		}
	      if(elem[i].getAttribute("href") == "/prajs_list/")
		{
		elem[i].removeAttribute("href");
		elem[i].setAttribute("href", "#null");
		}
	}
	for (var j=0, len=elem_left.length; j<len; j++)
	{
	      if(elem_left[j].getAttribute("href") == "/uslugi/blagoustrojstvo/")
		{
		elem_left[j].removeAttribute("href");
		elem_left[j].setAttribute("href", "#null");
		}
	      if(elem_left[j].getAttribute("href") == "/uslugi/ozelenenie/")
		{
		elem_left[j].removeAttribute("href");
		elem_left[j].setAttribute("href", "#null");
		}
    }
}
}

function start()
{
 change_href();
 menu_center();
 set_left_down();
 set_right_down();
 menu_center();
}

function resize()
{
 menu_center();
 set_left_down();
 set_right_down();
}

function menu_center()
{
    var menu_w = top_h = parseInt(document.getElementById("menu_up_pos").offsetWidth);
	var loc = document.getElementById("nav_up").getElementsByTagName("li");
	var childsN = loc.length;
    var i=0, k=0;
    var nodeList = new Array(0);
    var ul_node;
    var total_w = 0; //общая ширина всех слов меню

    var menu_w = parseInt(document.getElementById("up_menu_cell").offsetWidth);
    var ul_w, free_w = menu_w, margin_w;

    for(i = 0; i < childsN; i++)
    {
     if(loc[i].childNodes.length > 1)
        nodeList.push(loc[i]);

     ul_node = loc[i].parentNode;

     if(ul_node.nodeName === "UL")
        if(ul_node.getAttribute("id") === "nav_up")
            {
                nodeList[k] = loc[i];
                free_w = free_w - parseInt(nodeList[k].offsetWidth);
                total_w = total_w + parseInt(nodeList[k].offsetWidth);
                k = k+1;
            }
    }
   margin_w = (free_w - 40)/ (k + 1);
   for(i = 0; i < k; i++)
    {
       nodeList[i].style.marginLeft = margin_w + "px";
    }
}

var no_free_left = false;

function set_left_down()
{
    var h, h_new;
    h = get_Height1();
    var top_h, news_h, site_h, left_menu_h;
    top_h = parseInt(document.getElementById("top").offsetHeight);
    news_h = parseInt(document.getElementById("news").offsetHeight);
    content_h = parseInt(document.getElementById("content").offsetHeight);
    left_menu_h = parseInt(document.getElementById("left_menu").offsetHeight);
    site_h = top_h + news_h + content_h;

    //Вставляем в нижнюю колонку подходщий по высоте цветок.
    var left_col_h = top_h + news_h + left_menu_h;
    var left_col_free = h - left_col_h;  //свободное место в левой колонке под меню.

 //выставляем парамерты верхней и нижней частей цветка под левым меню.
    h_new = Math.abs(h - top_h - news_h - left_menu_h);
    document.getElementById("left_down1").style.height = h_new + "px";

   //сделать проверку страницы по ширине. если ширина экрана меньше ширины сайта, то половы прокрутки на auto. Этот код вместе с предыдущей проверкой по высоте вынести в отдельную фукнцию и прописать ее в start и onresize.

    //устанавливаем фон под цветком
    var left_down2_h = parseInt(document.getElementById("left_down2").offsetHeight);
    var left_col_BGpos = left_down2_h + h - news_h - top_h;
    document.getElementById("left_col").style.backgroundPosition = 0 + "px " + left_col_BGpos + "px";
}

function set_right_down()
{
    var h, h_new;
    h = get_Height1();
    var top_h, news_h, site_h, left_menu_h;
    top_h = parseInt(document.getElementById("top").offsetHeight);
    news_h = parseInt(document.getElementById("news").offsetHeight);
    content_h = parseInt(document.getElementById("content").offsetHeight);
    news_list_h = parseInt(document.getElementById("news_list").offsetHeight);
    site_h = top_h + news_h + content_h;

    //Вставляем в нижнюю колонку подходщий по высоте цветок.
    var right_col_h = top_h + news_h + news_list_h;
    var right_col_free = h - right_col_h;  //свободное место в левой колонке под меню.

    h_new = Math.abs(h - top_h - news_h - news_list_h);
    document.getElementById("right_down1").style.height = h_new + "px";

    //устанавливаем фон под цветком
    var right_down2_h = parseInt(document.getElementById("right_down2").offsetHeight);
    var right_col_BGpos = right_down2_h + h - news_h - top_h;
    document.getElementById("right_col").style.backgroundPosition = 0 + "px " + right_col_BGpos + "px";
}


function get_Height1()
{
if(document.body.clientHeight)
    {
    var ie_height1 = document.body.clientHeight;//ie6
    var ie_height2 = document.documentElement.clientHeight;//ie7

    if(ie_height2 != 0) return (Math.min(ie_height1, ie_height2));
    else return (ie_height1);
    }
else
    return innerHeight;//opera miozilla
}


var scr='/js/cms/open.html';
function openImg(img)
{
var wn=window.open(scr+'?'+escape(img),'win','resizable,width=550,height=550,screenX=0,screenY=0,top=0,left=0');
wn.focus();
return false;
}

jsHover = function()
{
    var hEls = document.getElementById("nav_up").getElementsByTagName("LI");
    for (var i=0, len=hEls.length; i<len; i++) {
      hEls[i].onmouseover=function() { this.className+=" jshover"; }
      hEls[i].onmouseout=function() { this.className=this.className.replace(" jshover", ""); }
    }

    var hEls_left = document.getElementById("nav_left").getElementsByTagName("LI");
    for (var j=0, len=hEls_left.length; j<len; j++) {
      hEls_left[j].onmouseover=function() { this.className+=" jshover"; }
      hEls_left[j].onmouseout=function() { this.className=this.className.replace(" jshover", ""); }
    }

    if(document.getElementById("price_list"))
    {
        var hEls_price = document.getElementById("price_list").getElementsByTagName("a");
        for (var k=0, len=hEls_price.length; k<len; k++) {
          hEls_price[k].onmouseover=function() { this.className+=" jshover"; }
          hEls_price[k].onmouseout=function() { this.className=this.className.replace(" jshover", ""); }
        }
    }
}
  if (window.attachEvent && navigator.userAgent.indexOf("Opera")==-1) window.attachEvent("onload", jsHover);
