﻿//function btnBack_Click(obj)
//{
//    //Переключится на предыдущий таб
//}
//function btnNext_Click(obj)
//{
//	//Переключится на следующий таб
//}

_ctrl = false;
function body_KeyDown(e)
{
    var e = e ? e : window.event;
    var key = e.keyCode ? e.keyCode : e.which;
    if(key == 17)
        _ctrl = true; 
    else
    {
        if(_ctrl)
        {
            //if(key == 37)
                //btnBack_Click();
            //else if(key == 39)
                //btnNext_Click();
        }
    }
}
function body_KeyUp(e)
{
    var e = e ? e : window.event;
    var key = e.keyCode ? e.keyCode : e.which;
    if(key == 17)
        _ctrl = false;
}
function body_Load()
{
}


var $toplist = 
{
    Row_Click:function(obj)
    {
        if(obj)
        {
            var row = $global.GET(obj, "up:2");
            if(row)
                $global.SwapClasses('Row', 'Row Active', row);
        }
    }
}
var $tabs = 
{
	Const:
	{
		TabPrefix:"tab:",
		PanelPrefix: "panel:"
	},
	ContainsTab:function(id)
	{
		return this.GetTab(id) != null;
	},
	GetTab:function(id)
	{
		return document.getElementById(this.Const.TabPrefix+id);
	},
	AddTab:function(ul, id)
	{
		var tab = document.createElement("li");
		tab.id = this.Const.TabPrefix+id;
		var a = document.createElement("a");
		a.title = "Информация о домене";
		a.href = "javascript:void(0)";
		a.onclick = function(){ $tabs.Tab_Click(this); };
		tab.appendChild(a);
		var span = document.createElement("span");
		span.innerHTML = id.toString();
		a.appendChild(span);
		ul.appendChild(tab);
		if(this.TabsWidth(ul) > this.Width(ul))
			$global.Display.Hide(tab);
		return tab;
	},
	AddPreloader:function(tab)
	{
		if(tab)
		{
			var span = $global.GET(tab, "a:0,span:0");
			if(span)
			{
				if($global.GET(span, "img:0") == null)
				{
					var img = document.createElement("img");
					img.alt = " ";
					img.src = "resource/ajax-loader.gif";
					span.appendChild(img);
				}
			}
		}
		return this;
	},
	RemovePreloader:function(tab)
	{
		if(tab)
		{
			var img = $global.GET(tab, "a:0,span:0,img:0");
			if(img)
			{
				img.parentNode.removeChild(img);
			}
		}	
		return this;
	},
    Areas:function(ul)
    {
        var result = new Array();
        var tabs = this.Tabs(ul);
        for(var i = 0;i< tabs.length;i++)
        {
            if(tabs[i].id)
            {
                var str = tabs[i].id.split(",");
                if(str.length > 1)
                {
                    for(var j=0;j<str.length;j++)
                    {
                        if(str[j].indexOf("area:") != -1)
                        {   
                            var area = document.getElementById(str[j].replace('area:', ''));
                            if(area)
                                result.push(area);
                        }
                    }
                }
            }
        }
        return result;
    },
    GetArea:function(li)
    {
        var result = null;
        if(li)
        {
            var id = li.id;
            if(id)
                result = document.getElementById(id.replace(this.Const.TabPrefix, this.Const.PanelPrefix));
        }
        return result;
    },
    SelectedTab:function(ul)
    {
        var result = null;
        if(ul)
        {
            var tabs = this.Tabs(ul);
            for(var i=0;i<tabs.length;i++)
            {
                if(tabs[i].className == "Selected")
                {
                    result = tabs[i];
                    break;
                }
            }
        }
        return result;
    },
    Tab_Click:function(obj)
    {
        var ul = $G.GET(obj, "up:2");
        if(ul)
        {
            var tabs = $G.GetElementsByTagName(ul, "li");
            for(var i=0;i<tabs.length;i++)
                tabs[i].className = "";
        }
        obj.parentNode.className = "Selected";
        var _tab = this.GetArea(obj.parentNode);
        if(_tab)
        {
			this.HideAreas();
            $G.Display.Clear(_tab);
            
        }
    },
    GetOpenedArea:function()
	{
		var list = document.getElementById("InfoAreasContainer");
		var result = null;
		if(list)
		{
			var areas = $G.GetElementsByTagName(list, "div");
			for(var i = 0;i<areas.length;i++)
			{
				if(areas[i].style.display != "none")
				{
					result = areas[i];
					break;
				}
			}
		}
		return result;
	},
	HideAreas:function()																				
	{
		var list = document.getElementById("InfoAreasContainer");
		if(list)
		{
			var areas = $G.GetElementsByTagName(list, "div");
			for(var i=0;i<areas.length;i++)
				$G.Display.Hide(areas[i]);
		}
	},
    GetUl:function(tab)
    {
		return tab.parentNode;
    },
    SetFirst:function(tab)
    {
        //TODO: Указаная закладка становится первой или максимально близко
        var ul = this.GetUl(tab);
        this.HideAll(ul);
        this.ShowTab(tab);
        var width = this.Width(ul);
        var _width = this.TabsWidth(ul);
        var next = tab;
        while(_width < width)
        {
			next = $global.Next(next);
			if(next != null)
			{
				this.ShowTab(next);
				_width = this.TabsWidth(ul);
			}
			else
				break;
        }
        if(_width >= width)
			this.HideTab(next);
		if(next == null)
		{
			var last = tab;
			while(_width < width)
			{
				last = $global.Previous(last);
				if(last != null)
				{
					this.ShowTab(last);
					_width = this.TabsWidth(ul);
				}
				else
					break;
			}
			if(_width >= width)
				this.HideTab(last);
		}
    },
    SetLast:function(tab)
    {
		//TODO: Указаная закладка становится последней или максимально близко 
		var ul = this.GetUl(tab);
        this.HideAll(ul);
        this.ShowTab(tab);
        var width = this.Width(ul);
        var _width = this.TabsWidth(ul);
        var next = tab;
        while(_width < width)
        {
			next = $global.Previous(next);
			if(next != null)
			{
				this.ShowTab(next);
				_width = this.TabsWidth(ul);
			}
			else
				break;
        }
        if(_width >= width)
			this.HideTab(next);
		if(next == null)
		{
			var last = tab;
			while(_width < width)
			{
				last = $global.Next(last);
				if(last != null)
				{
					this.ShowTab(last);
					_width = this.TabsWidth(ul);
				}
				else
					break;
			}
			if(_width >= width)
				this.HideTab(last);
		}
    },
    HideAll:function(ul)
    {
		var tabs = this.VisibleTabs(ul);
		for(var i=0;i<tabs.length;i++)
		{
			this.HideTab(tabs[i]);
		}
		return this;
    },
    HideTab:function(tab)
    {
		$global.Display.Hide(tab);
		return this;
    },
    ShowTab:function(tab)
    {
		$global.Display.Clear(tab);
		return this;
    },
    FirstVisible:function(ul)
    {
        var tabs = this.VisibleTabs(ul);
        return tabs.length > 0 ? tabs[0] : null;
    },
    LastVisible:function(ul)
    {
        var tabs = this.VisibleTabs(ul);
        return tabs.length > 1 ? tabs[tabs.length-1] : tabs.length > 0 ? tabs[0] : null;
    },
    VisibleTabs:function(ul)
    {
        var result = new Array();
        var tabs = this.Tabs(ul);
        for(var i=0;i<tabs.length;i++)
            if($global.GetStyle(tabs[i], "display") != "none")
                result.push(tabs[i]);
        return result;
    },
    Tabs:function(ul)
    {
        return $global.GetElementsByTagName(ul, "li");
    },
    TabsWidth:function(ul)
    {
        var width = 0
        if(ul)
        {
            var nodes = this.Tabs(ul);
            for(var i=0;i<nodes.length;i++)
            {   
                var a = $global.GET(nodes[i], "a:0");
                width += a.offsetWidth;   
            }
        }
        return width;
    },
    Width:function(ul)
    {
        var result = 0;
        if(ul)
            result = ul.parentNode.offsetWidth;
        return result;
    },
    btnDelete_Click:function(obj)
    {  
        if(obj)
        {
            var container = $global.Previous($global.Previous(obj.parentNode));
            if(container)
            {
                var ul = $global.GET(container, "div:0,div:0,div:0,ul:0");
                if(ul)
                {
                    var tab = this.SelectedTab(ul);
                    if(tab)
                    {
						var area = this.GetArea(tab);
						if(area)
							area.parentNode.removeChild(area);
						var ntab = $global.Previous(tab);
						if((ntab == null)||(!ntab.id))
							ntab = $global.Next(tab);
						tab.parentNode.removeChild(tab);
                        if(ntab)
                        {
                            this.Tab_Click($global.GET(ntab, "a:0"));
                            this.SetFirst(this.FirstVisible(ul))
                        }
                        else
							document.location = "index.html";
                    }
                    
                }
            }
        }
    },
    MoveLeft:function(ul)
    {
        if(ul)
        {
            var _last = this.LastVisible(ul);
            if(_last)
            {
                var first = this.FirstVisible(ul);
                var tab = $global.Previous(first);
                if(tab != null)
					this.SetFirst(tab);
            }
        }
    },
    MoveRight:function(ul)
    {
        if(ul)
        {
            var _last = this.LastVisible(ul);
            if(_last)
            {
                var first = this.FirstVisible(ul);
                var tab = $global.Next(_last);
                if(tab != null)
					this.SetLast(tab);
            }
        }
    },
    btnLeft_Click:function(obj)
    {
        if(obj)
        {
            var wraper = $global.Next(obj);
            if(wraper)
            {
                var ul = $global.GET(wraper, "ul:0");
                this.MoveLeft(ul);
            }
        }
    },
    btnRight_Click:function(obj)
    {
        if(obj)
        {
            var wraper = $global.Previous(obj);
            if(wraper)
            {
                var ul = $global.GET(wraper, "ul:0");
                this.MoveRight(ul);
            }
        }
    }
}
$param_list = 
{
	NewList:function()
	{
		var ul = document.createElement("ul");
		ul.className = "ParamList";
		return ul;
	},
	AddLine:function(list, CssName, title, value, onclick)
	{
		var li = document.createElement("li");
		if(list)
		{
			li.className = CssName ? CssName : "";
			var span = document.createElement("span");
			span.onclick = onclick ? onclick : function(){};
			span.innerHTML = value ? value : "";
			li.appendChild(span);
			li.innerHTML += title ? title : "";	  
			list.appendChild(li);
		}
		return li;
	},
	CYRange:function(value)
	{
		return value < 0 ? -1 :
			   value <= 10 ? 0 :
			   value <= 30 ? 1 :
			   value <= 60 ? 2 :
			   value <= 100 ? 3 :
			   value <= 300 ? 4 :
			   value <= 700 ? 5 :
			   value <= 1000 ? 6 :
			   value <= 5000 ? 7 :
			   value <= 10000 ? 8 :
			   value <= 50000 ? 9 : 10;
	}
}
$rows_list = 
{
	NewList:function()
	{
		var ul = document.createElement("div");
		ul.className = "ResultsPanel";
		return ul;
	},
	AddRow:function(list, CssName)
	{
		var row = document.createElement("div");
		row.className = "Row "+(CssName ? CssName : "");
		if(list)
			list.appendChild(row);
		return row;
	},
	AddCell:function(row, value, CssName)
	{
		var cell = document.createElement("div");
		cell.className = "cell "+(CssName ? CssName : "");
		cell.innerHTML = value ? value : "";
		if(row)
			row.appendChild(cell);
		return cell;
	},
	AddLine:function(list, name, cy, pr, pi, css)
	{
		var row = this.AddRow(list, css);
		this.AddCell(row, name, "dn");
		this.AddCell(row, cy, "tic");
		this.AddCell(row, pi, "tic");
		this.AddCell(row, pr, "tic");
		row.appendChild($global.GetClear());
	},
	AddBigLine:function(list, tag, name, meta, href, regect, cy, pr, pi, yaca, ol, css)
	{
		var row = this.AddRow(list, css);
		var cell = this.AddCell(row, name, "dn");
		cell.innerHTML = tag+". ";
		var a = $G.Tag("a", null, null, $global.ShrinkStr(name, regect == "Да" ? 15 : 20, "...", ""));
		a.href = "http://"+name;
		a.target = "_blank";		
		cell.appendChild(a);
		if(regect == "Да")
		{
			cell.innerHTML += "&nbsp;";
			a = $G.Tag("a", "nolink", null, "[БАН]");
			a.title = "Бан Yandex";
			a.style.color = "#ffb10a";
			cell.appendChild(a);
		}
		a = $G.Tag("a", "wink nolink", null, "&rarr;");
		a.href = "javascript:void(0)";
		a.title = "LinkWink it!";
		a.style.marginLeft = "10px";
		cell.appendChild(a);	
		a.onclick = function()
		{
			var _prev = $G.Previous(this);
			if(_prev)
				if(_prev.className == "nolink")
					_prev = $G.Previous(_prev);
			if(_prev)
				$DomainInfo.WinkIt($G.HTML.GetDomain(_prev.href));
		}	
		cell.appendChild(document.createElement("br"));
		var small = document.createElement("small");
		cell.appendChild(small);
		a = document.createElement("a");
		a.href = href;
		a.title = meta;
		a.target = "_blank";
		a.innerHTML = $global.HTML.Encode($global.ShrinkStr(meta, 30, "...", "[ПУСТО]"));
		small.appendChild(a);
		var ya = $DomainInfo.String.YaCaShort(yaca, name);
		this.AddCell(row, ya, "tic");
		this.AddCell(row, cy, "tic");
		this.AddCell(row, pr, "tic");
		this.AddCell(row, pi, "tic");
		this.AddCell(row, ol, "tic");
		row.appendChild($global.GetClear());
	},
	AddBigCaption:function(list, name)
	{
		var row = this.AddRow(list, "Caption");
		var cell = this.AddCell(row, "", "dn");
		var input = $G.Tag("input", "noinput");
		input.type = "text";
		input.value = $DomainInfo.Const.DefalutAnchorValue;
		input.onfocus = function(){ this.className = ''; this.value = $G.Next(this).value; }; 
		input.onblur = function(){ this.className = 'noinput'; this.value = $DomainInfo.Const.DefalutAnchorValue; };
		input.onkeyup = function(ev){ $DomainInfo.Interact.SearchAnchor(this, ev); };
		cell.appendChild(input);
		input = $G.Tag("input");
		input.type = "hidden";
		input.value = "";
		input.style.display = "none";
		cell.appendChild(input);
		var img = $G.Tag("img");
		img.onclick = function() { $DomainInfo.Interact.SearchAnchor($G.Previous(this), null, true); };
		img.src = "resource/search.gif";
		img.alt = "Сортировать по ссылке";
		cell.appendChild(img);
		this.AddCell(row, "<a href=\"javascript:void(0)\" title=\"Информация о ссылке\" id=\"column::"+name+"::0\" onclick=\"$DomainInfo.Interact.SortColumn(this)\" >YaCa</a>", "tic");
		this.AddCell(row, "<a href=\"javascript:void(0)\" title=\"Индекс цитирования. Яндекс\" id=\"column::"+name+"::1\" onclick=\"$DomainInfo.Interact.SortColumn(this)\">ТИЦ</a>", "tic");
		this.AddCell(row, "<a href=\"javascript:void(0)\" title=\"Google Page Rank\" id=\"column::"+name+"::2\" onclick=\"$DomainInfo.Interact.SortColumn(this)\">PR</a>", "tic");
		this.AddCell(row, "<a href=\"javascript:void(0)\" title=\"Проиндексировано страниц\" id=\"column::"+name+"::3\" onclick=\"$DomainInfo.Interact.SortColumn(this)\">ПС</a>", "tic");
		this.AddCell(row, "<a href=\"javascript:void(0)\" title=\"Исходящих ссылок\" id=\"column::"+name+"::4\" onclick=\"$DomainInfo.Interact.SortColumn(this)\">ИС</a>", "tic");
		row.appendChild($global.GetClear());
	}
}

