
if( window.location.pathname.split( '/' )[1] != 'wad' ){
	EventFramework.Instance.AttachEvent( window, "onload", null, InitTree );
}

var Shop = new Object();

addImgLoad( 'printer.gif', 'close.gif', 'fenya_s.gif' );

function Sh( id, Name, ShowName, Partner, Address, City, Phone, Metro, Comments, Width, Platform, IsAccent ){
  this.id = id;
  this.Name = Name;
  this.ShowName = ShowName;
  this.Partner = Partner;
  this.Address= Address;
  this.City = City;
  this.Phone = Phone;
  this.Metro = Metro;
  this.Comments = Comments;
  this.Width = Width;
  this.Platform = Platform;
  this.IsAccent = IsAccent == 0 ? false : true;  
}

function InitTree(){
	var imgs = document.getElementsByName( 'eb' );
	var l = imgs.length;
	
	for( var i=0; i < l; i++ ){
		EventFramework.Instance.AttachEvent( imgs[i], "onclick", null, ExpendIt );
		EventFramework.Instance.AttachEvent( imgs[i].parentNode.parentNode.getElementsByTagName('DIV')[0], "onclick", null, ExpendIt );		
	}
	
	InitToolTip();
}

function InitToolTip( parent ){
	var tooltip = ToolTip.CreateInstance( 'ShopInfo', 'ttFrame', 'ttShadow', true, parent );
	tooltip.PointShift.X = 30;
	tooltip.PointShift.Y = -5;
	tooltip.Template = 
    '<table width=100% border=0 cellpadding=0 cellspacing=0>' +
	    '<tr>' +
		    '<td class=tooltipHead>' +
			    '<table width=100% border=0 cellpadding=0 cellspacing=0>' +
				    '<tr>' +
					    '<td width=13><img src=/images/fenya_s.gif hspace=4 width=8 height=8></td>' +
					    '<td width=100%>{1}</td>' +
					    '<td width=16><img class=bt ' + ( !IsValid(parent) ? 'onclick=PrintAdress("{0}")' : '' ) + ' src=/images/printer.gif width=16 height=14 alt=" Распечатать "></td>' +
					    '<td width=8><div style="width:8px;"></div></td>' +
					    '<td width=13><img class=bt onclick="ToolTip.GetInstance(\'ShopInfo\').Hide();" src=/images/close.gif width=13 height=12 alt=" Закрыть "></td>' +					
				    '</tr>' +				
			    '</table>' +
		    '</td>' +
	    '</tr>' +
	    '<tr>' + 
	      '<td>' +
     		    '{2}' +
	      '</td>' +
	    '</tr>' +
    '</table>';

	InitBrowserArea();
	tooltip.AreaInspection = function( squareBox ) {  return window[ BrowserAreaName ].TypeInArea( squareBox ); }
}

function showTT( obj, ev ){
  var pt = ev.GetPoint();
  var sh = Shop[ obj.getAttribute('alias') ];
  ToolTip.GetInstance( 'ShopInfo' ).ShowAt( sh.Width, ev.GetPoint(), sh.id, GetName( sh ), GetContent( sh, false ) );
  ev.cancelBubble=true;
}

function GetName( sh ){
  return ( sh.Partner != '' ) ? sh.Partner : ( sh.Name != '' ) ? sh.Name : '&nbsp;';
}

function GetContent( sh, forPrint ){
  var str = '<table class=shopinfo width="' + sh.Width + '" cellpadding=4 cellspacing=0 border=0>';

  if( forPrint )
    str += '<tr class=dk><td><b>Магазин</b>:</td><td>' + ( IsValid(sh.Partner) ? sh.Partner + ' - ' : '' ) + sh.Name + '</td></tr>';

  if( IsValid( sh.Metro ) )
    str += '<tr class=dk><td><b>Метро</b>:</td><td>' + sh.Metro + '</td></tr>';

  var plat = 'Игры для ';
  var platLen = sh.Platform.length;
  
  for(var i=0; i<platLen; i++)
  {
    plat += Platforms[sh.Platform[i]] + ', ';
  }
  plat = plat.substr(0, plat.length-2) + '.';
  
  str += '<tr><td><b>Адрес</b>:</td><td>' + ( sh.City != '' ? sh.City + ', ' : '' ) + sh.Address + '</td></tr>' +
         '<tr class=dk><td><b>В&nbsp;продаже</b>:</td><td width=100%>' + plat + '</td></tr>';
  
  if( IsValid( sh.Phone ) )
    str += '<tr><td><b>Телефон</b>:</td><td>' + sh.Phone + '</td></tr>';
    
  if( IsValid( sh.Comments ) )
    str += '<tr><td colspan=2>' + sh.Comments + '</td></tr>';
    
  str += '</table>';
  
  return str;
}

