var objCount = 0; // количество изображений на web-странице
var result;
var new_win;
pics = new Array();

//var cookieList = document.cookie.split("; ");
//var cookieArray = new Array();
//var isUserLogin = 1;

var is_user_be_here;

//------------------- этот кусок кода перенесен с модуля авторизации ----------------------------
var cookieList = document.cookie.split("; ");
var cookieArray = new Array();
var isUserLogin = 1;
var check_auth = 0;

var user_login = '';
var ulogin = '';
var banner_open = 0;

for (var i = 0; i < cookieList.length; i++)
{
	var cookname2 = cookieList[i].split("=");
	if(unescape(cookname2[0])=="isUserLogin"){ isUserLogin = unescape(cookname2[1]);}
	if(unescape(cookname2[0])=="auto" && cookname2[1] == 1){ check_auth = 1;}
	if(unescape(cookname2[0])=="user_login" ){ user_login = cookname2[1];}
	if(unescape(cookname2[0])=="ulogin" ){ ulogin = cookname2[1];}
	if(unescape(cookname2[0])=="banner_open" ){ banner_open = cookname2[1];}



//alert(cookname2[0]);

}
//----------------------------------------------------------------------------------------------

/*for (var i = 0; i < cookieList.length; i++)
{
	var cookname2 = cookieList[i].split("=");
	if(unescape(cookname2[0])=="isUserLogin"){ isUserLogin = unescape(cookname2[1]);}
	if(unescape(cookname2[0])=="uniq_cookie_id"){ is_user_be_here = unescape(cookname2[1]);}
}*/
if (check_auth == 1 && isUserLogin == '') 
{
	document.write('<');
	document.write('script');
	document.write(' src=/js/auth.php?' + Math.random());
	document.write('></');
	document.write('script');
	document.write('>');
//	alert(is_user_be_here);
}



//-----------------------------------------
function getCookieVal( offset )
{
    var endstr = document.cookie.indexOf (";", offset);

    if (endstr == -1)
        endstr = document.cookie.length;
    return unescape(document.cookie.substring(offset, endstr));
}
function getcookie (name)
{
    var arg  = name + "=";
    var alen = arg.length;
    var clen = document.cookie.length;
    var i    = 0;

    while (i < clen)
    {
        var j = i + alen;
        if (document.cookie.substring(i, j) == arg)
          return getCookieVal (j);
        i = document.cookie.indexOf(" ", i) + 1;
        if (i == 0) break;
    }
    return "";
}
function DeleteCookie (name)
{
    var exp  = new Date();
    var cval = getcookie (name);

    exp.setTime (exp.getTime() - 1);  // This cookie is history
    document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}

function SetCookie(sName, sValue)
{
  document.cookie = sName + "=" + escape(sValue) + "; expires=Tue, 14 Dec 2010 23:59:59 GMT; path=/;";
}

//-----------------------------------------

function on_close_win()
{
	result = false;
}
function show_big_img(href_beg,win_w,win_h,img_src)
{
	if (result) result.close();
	left_pos = (screen.width - (1*win_w))/2;
	top_pos = (screen.height - (1*win_h))/2;
	set_scrollbars = "no";
	win_add_w = 0;
	if(left_pos<40)
	{
		left_pos = 40;
		win_w = screen.width-80;
		set_scrollbars = "yes";
	}
	tt_win_w = 1*win_w;
	if(top_pos<40)
	{
		top_pos = 20;
		win_h = screen.height-80;
		if(set_scrollbars == "no") tt_win_w += 18;
		set_scrollbars = "yes";
	}
	doc_text = "<HTML><HEAD><TITLE>Webo</TITLE></HEAD><BODY MARGINWIDTH=0 MARGINHEIGHT=0 LEFTMARGIN=0 RIGHTMARGIN=0 TOPMARGIN=0 BGCOLOR=white onunload='opener.on_close_win()'>";
	doc_text += "<div align=center><img src='"+img_src+"' border=0></div>";
	doc_text += "</BODY></HTML>";
	result = open("", "displayWindow","width="+tt_win_w+",height="+win_h+",left="+left_pos+",top="+top_pos+",resizable=yes,status=no,toolbar=no,menubar=no,scrollbars="+set_scrollbars);
	result.document.open();
	result.document.write(doc_text);
	result.document.close();
	result.focus();
}
function open_new_win(win_href,win_w,win_h)
{
	if (result) result.close();
	left_pos = (screen.width - (1*win_w))/2;
	top_pos = (screen.height - (1*win_h))/2;
	set_scrollbars = "no";
	win_add_w = 0;
	if(left_pos<40)
	{
		left_pos = 40;
		win_w = screen.width-80;
		set_scrollbars = "yes";
	}
	tt_win_w = 1*win_w;
	if(top_pos<40)
	{
		top_pos = 20;
		win_h = screen.height-80;
		if(set_scrollbars == "no") tt_win_w += 18;
		set_scrollbars = "yes";
	}
	result = open(win_href, "displayWindow","width="+tt_win_w+",height="+win_h+",left="+left_pos+",top="+top_pos+",resizable=no,status=no,toolbar=no,menubar=no,scrollbars="+set_scrollbars);
	result.focus();
}