$panels_list = 
{
	Panels:new Array(),
	GetPanel:function(obj)
	{
		var result = null;
		if(obj)
			for(var i = 0;i<this.Panels.length;i++)
				if(this.Panels[i].obj  == obj)
					result = this.Panels[i];
		return result;
	},
	CreatePanel:function(id)
	{
		var wrap = $G.Tag("div", "PopUpWrap", id);
		var close = $G.Tag("a", "CloseBtn Close", null, "закрыть");
		close.href = "javascript:void(0)";
		close.onclick = function(){ CloseBackLinksPanel(this); };
		wrap.appendChild(close);
		wrap.appendChild($global.GetClear());
		var float_wrap = $G.Tag("div", "FloatWrap");
		wrap.appendChild(float_wrap);		
		var left = $G.Tag("div", "Left");
		float_wrap.appendChild(left);
		var right = $G.Tag("div", "Right");
		float_wrap.appendChild(right);
		var h1 = $G.Tag("h1", null, null, "Информация о вторичных ссылках");
		left.appendChild(h1);
		var list = $param_list.NewList();
		var line = $param_list.AddLine(list, "", "Страниц с ТИЦ более 50к", "?", null);
		    line = $param_list.AddLine(list, "", "Страниц с ТИЦ от 10к до 50к", "?", null);
		    line = $param_list.AddLine(list, "", "Страниц с ТИЦ от 5к до 10к", "?", null);
		    line = $param_list.AddLine(list, "", "Страниц с ТИЦ от 1к до 5к", "?", null);
		    line = $param_list.AddLine(list, "", "Страниц с ТИЦ от 700 до 1к", "?", null);
		    line = $param_list.AddLine(list, "", "Страниц с ТИЦ от 300 до 700", "?", null);
		    line = $param_list.AddLine(list, "", "Страниц с ТИЦ от 100 до 300", "?", null);
		    line = $param_list.AddLine(list, "", "Страниц с ТИЦ от 60 до 100", "?", null);
		    line = $param_list.AddLine(list, "", "Страниц с ТИЦ от 30 до 60", "?", null);
		    line = $param_list.AddLine(list, "", "Страниц с ТИЦ от 10 до 30", "?", null);
		    line = $param_list.AddLine(list, "", "Страниц с ТИЦ от 0 до 10", "?", null);
		    line = $param_list.AddLine(list, "", "ТИЦ неизвестен", "?", null);
		    line = $param_list.AddLine(list, "", "Средний ТИЦ", "?", null);
		    line = $param_list.AddLine(list, "", "Суммарный ТИЦ", "?", null);
		    line = $param_list.AddLine(list, "", "Уникальных доменов", "?", null);
		    line = $param_list.AddLine(list, "bold grey", "Список обратных ссылок", "[<a href='javascript:void(0)' onclick='ShowLeftPanel(this)'>&rarr;</a>]", null);
		left.appendChild(list);
		h1 = $G.Tag("h1", null, null, "Список обратных ссылок");
		right.appendChild(h1);
		var list = this.CreateLinksPanel(id);
		right.appendChild(list);
		var di = new DomainInfo(wrap);
		this.Panels.push(di);
		return di;
	},
	CreateLinksPanel:function()
	{
		var list = $rows_list.NewList();
		var caption = $rows_list.AddRow(list, "Caption");
		$rows_list.AddCell(caption, "Ссылка", "dn");
		$rows_list.AddCell(caption, "<a title=\"Идекс цитирования. Яндекс\" href=\"javascript:void(0)\">ТИЦ</a>", "tic");
		$rows_list.AddCell(caption, "<a title=\"Проиндексированно страниц\" href=\"javascript:void(0)\">ПС</a>", "tic");
		$rows_list.AddCell(caption, "<a title=\"Google Page Rank\" href=\"javascript:void(0)\">PR</a>", "tic");
		caption.appendChild($global.GetClear());
		var links = $G.Tag("div", "Links");
		list.appendChild(links);
		return list;
	},
	ShowLeftPanel:function(panel)
	{		
		var wrap = panel;
		if(wrap)
		{
			var width = $global.GetStyle(wrap, "width", true);
			var onRun = function()
			{
				//$global.ShowInPageCenter(wrap);
			}
			$effect.Time(500).AnimateStyle(wrap, "width", 830-width, 830, null, onRun);
		}
		return $panels_list;
	},
	HideLeftPanel:function(panel)
	{		
		var wrap = panel;
		if(wrap)
		{
			var width = $global.GetStyle(wrap, "width", true);
			var onRun = function()
			{
				//$global.ShowInPageCenter(wrap);
			}
			$effect.Time(500).AnimateStyle(wrap, "width", -(830-325), 325, null, onRun);
		}
		return $panels_list;
	}
}
function DomainInfo(obj)
{
	this.obj = obj;
	this.PR = [0,0,0,0,0,0,0,0,0,0,0];
	this.PRU = 0;
	this.Links = 0;
	this.AllLinks = 0;
	this.CYSumm = 0;
	this.CYAVG = 0;
	this.UD = 0;
	this.backs = new Array();
	this.ULinks = new Array();
	
	this.AppendInfo = function(info)
	{
		//this.UD += Number( info.UD ? info.UD : 0 );
		if(info.backs)
		{
			var list = $global.GET(this.obj, "div:1,div:1,div:1,div:0");
			for(var i=0;i<info.backs.length;i++)
			{
				var link = info.backs[i];
				this.CYSumm += link.CY ? Number(link.CY) >= 0 ? Number(link.CY) : 0 : 0;
				this.Links += link.CY ? Number(link.CY) >= 0 ? 1 : 0 : 0;
				this.AllLinks += 1;
				if(link.CY)
				{											
					pr = $param_list.CYRange(Number(link.CY));
					if((pr >= 0)&&(pr <=10))
						this.PR[pr] += 1;
					else
						this.PRU += 1;
				}
				else
					this.PRU += 1;
				var domain = $global.HTML.GetDomain(link.URL);
				if(!$global.ValueInArray(this.ULinks, domain))
				{
					this.UD += 1;
					this.ULinks.push(domain);
				}	
			}
		}			
		this.CYAVG = this.Links > 0 ? Math.floor(this.CYSumm / this.Links) : 0;
		return this;
	}
	this.Update = function()
	{
		if(this.obj)
		{
			var list = $global.GET(this.obj, "div:1,div:0,ul:0");
			if(list)
			{
				for(var i = 0;i<this.PR.length;i++)
				{
					var pr = $global.GET(list, "li:"+(10-i)+",span:0");
					pr.innerHTML = this.PR[i].toString();
				}
				var cys = $global.GET(list, "li:13,span:0");
				var cyavg = $global.GET(list, "li:12,span:0");
				var pru = $global.GET(list, "li:11,span:0");
				var ud = $global.GET(list, "li:14,span:0");
				if(cys)
					cys.innerHTML = this.CYSumm;
				if(cyavg)
					cyavg.innerHTML = this.CYAVG;
				if(pru)
					pru.innerHTML = this.PRU;
				if(ud)
					ud.innerHTML = this.UD;
			}
		}
		return this;
	}
    this.AddLinks = function(info)
    {
		if(info)
		{
			if(this.obj)
			{
				var list = $global.GET(this.obj, "div:1,div:1,div:1,div:1");
				if(list)
					for(var i=0;i<info.backs.length;i++)
					{
						var link = info.backs[i];
						var css = $global.Number.is2N((this.AllLinks-info.backs.length)+i+1) ? "Alt" : "";
						var num = (this.AllLinks-info.backs.length)+i+1;
						var num = num >= 10 ? num : "0"+num;
						$rows_list.AddLine(list, num+".&nbsp;<a href='"+link.URL+"' title='"+link.URL+"' target='_blank' >"+$global.HTML.GetDomain(link.URL)+"</a>", link.CY, link.PR, link.PI, css);
					}
			}
		}
    }
}
function BackLink()
{
	this.URL = "";
	this.PR = 0;
	this.PI = 0;
	this.CY = 0;
}