function ShowShops( id, obj ){
  var ids = obj.getAttribute( 'shop' );
  if( IsValid( ids ) ){
    ids = ids.split( ',' );
    var l = ids.length;
    
    var table = document.createElement( 'TABLE' );
    var tbody = document.createElement( 'TBODY' );
    table.appendChild( tbody );
    
    for( var i=0; i < l; i++ ){
      tbody.appendChild( CreateRow( Shop[ids[i]], (i == l-1) )  );
    }
    
    table.cellPadding = 0;
    table.cellSpacing = 0;
    table.border = 0;
    table.id = '_' + id;
    
    obj.parentNode.parentNode.getElementsByTagName( 'TD' )[1].appendChild( table );
    
    var span = table.getElementsByTagName( 'SPAN' );
    
    l = span.length;
    var o;
    for( var i=0; i < l; i++ ){
      o = span[i];
    	EventFramework.Instance.AttachEvent( o, 'onclick', null, showTT );
    	EventFramework.Instance.AttachEvent( o, 'onmouseover', null, function( obj, ev ){ with( obj.style ) color = borderColor = '#074AD5'; } );
	    EventFramework.Instance.AttachEvent( o, 'onmouseout' , null, function( obj, ev ){ with( obj.style ) color = borderColor = '#20356A'; } );
    } 
  }
}

function CreateRow( sh, isLast ){
  var tr = document.createElement( 'TR' );
  var td = document.createElement( 'TD' );
  
  td.vAlign = 'top';
  td.width = 9;
  td.className = 'bg';
  if( isLast ) td.style.backgroundRepeat = 'no-repeat';
  
  td.innerHTML = '<img src="/images/_tree/blank.gif" class="i" >';
  
  tr.appendChild( td );
  
  td = document.createElement( 'TD' );
  
  var str = '';
  
  if( sh.Partner != '' ) str = '«' + sh.Partner + '»; ';
  str += sh.Address;
  
  if( sh.ShowName == 1 && sh.Name != '' )
    str += '; ' + sh.Name;
  
  var re;

	// удаляем форматирование
  if( Browser.Type == Browser.IE && Browser.MajorVersion < 6 ){
		var index = str.indexOf( '<' ) ;
		if( index != -1 ){
			var s = '';
			var last = -1;
			do {
				s += str.substring( last + 1, index );
				last = str.indexOf( '>', index );
				index = last;
			} 
			while( ( ( index = str.indexOf( '<', index ) ) != -1 ) )
			s += str.substring( last + 1, str.length );
			str = s;
		}
	}
  else {
		str = str.replace( new RegExp( '<.*?>', 'ig' ), '' );
	}

  str = str.replace( re, '' );
  
  var div = document.createElement( 'DIV' );
  div.innerHTML = 
    '<span ' +
    ( sh.IsAccent ? 'style="font-weight:bold;" ' : '' )  +
    'class="tooltip" alias=' + 
    sh.id + 
    '>' + 
    str + 
    '</span>';

  td.appendChild( div );
  tr.appendChild( td );
  
  return tr;
}

function ExpendIt( obj, ev ){
	if( obj.tagName == 'DIV' )
		obj = obj.parentNode.parentNode.getElementsByTagName('IMG')[0];


	var id = obj.getAttribute('n');
	var tbl = document.getElementById( '_' + id );
	
	if( tbl == null ){
	  ShowShops( id, obj );
    obj.src = '/images/_tree/minus.gif';
	}
	else{
	  if( obj.src.indexOf('plus.gif') != -1  ){
		  obj.src = '/images/_tree/minus.gif';
		  tbl.className = '';
	  }
	  else{
		  obj.src = '/images/_tree/plus.gif';
		  tbl.className = 'h';		
	  }
	}
}

function PrintAdress( id ){
  var sh = Shop[ id ];
  var nw = newWindow( '', 400, 150, 'shop', 'no', 'no', 'yes' );
  nw.document.open();
  nw.document.write( '<head><title>' + sh.Name + '</title><link rel=stylesheet href=/css/main.css type=TEXT/CSS></head>' );
  nw.document.write( '<style> body {background-color:#FFFFFF;} .shopinfo TD{ vertical-align:top; }</style>' );
  nw.document.write( '<body>' );
  nw.document.write( GetContent( sh, true ) );
  nw.document.write( '</body>' );
  nw.document.close();
  nw.print();
}

