//search
function checkFrmSearch(frm){
  if(typeof(frm)=='undefined')
    frm = document.forms[0];
    
	var val = frm.sval.value.trim();
	if(val.length==0){ 
		alert('Введите строку поиска!'); 
		frm.sval.focus();
		return false; 
	}	
	else {
		frm.sval.value = val;
		return true;
	}
}

// rollovers
function sid(o,v){o.id=v;}

// 
function viewBox(id, dir){
	window.frames[id].showBox(dir);
}

// screenshots
function screenshot(id, w, h){
	if(Browser.Type == Browser.IE && Browser.MajorVersion > 5){

		if(document.body.clientWidth < w+132 || document.body.clientHeight < h+90){
			screenshotNewsWin(id,w,h);
		}
		else {
			var tt = ToolTip.GetInstance('ScreenShotFrame');
			if('' + tt  == 'undefined'){
				tt = ToolTip.CreateInstance('ScreenShotFrame', 'ttFrame2', 'ttShadow2', false);

				tt.Template = 
				'<table width=100% border=0 cellpadding=0 cellspacing=0>' +
				'<tr><td height=17 valign=bottom style="background-color:#557EBA;border-width:1px 1px 0px 1px;border-style:solid;border-color:#9EB6D8 #000000 #000000 #9EB6D8;"><img align=right onclick="ToolTip.GetInstance(\'ScreenShotFrame\').Hide();" src=/images/close.gif width=13 height=12 alt=" Закрыть " hspace=6 style="cursor:pointer;"></td></tr>' +
					'<tr><td>' +
						'<div style="border-width:0px 1px 1px 1px;border-style:solid;border-color:#9EB6D8 #000000 #000000 #9EB6D8;background-color:#557EBA;padding:4px;">' +
							'<div style="border-width:1px;border-style:solid;border-color:#000000 #9EB6D8 #9EB6D8 #000000;">' +
								'<div id=ScreenFrame style="height:'+(h+27)+'px;"></div>' +
							'</div>' +
						'</div>' +
					'</td></tr>' +
				'</table>';	
				
				EventFramework.Instance.AttachEvent(window, 'onscroll', null, function(){ ToolTip.GetInstance('ScreenShotFrame').Hide(); });
			}
			tt.Show(w+12);
			var div = document.getElementById('ScreenFrame');
			var iframe = document.createElement('IFRAME');
			iframe.frameBorder = 0;
			iframe.scrolling = 'no'; 
			with(iframe.style){
				width = '100%';
				height = '100%';
			}
      div.appendChild(iframe);
      iframe.src = '/games/screenshot.asp?id='+id;	
      event.cancelBubble=true;
	  }
	}
	else {
		screenshotNewsWin(id,w,h);
	}

}

function screenshotNewsWin(id,width,height){
	height += 33;
	var left=(screen.width/2)-width/2, top = (screen.height/2) - height/2-20;
	var nw = window.open('/games/screenshot.asp?id='+id,'screenshot','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,width='+width+',height='+height+',left='+left+',top='+top+',screenX='+left+',screenY='+top);
	nw.focus();
}

// --------------------------------

var img = new Image;
img.onload = setSrc;

function pic(i, shift) {
	var obj = document.getElementById('pr'+i);
	var i = obj.src.match(/\/(\d)\.jpg/i)[1];
	i = parseInt(i, 10) + shift;
	if(i < 0) i = 9; else if(i > 9) i = 0;
	img.source = obj;
	img.src = '/images/temp/' + i + '.jpg';
}

function setSrc(){
	img.source.src = img.src;
}

// preload tabs
var tabImg=['blue_s.gif', 'blue_e.gif', 'blue_bg.gif', 'yellow_s.gif', 'yellow_bg.gif', 'yellow_e.gif'];
var tabL=tabImg.length, i=0;
for(i=0;i<tabL;i++) newImage('/images/tab/'+tabImg[i]);


function createPopUpMenu(){

	Menu.createElement('news',113, 1,
			 'Новости','/news/default.asp?sec=1',
			 'Статьи','/news/default.asp?sec=2',
			 'Поиск','/news/search.asp',			 
			 'Подписка','/news/subscribe.asp');
			 
	Menu.createElement('catalog',120, 1,
			 'Игры для PC','/games/section.asp?platform=PC',
			 'Игры для PS2','/games/section.asp?platform=PS2',
			 'Игры для PS3','/games/section.asp?platform=PS3',
			 'Игры для PS Move','/games/ps3-move/default.aspx',
			 'Игры для PSP','/games/section.asp?platform=PSP',
			 'Игры для Xbox 360','/games/section.asp?platform=X360',
			 'Игры для Wii','/games/section.asp?platform=Wii',
			 //'Аксессуары','/games/hardlist.aspx',
			 'Поиск','/games/default.asp',
			 'График выхода','/games/releases.aspx');
			 
	Menu.createElement('user',115, 1,
				'Регистрация игр', '/users/default.asp',
				'Личный кабинет', '/personal/default.aspx',
				'Поддержка','/users/support.asp',
				'FAQ','/users/support-faq.aspx',				 
				'Где купить','/users/shops.asp', 
			  'Вакансии', '/users/job.asp');
				 
	Menu.createElement('partner',113, 1,
//			 'О компании', '/partners/default.asp',
//			 'Контакты', '/partners/contacts.asp',
			  'Прессе', '/partners/press.asp',
			  'Стать дилером','/partners/newpartner.asp');
}