function preload(name, first, second)
{
	// предварительная загрузка изображений и размещение их в массиве
	pics[objCount] = new Array(3);
	pics[objCount][0] = new Image();
	pics[objCount][0].src = first;
	pics[objCount][1] = new Image();
	pics[objCount][1].src = second;
	pics[objCount][2] = name;
	objCount++;
}

function on_out(type)
{
//	alert(type);
	if (this.event.srcElement && this.event.srcElement.src)
	{
		for (var i=0; i < objCount; i++)
		{
			if (pics[i][2] == this.event.srcElement.name)
			{
				if (type == 1) this.event.srcElement.src = pics[i][0].src;
				else this.event.srcElement.src = pics[i][1].src;
			}
		}
	}
}

function on(name)
{
	for (i = 0; i < objCount; i++)
	{
		if (document.images[pics[i][2]] != null)
			if (name != pics[i][2])
			{
				// вернуть в исходное систояние все другие изображения
				document.images[pics[i][2]].src = pics[i][0].src;
			}else{
				// показывать вторую картинку, поскольку курсор пересекает данное изображение
				document.images[pics[i][2]].src = pics[i][1].src;
			}
	}
}

function off()
{
	for (i = 0; i < objCount; i++)
	{
		// вернуть в исходное систояние все изображения
		if (document.images[pics[i][2]] != null)
			document.images[pics[i][2]].src = pics[i][0].src;
	}
}
function on_inp(el,img_src){el.src = img_src;}

function new_pas(href_beg)
{
	if (result) result.close();
	left_pos = (screen.width - 360)/2;
	top_pos = (screen.height - 200)/2;
	eval("result=window.open('"+href_beg+"/new_pas/','new_pas','resizable=no,menubar=no,status=no,scrollbars=no,width=360,height=185,left="+left_pos+",top="+top_pos+"')");
	result.focus();
}

function delivery(href_beg,wtype)
{
	if (result) result.close();
	left_pos = (screen.width - 360)/2;
	top_pos = (screen.height - 200)/2;
	eval("result=window.open('"+href_beg+"/delivery/"+wtype+"/','delivery','resizable=no,menubar=no,status=no,scrollbars=no,width=360,height=200,left="+left_pos+",top="+top_pos+"')");
	result.focus();
}

function go_delivery(p)
{
	if (document.deliv.y_email.value == '') 
	{
		alert('Введите Email');
		return false;
	}
	else
	{
		if (document.deliv.y_name.value == '' && document.deliv.wtype[0].checked)
		{
			alert('Введите Имя');
			return false;
		}
		else
		{
			if (result) result.close();
			left_pos = (screen.width - 360)/2;
			top_pos = (screen.height - 200)/2;
			result = window.open('','delivery','resizable=no,menubar=no,status=no,scrollbars=no,width=360,height=200,left=' + left_pos + ',top=' + top_pos);
			result.focus();
			document.deliv.submit();
		}
	}
}

function see_card(href_beg)
{
	if (result) result.close();
	left_pos = (screen.width - 360)/2;
	top_pos = (screen.height - 200)/2;
	eval("result=window.open('"+href_beg+"/','see','resizable=no,menubar=no,status=no,scrollbars=no,width=360,height=200,left="+left_pos+",top="+top_pos+"')");
	result.focus();
}

function go_basket(id, lp)
{
	if (!new_win || new_win.closed)
	{
		new_win = window.open(lp + '/eshop/gobasket/' + id + '/', 'bas', 'resizable=no,menubar=no,status=no,scrollbars=no,width=170,height=80');
		new_win.focus();
	}
	else
	{
		new_win.location.href = lp + '/eshop/gobasket/' + id + '/';
		new_win.focus();
	}
}

function basket_del()
{
	if(confirm('Вы действительно хотите удалить эти товары?')) 
	{
		document.fbasc1.go_tov_delete.value=1;
		document.fbasc1.submit()
	} 
	else return false;
}


function gocomparewin(href_beg,el_id,c_xcid)
{
	str = getcookie('compare_str');
	xcid = 0 + Number(getcookie('comp_xcid'));
	c_xcid = 0+Number(c_xcid);
	SetCookie('comp_xcid', c_xcid);
	if (c_xcid!=xcid) str = "";
var newArray = str.split("::");
if (newArray.length==3)
{
	alert('В таблицу стравнения может быть занесено не более 3 товаров одновременно!');
}
else
{
	DeleteCookie('compare_str');			
	if (str.indexOf(':'+el_id+':')<0)
		str = str+":" + el_id + ":";
	SetCookie('compare_str', str);
}
	
}

function over_menu(id)
{

}

function out_menu(id)
{

}

var my_timeout;
var last_open_menu;

