﻿var ToolTip = {
	fadeInTimer:null,
	fadeOutTimer:null,
	fadeOutTimerG:null,
	//Получить ширину окна
	GetClienWidth:function() {
		if(document.documentElement.scrollWidth)
			return document.documentElement.scrollWidth;
		else if(window.innerWidth)
			return ((Sys.Browser.agent === Sys.Browser.Safari) ? window.innerWidth : Math.min(window.innerWidth, document.documentElement.clientWidth));
		else
			return document.documentElement.clientWidth;
	},
	//Получить высоту окна
	GetClientHeight:function() {
		if(document.documentElement.scrollHeight)
			return document.documentElement.scrollHeight;
		else if(window.innerHeight)
			return ((Sys.Browser.agent === Sys.Browser.Safari) ? window.innerHeight : Math.min(window.innerHeight, document.documentElement.clientHeight));
		else
			return document.documentElement.clientHeight;
	},
	//Открыть модальное окно
	ShowModalWindow:function(sender,elementId) {
		var bg=document.getElementById("bgModal");
		var element=document.getElementById(elementId);
		if(bg==null)
		{
			bg=document.createElement("div");
			bg.id="bgModal";
			bg.style.dispaly="none";
			bg.style.position="fixed";
			bg.style.top="0px";
			bg.style.zIndex=10;
			element.style.zIndex=11;
			bg.className="mupGray";
			bg.style.width=this.GetClienWidth()+"px";
			bg.style.height=this.GetClientHeight()+"px";
			document.documentElement.appendChild(bg);
		}
		bg.style.display = element.style.display="";
	},
	//Закрыть модальное окно
	CloseModalWindow:function(sender,elementId) {
		document.getElementById(elementId).style.display = document.getElementById("bgModal").style.display = "none";
	},
	//Отобразить или спрятать элемент
	ToggleWindow:function(sender, element, bSetColor) {
		element=Utils.GetObjectFromId(element);
		sender=Utils.GetObjectFromId(sender);

		if(element.style.display=="") {
			element.style.display="none";
			if(sender!=null) {
				if(sender.tagName=="IMG") sender.src="http://s.techhome.ru/Theme/Blue/ui/plus.gif";
				else if(bSetColor) sender.style.backgroundColor="";
				else sender.style.fontWeight="";
			}
		}
		else {
			element.style.display="";
			if(sender!=null) {
				if(sender.tagName=="IMG") sender.src="http://s.techhome.ru/Theme/Blue/ui/minus.gif";
				else if(bSetColor) sender.style.backgroundColor="#FF0000";
				else sender.style.fontWeight="bold";
			}
		}
		return false;
	},
	//Развернуть или свернуть блок
	ToggleBlock:function(sender,element,baseClass)
	{
		element=Utils.GetObjectFromId(element);
		sender=Utils.GetObjectFromId(sender);

		sender.className=baseClass+(element.style.display==""?" collapsed":" expanded");
		element.style.display=element.style.display==""?"none":"";
	},
	// Отобразить или спрятать элемент и прижать его к вызываемому элементу
	TogglePopup:function(sender, element, left, top) {
		element=Utils.GetObjectFromId(element);
		if(element!=null) {
			if(element.style.display=="")
				element.style.display="none";
			else {
				var pos = Sys.UI.DomElement.getBounds(sender);
				Sys.UI.DomElement.setLocation(element,pos.x,pos.y-135);
				element.style.display="";
			}
		}
	},
//Region: FadeBlock
	//Отобразить элемент
	FadeIn:function(e,el,timeout) {
		this.ClearInterval();
		this.SetOpacity(el,0);
		el=Utils.GetObjectFromId(el);
		el.style.display="block";
		var t=0;
		for(i=0;i<=100;i++){
			ToolTip.fadeInTimer=setTimeout("ToolTip.SetOpacity('"+el.id+"',"+i+")",t*5);
			t++;
		}
		if(timeout)
			setTimeout("ToolTip.FadeOut(null,'"+el.id+"')",timeout);
	},
	//Спрятать элемент
	FadeOut:function(e,el) {
		el=Utils.GetObjectFromId(el);
		var t=0;
		for(i=100;i>=1;i--) {
			ToolTip.fadeOutTimer=setTimeout("ToolTip.SetOpacity('"+el.id+"',"+i+")", t * 3);
			t++;
		}
		ToolTip.fadeOutTimerG=setTimeout("document.getElementById('"+el.id+"').style.display='none'", 500);
	},
	ClearInterval:function() {
		clearTimeout(ToolTip.fadeOutTimerG);
		clearTimeout(ToolTip.fadeOutTimer);
		clearTimeout(ToolTip.fadeInTimer);
		this.fadeInTimer=this.fadeOutTimer=this.fadeOutTimerG=null;
	},
	SetOpacity:function(el,lev) {
		var s=Utils.GetObjectFromId(el).style;
		s.opacity=lev/100;
		s.filter='alpha(opacity='+lev+')';
	},
//EndRegion: FadeBlock
	GetValidCell:function(s){
		return s.childNodes[0].tagName=="TD"?s.childNodes[0]:s.childNodes[1];
	},
	ToggleTable:function(s) {
		var row=(s.tagName=="TD"?s.parentNode:s).nextSibling;
		s=s.tagName=="TD"?s:this.GetValidCell(s);
		var collapse=s.className=="group expanded";
		s.className=collapse?"group collapsed":"group expanded";
		do{
			if(row.tagName=="TR") {
				if(this.GetValidCell(row).className=="")
					row.style.display=collapse?"none":"";
				else break;
			}
			row=row.nextSibling;
		}while(true);
	},
	// В статистике раскрыть таблицу с городами
	ToggleTableEx: function(isOpen,tableId,baseClass) {
		var rows=document.getElementById(tableId).getElementsByTagName("TR");
		for(var loop=0;loop<rows.length;loop++)
			if(rows[loop].onclick==null && this.GetValidCell(rows[loop]).tagName=="TD")
				rows[loop].style.display=isOpen?"":"none";
			else if(rows[loop].onclick!=null)
				this.GetValidCell(rows[loop]).className=baseClass+(isOpen?" expanded":" collapsed");
	},
	MovePopup:function(e,sender) {
		this.SelectedItem=sender;
		this.X=sender.style.pixelLeft?sender.style.pixelLeft:sender.offsetLeft;
		this.Y=sender.style.pixelTop?sender.style.pixelTop:sender.offsetTop;
		if(!e) e=window.event;
		this.MouseX=e.pageX?e.pageX:e.clientX;
		this.MouseY=e.pageY?e.pageY:e.clientY;
		document.onmousemove = this.MoveDrag.bind(this);
		document.onmouseup = this.MoveDrop.bind(this);
	},
	MoveDrag:function(e) {
		if (!e) e = window.event;
		var newx=e.pageX?e.pageX:e.clientX, newy=e.pageY?e.pageY:e.clientY;
		/*var left=this.X + (newx - this.MouseX);
		var top=this.Y + (newy - this.MouseY);
		this.SelectedItem.style.left = String(left<0?0:left) + "px";
		this.SelectedItem.style.top = String(top<0?0:top) + "px";*/
		this.SelectedItem.style.left = String(this.X + (newx - this.MouseX))+"px";
		this.SelectedItem.style.top = String(this.Y + (newy - this.MouseY))+"px";
		return false;
	},
	MoveDrop:function(e) {
		document.onmousemove=null;
		document.onmouseup=null;
	}
};
var ImageTip={
	m_Tip:null,
	m_Image:null,
	m_FollowMouse:false,
	NS5:(!document.all && document.getElementById || window.opera)?true:false,
	IE5:(navigator.userAgent.indexOf("MSIE")>-1 && document.getElementById)?true:false,
	IE4:(document.all && !document.getElementById)?true:false,
	internal:{
		m_OffsetX:20,
		m_OffsetY:12,
		m_bOnTip:false,
		m_Timer1:null,
		m_Timer2:null,
		m_MouseX:0,
		m_MouseY:0,
		hPageLoad:null,
		hImageLoad:null
	},
	OnLoad:function(){
		this.m_Tip=document.getElementById("productToolTip");
		if(this.m_Tip/* && this.m_FollowMouse*/)
		{
			this.internal.hPageLoad=this.TrackMousePos.bind(this);
			Utils.AttachEvent("mousemove",document,this.internal.hPageLoad);
		}
	},
	ShowToolTip:function(e,imgUrl,imageId)
	{
		if(!this.m_Tip) this.OnLoad();
		if(this.m_Tip && imageId) {
			if(this.internal.m_Timer1) clearTimeout(this.internal.m_Timer1);
			if(this.internal.m_Timer2) clearTimeout(this.internal.m_Timer2);
			this.internal.m_bOnTip=true;

			if(this.m_Image==null)
			{
				this.m_Image=document.createElement("img");
				this.m_Tip.appendChild(this.m_Image);
				
				this.internal.hImageLoad=this.SetTipPos.bind(this);
				Utils.AttachEvent("load",this.m_Image,this.internal.hImageLoad);
			}
			this.m_Image.src=imgUrl+imageId;
			//this.m_Tip.innerHTML='<img src="'+imgUrl+imageId+'"/>';
			
			if(this.m_FollowMouse) this.internal.m_Timer1=setTimeout(function(){ImageTip.m_Tip.style.display="";},100);
			//else this.SetTipPos(e);
		}
	},
	TrackMousePos:function(e){
		standard=document.compatMode=="CSS1Compat"? document.documentElement : document.body;//create reference to common "body" across doctypes
		this.internal.m_MouseX = this.NS5? e.pageX: window.event.clientX + standard.scrollLeft;
		this.internal.m_MouseY = this.NS5? e.pageY: window.event.clientY + standard.scrollTop;
		if(this.m_FollowMouse && this.internal.m_bOnTip) this.SetTipPos(e);
	},
	SetTipPos:function(e){
		if(!this.internal.m_bOnTip) return;
		/*if(!this.m_FollowMouse){
			standard=document.compatMode=='CSS1Compat'? document.documentElement : document.body;
			this.internal.m_MouseX = this.NS5? e.pageX: window.event.clientX + standard.scrollLeft;
			this.internal.m_MouseY = this.NS5? e.pageY: window.event.clientY + standard.scrollTop;
		}*/
		// tooltip width and height
		var tpWd=this.m_Image.width;
		var tpHt=this.m_Image.height;
		/*var tpWd = this.IE4 || this.IE5? this.m_Tip.clientWidth: this.m_Tip.offsetWidth;
		var tpHt = this.IE4 || this.IE5? this.m_Tip.clientHeight: this.m_Tip.offsetHeight;*/
		// document area in view (subtract scrollbar width for ns)
		var winWd = this.NS5? window.innerWidth-20+window.pageXOffset: standard.clientWidth+standard.scrollLeft;
		var winHt = this.NS5? window.innerHeight-20+window.pageYOffset: standard.clientHeight+standard.scrollTop;
		// check mouse position against tip and window dimensions
		// and position the tooltip
		if((this.internal.m_MouseX + this.internal.m_OffsetX + tpWd) > winWd)
			this.m_Tip.style.left = this.internal.m_MouseX - (tpWd + this.internal.m_OffsetX) + "px";
		else this.m_Tip.style.left = this.internal.m_MouseX + this.internal.m_OffsetX + "px";
		if((this.internal.m_MouseY + this.internal.m_OffsetY + tpHt) > winHt)
			this.m_Tip.style.top = winHt-(tpHt + this.internal.m_OffsetY) + "px";
		else this.m_Tip.style.top = this.internal.m_MouseY+this.internal.m_OffsetY+"px";
		if(!this.m_FollowMouse) ImageTip.m_Tip.style.display="";//this.internal.m_Timer1=setTimeout("ImageTip.m_Tip.style.display=''",100);
	},
	HideToolTip:function(){
		if(this.m_Image)
		{
			this.internal.m_Timer2=setTimeout(function() {ImageTip.m_Tip.style.display="none";},100);
			this.internal.m_bOnTip=false;
		}
	}
};
ImageTip.internal.hPageLoad=ImageTip.OnLoad.bind(ImageTip);
Utils.AttachEvent("load",window,ImageTip.internal.hPageLoad);
document.write('<div id="productToolTip" style="position:absolute;display:none;z-index:100"></div>');