function MenuFramework(name){

	this.VISIBLE_DELAY = 300
	this.HIDDEN_DELAY  = 300;
	this._MenuFilePath = '/images/topnav/';
	
	this.InstanceName = name;
	
	this._visTimeout   = null;
	this._hideTimeout  = null;
	this._isVisible    = false;
	this._active       = null; 
	this._activeButton = null; 

	this.view = function(o){ 

		var id = this.getButtonID(o);
		this.kill(this._hideTimeout);

		if(this._active != id) {

			if(this._activeButton) this.setVisButton(this._activeButton, false);
			this.setVisButton(id, true);
		
			if(this._isVisible) {
				if(this._active) this.hideDropDownMenu(this._active);
				this.setVisible(id, 'visible');
			}
			else {
				this.setVisTimeout(id)
			}
			
		}
	}
	
	this.setVisTimeout = function(id){
		this._visTimeout = setTimeout(this.InstanceName + '.VisTimeout(\'' + id + '\');', this.VISIBLE_DELAY); 
	}

	this.VisTimeout = function(id){
		this._isVisible = true;
		this.setVisible(id, 'visible');
	}

	this.hide = function(obj){ 
		var id = this.getButtonID(obj);
		if(this._visTimeout) this.kill(this._visTimeout);
		this.setHideTimeout(id);
	}

	this.setHideTimeout = function(id){
		this._hideTimeout = setTimeout(this.InstanceName + '.HideTimeout(\'' + id + '\');', this.HIDDEN_DELAY); 
	}

	this.HideTimeout = function(id){
		this._isVisible = false;
		this.hideDropDownMenu(id, 'hidden');
	}

	this.hideDropDownMenu = function(id){
		this.setVisButton(id, false);
		this.setVisible(id, 'hidden');
	}
	
	this.NoHide  = function(){
		this.kill(this._hideTimeout);
	}

	this.getButtonID = function(obj){
		return obj.name;
	}

	this.getButton = function(id){
		return document.images[ id ];
	}

	this.setVisButton = function(id, vis){
		if(vis) this._activeButton = id; else this._activeButton = null;
		this.getButton(id).src = this._MenuFilePath + id + (vis?'_over':'')+'.gif';
	}

	this.kill = function(interval){ 
		clearInterval(interval);
		if(interval == this._visTimeout) this._visTimeout = null; 
	}

	this.setVisible = function(id, vis){
		var obj = this.getDropDownMenu(id);
		if(vis == 'visible'){
			var pos = this.getCoordinate(this.getButton(id), obj.getAttribute('hShift'));
			with(obj.style){
				left = pos.left;
				top  = pos.top;
				visibility = 'visible'; 
			}
			this._active = id;
		}
		else {
			obj.style.visibility = 'hidden'; 
			this._active = null;
		}
	}

	this.getCoordinate = function(obj, hShift){
		var res = new Object();
		res.left = res.top = 0;
		o = obj;
		while(o != document.body) {
			res.left += o.offsetLeft;
			res.top  += o.offsetTop;
			o = o.offsetParent;
		} 
		res.left += parseInt(hShift, 10);
		res.top += obj.height;
		return res;
	}
	
	this.getDropDownMenu = function(id){ 
		return document.getElementById(this.getDropDownMenuName(id)); 
	}
	
	this.getDropDownMenuName = function(id){
		return 'menu_' + id;
	}
	
	this.go = function(url){
		window.location.href = url;
	}
	
	this.createElement = function(id, width, hShift){
		var arg = arguments;
		var l = arg.length;
		document.write('<div id="' + this.getDropDownMenuName(id) + '" class=menu hShift=' + hShift + ' style="width:' + width + 'px;" onmouseover="' + this.InstanceName + '.NoHide();" onmouseout="' + this.InstanceName + '.setHideTimeout(\'' + id + '\');">'); 
		document.write('<table width=100% border=0 cellpadding=0 cellspacing=0>')
		for(var i=3; i<l; i+=2)	this.createItem(arg[i], arg[i+1]); 
		document.write('</table>')
		document.write('</div>');
	}
	
	this.createItem = function(str, url){
		document.write('<tr><td id=mNorm onmouseover="sid(this,\'mNormOver\');" onmouseout="sid(this,\'mNorm\');" onclick="' + this.InstanceName + '.go(\'' + url + '\');"><a href="'+url+'">'+str+'</a></td></tr>');
	}
	
}

var Menu = new MenuFramework('Menu');