function FIND(item) {
	if (document.all) return(document.all[item]);
	if (document.getElementById) return(document.getElementById(item));
	return(false);
}




function getBounds(element)
{
  var left = element.offsetLeft;
  var top = element.offsetTop;
  for (var parent = element.offsetParent; parent; parent = parent.offsetParent)
  {
    left += parent.offsetLeft - parent.scrollLeft;
    top += parent.offsetTop - parent.scrollTop
  }
  return {left: left, top: top, width: element.offsetWidth, height: element.offsetHeight};
}

if (document.layers) lrs = 1;
else lrs = 0;

var other_page = 1;	



function on_sub_menu(id, evt)
{
	if (my_timeout) clearTimeout(my_timeout);
	if (last_open_menu && last_open_menu > 0 && last_open_menu != id){
		$('#my_menu_' + last_open_menu).fadeOut('fast');
//		last_open_menu = 0; 
	}
	if (id != last_open_menu){
		$('#my_menu_' + id).fadeIn('fast');
	}
	last_open_menu = id;
}

function off_sub_menu(id)
{
	$('#my_menu_' + id).fadeOut('fast');
	return;
/*	if (!lrs)
	{
		if (FIND('my_menu_' + id))
			FIND('my_menu_' + id).style.visibility = 'hidden';
	}
	else
		if (document.layers['my_menu_' + id])
			document.layers['my_menu_' + id].visibility = 'hidden';
	out_menu(id);		*/
}

function sub_menu_over()
{
	if (my_timeout) 
		clearTimeout(my_timeout);
}

function prepare_sub_menu(id)
{
//	alert('dd');
	my_timeout = setTimeout('off_sub_menu(' + id + ')', 500);
	last_open_menu = id;
}

function off_sub_menu_prepare(id)
{
	my_timeout = setTimeout('off_sub_menu(' + id + ')', 500);
}


function show_a_big(ulr)
{
var show_img_win;

	if (!show_img_win || show_img_win.closed == true)
	{
		left_pos = (screen.width - 360)/2;
		top_pos = (screen.height - 200)/2;
		show_img_win = window.open('/show_big_img' + ulr,'','resizable=yes,menubar=no,status=no,scrollbars=no,width=100,height=100,left=' + left_pos + ',top=' + top_pos);
	}
	else
		show_img_win.document.location.href = '/show_big_img' + ulr;
	show_img_win.focus();
}


function openWindow(url, target, width, height, centered) {
    if (centered == true && (navigator.appName.indexOf("Netscape") > -1 && parseInt(navigator.appVersion.substring(0,1)) >=4 )
        || (navigator.appName.indexOf("Microsoft Internet Explorer") > -1 && parseInt(navigator.appVersion.substring(0,1)) >=4 )) {
        var w = (screen.width/2)-(width/2); 
        var h = (screen.height/2)-(height/2); 
        var parameter = "scrollbars=yes,resizable=yes";
        
        if (width != 0) {
            parameter += ',width=' + width + ', screenX=' + w + ',left=' + w;
        }
        if (height != 0) {
            parameter += ',height=' + height + ', screenY=' + h + ',top=' + h;
        }
    } else {
        var parameter = "scrollbars=yes,resizable=yes";
        if (width != 0) {
            parameter += ',width=' + width;
        }
        if (height != 0) {
            parameter += ',height=' + height;
        }
    }
    var win = window.open(url, target, parameter);
    if (win) {
        win.focus();
    }
}

var popup_win;

function check_popup_win()
{
	popup_open = getcookie('popup_open');
	if (popup_open!='1')
	{
		im_w = 300;
		im_h = 300;
		show_scroll = 'yes';
		if (popup_win == null || popup_win.closed == true) 
		popup_win = window.open("/popup/","popup_show","width="+im_w+",height="+im_h+",scrollbars="+show_scroll+",toolbar=no,status=no,resizable=yes,location=no"); 
		popup_win.document.location.href = '/popup/'
		popup_win.focus(); 
		DeleteCookie ('popup_open');
		document.cookie = 'popup_open' + "=" + '1' + "; path=/;";
	}
}



var cur_news_block = 0;
var tmp_img = new Image();
tmp_img.src = '/img/site/ico/str_right.png';
function move_top_news(dir){
	if (dir == 0){
		if (cur_news_block == 999){
			$('#top_news_0').slideDown('slow');
			$('#str_ico').attr('src', '/img/site/ico/str_down.png');
			cur_news_block = 0;
		}
		else{
			
			$('#top_news_' + cur_news_block).slideUp('slow');
			$('#str_ico').attr('src', '/img/site/ico/str_right.png');
			cur_news_block = 999;
		}
		return;
	}
	
	news_count = $('#top_news > div').length - 1;
	$('#top_news_' + cur_news_block).fadeOut('fast');
	cur_news_block = cur_news_block + dir;
	if (cur_news_block < 0) cur_news_block = news_count - 1;
	if (cur_news_block >= news_count) cur_news_block = 0;
	$('#top_news_' + cur_news_block).fadeIn('slow');
}