//resize popUp окна для схемы проезда 

function psPopImg2(obj){
	var w = window.open(obj.href, "", "resizable=1,HEIGHT=100,WIDTH=100,scrollbar=1");
	with(w.document){
		open();
		var c = [
			'<html><head>',
				'<title>',title,'</title>',
				'<script language="javascript">',
					'function fitPic(){',
						'iWidth = document.body.clientWidth;iHeight = document.body.clientHeight;iWidth = document.images[0].width - iWidth;',
						'iHeight = document.images[0].height - iHeight;window.resizeBy(iWidth, iHeight-1);',
						'var sw=(document.body.clientWidth-iWidth)/2;var sh=(document.body.clientHeight-iHeight)/2;',
						'if (sh<0) sh=0; if (sw<0) sw=0;window.moveTo(sw,sh);self.focus();}',
				'</script></head>',
				'<body onload="fitPic();" style="margin:0;padding:0;">',
					'<img src="',obj.href,'" border="0" alt="',title,'" title="',title,'" />',
				'</body>',
			'</html>'].join('');
		write(c);
		close();
	}
  return false;	
}

function psPrint(obj){
  popupWin = window.open(obj.href, '', 'width=700,height=600,toolbar=no,menubar=yes,location=no,status=yes,resizable=yes,scrollbars=yes,left=50,top=50');
  popupWin.focus();
  return false;	
}

// - Корзина --------------------------------------------

function psAddToCart(obj, id){
	var c = $('psProdCount'+id), sdata = {'ajax':1}, r;
	if (c){
		sdata.count = c.value;
	}
	
	obj.innerHTML = '<img src="'+psIMG+'_tpl_loader_small.gif" />';	
	
	var request = new Request.JSON({
		url: obj.href,
		onComplete: function(jsonObj) {
			if(this.response.json)	{
				var d = this.responseJS, dCost, dCount;
				
				obj.innerHTML = (d.cmd_errors) ? d.cmd_errors.join(' ') : d.cmd_messages.join(' ');
				
				dCost = gebi('psCartCost');
				if (dCost && d.cart_cost){
					dCost.innerHTML = d.cart_cost;
				}
				dCount = $('psCartCount');
				if (dCount && d.cart_count){
					dCount.innerHTML = d.cart_count;
				}
				if (c){
					c.value = 1;
				}else{
					obj.href = d.cart_url? d.cart_url : '/cart.html';
					obj.onclick = function(){};					
				}
			}
		}
	}).post(sdata);	

}

// - Windows --------------------------------------------

var psWindows = {
	current:false,
	bg:false,
	loader:false,
	panel:false,
	image:null,
	
	popImg:function(url){
		this.loadingOn();
		var i = new Image();
		this.image = i;
		i.obj = this;
		i.canceled = false;
		i.onload = function(){
			if (this.canceled) return;
			
			this.obj.current = psCreateDiv('psWindow');
			this.obj.current.appendChild(this);
			this.obj.current.className = 'psWindow';
			this.obj.position();
			this.obj.loadingOff();
			if(i.offsetHeight > this.obj.bg.offsetHeight){
				this.obj.bg.style.height = i.offsetHeight + 100+ 'px';
			}
		}
		i.onclick = function(){
			this.obj.close();		
		}		
		i.style.cursor = 'pointer';
		i.src = url.href;
		return false;		
	},
	
	popImgPreview:function(href, title, reloadPanel){
		this.loadingOn();
		var i = new Image();
		this.image = i;
		i.obj = this;
		if(title)
			i.title = title;
		else
			i.title = href.title;
		i.canceled = false;
		this.oldUrl = href;

		i.onload = function(){
			if (this.canceled) return;
			
			if((this.obj.oldUrl == this.src) && (reloadPanel == null)){
				this.obj.createPanel(this);
				this.obj.p = 1;
			}
			else if(reloadPanel != null)	{
				this.obj.currentImg = this;
				this.obj.p = 1;
			}
			else	{
				this.obj.p = 0;
			}

			this.obj.oldUrl = null;
			this.obj.current = psCreateDiv('psWindow');
			this.obj.current.style.zIndex = 1100;
				
			this.obj.current.appendChild(this.obj.closeButtonCreate());		
			this.obj.current.appendChild(this);
			this.obj.current.className = 'psWindow';

			this.obj.current.appendChild(this.obj.titleImg(this.title));
			
			this.obj.current.style.display = 'none';

			if(this.obj.p == 1)	{
				this.obj.current.p = 1;
			}
			
			this.obj.position();
			this.obj.loadingOff();
			if(i.offsetHeight > this.obj.bg.offsetHeight){
				this.obj.bg.style.height = i.offsetHeight + 100+ 'px';
			}
		}
		i.onclick = function(){
			this.obj.close();		
		}		
		i.style.cursor = 'pointer';
		i.src = href;
		return false;		
	},

	
	popDiv:function(id){
		this.loadingOn();
		objSrc = $(id);
		if (!objSrc) return false;
		this.current = psCreateDiv('psWindow');	
		this.current.className = 'psWindow';
		this.current.innerHTML = objSrc.innerHTML;
		this.current.onclick = stopEvent;
		this.position();
		this.loadingOff();
		return false;
	},
	
	popUrl:function(url){
		if (!url) return false;
		this.loadingOn();

		this.request = new Request.JSON({
			url: url,
			onComplete: function(jsonObj) {
				if(this.response.json)	{
					this.obj.current =	psCreateDiv('psWindow');	
					this.obj.current.className = 'psWindow';
					this.obj.current.innerHTML = this.responseJS.PageContent;
					this.obj.current.onclick = stopEvent;
					this.obj.position();
					this.obj.loadingOff();
				}
			}
		}).post({ajax:1});
	},

	close:function(){
		if (this.current){
			psDeleteObj(this.current);
			this.current = false;
		}
		if (this.bg){
			psDeleteObj(this.bg);
			this.bg = false;
		}
		if (this.panel){
			psDeleteObj(this.panel);
			this.panel = false;
		}
		if (this.loader){
			psDeleteObj(this.loader);
			this.loader = false;
		}
	},

	position:function(obj){
		var t, l;
		obj = (obj)?obj:this.current;
		obj.style.display = 'block';
		psPage.getSize();

		t = (psPage.windowHeight - obj.offsetHeight)/2 + psGetPageScroll();
		if(obj.p == 1)	{
			l = ((psPage.windowWidth - 140 - obj.offsetWidth)/2);
		}
		else	{
			l = ((psPage.windowWidth - obj.offsetWidth)/2);
		}
		obj.style.top = (t <= 0) ? '0px' : t + 'px';
		obj.style.left = (l <= 0) ? '0px' : l + 'px';
		if(t < psGetPageScroll())	{
			window.scrollTo(0, t);
		}
		else	{
			window.scrollTo(0, psGetPageScroll());
		}
	},

	loadingOn:function(){
		this.closeBackGround();
		this.loader = psCreateDiv('psLoader');
		this.loader.className = 'psLoader';
		this.loader.innerHTML = 'Загрузка';
		this.position(this.loader);
	},
	
	loadingOff:function(){
		if (this.loader){
			psDeleteObj(this.loader);
			this.loader = false;
		}		
	},
	
	closeBackGround:function(){
		if (this.bg){
			return false;
		}
		psPage.getSize();
		
		this.bg = psCreateDiv('closeFonId');
		this.bg.obj = this;
		this.bg.className='psCloseBackGround';
		this.bg.style.height=psPage.pageHeight+100+'px';
		this.bg.style.width=document.body.scrollWidth+'px';
		this.bg.onclick = function(){
			this.obj.close();		
		}
	},
	createPanel:function(img){
		this.currentImg = img;
		this.panel = psCreateDiv('psPanel');
		this.panel.style.height = parseInt(this.bg.style.height) - 40 +'px';
		this.wrapper = psCreateDiv('psIcoWrapper');
		
		/* таблица с иконками */
		this.divCenter = psCreateDiv('psCenter');			
		this.table = document.createElement('table');
		this.table.id = 'icoTable';
		this.tbody = document.createElement('tbody');
		this.table.appendChild(this.tbody);
		this.divCenter.appendChild(this.table);

		this.wrapper.appendChild(this.divCenter);
		this.panel.appendChild(this.wrapper);
				
		this.list = listImg;
		for(var i=0;i<this.list.length;i++){
			this.tr = document.createElement('tr');
			this.td = document.createElement('td');
			this.td.id = 'ImgShow'+i;
			this.td.onmouseover = function(){
				this.className = (this.className == 'icoTdActive') ? this.className : 'icoTdhover';
			}
			this.td.onmouseout = function(){
				this.className = (this.className == 'icoTdActive') ? this.className : 'icoTd';
			}
			this.td.className = 'icoTd';
			this.div = psCreateDiv('DivImgShow'+i);
			this.div.className = 'psIco';
			this.div.listImg = this.list[i];
			this.div.style.background = 'url('+this.div.listImg.ico + this.div.listImg.file+') no-repeat center center';

			if((this.div.listImg.img + this.div.listImg.file) == img.src)	{
				this.td.className = 'icoTdActive';
			}

			this.div.obj = this;
			this.div.td = this.td;
			this.div.onclick = function(){
				this.obj.td = this.td;
				this.obj.div = this;
				this.obj.reloadImg();
			}
			this.td.appendChild(this.div);
			this.tr.appendChild(this.td);
			this.tbody.appendChild(this.tr);
			this.div = null;
			this.td = null;
			this.tr = null;
		}
		
		psPage.getSize();
		this.wrapper.style.height = psPage.windowHeight + 'px';
		this.divCenter.style.margin = (this.wrapper.offsetHeight - this.divCenter.offsetHeight)/2 + 'px 0px 0px 0px';		
		
		if(LibBWCheck().ie6)	{
			this.wrapper.style.top = document.documentElement.scrollTop + (psPage.windowHeight - this.wrapper.offsetHeight)/2 + 'px';
			this.panel.style.position = 'absolute';
			this.panel.style.zIndex = 2000;
			this.panel.style.top = 0;
			this.panel.style.width = '140px';
			this.panel.style.padding = '0px 5px 0px 5px';
			this.panel.style.height = this.bg.style.height;
		}
	},
	closeButtonCreate:function(){
		this.title = '<img src="'+psIMG+'0.gif" width="15" height="15" alt="Закрыть" />';
		this.button = psCreateDiv('psClose');	
		this.button.className = 'psClose';
		this.button.obj = this;
		this.button.onclick = function(){
			this.obj.close();
		}
		this.button.innerHTML = this.title;
		return this.button;
	},
	titleImg:function(title){
		this.title = title;
		this.text = psCreateDiv('psTitle');	
		this.text.className = 'psTitle';
		this.text.obj = this;
		this.text.innerHTML = this.title;
		return this.text;
	},
	reloadImg:function(){
		for(var i=1;i<this.list.length;i++){
			if(this.currentImg.src == (this.list[i].img + this.list[i].file)){
				this.num = i;
				break;
			}
		}
		
		if(this.div)	{
			if(this.currentImg.src != (this.div.listImg.img + this.div.listImg.file))	{
				$('ImgShow'+this.num).className = 'icoTd';
				
				var tUrl = {};
				tUrl.href = this.div.listImg.img + this.div.listImg.file;
				tUrl.title = this.div.listImg.name;
				this.current.removeChild(this.currentImg);
				psDeleteObj(this.current);
				this.popImgPreview(tUrl.href, tUrl.title, 1);
				
				this.td.className = 'icoTdActive';
				this.div = null;
			}
		}
	}
}


// - Windows --------------------------------------------


// - Base Functions --------------------------------------------

function addEvent(el, evname, func) {
	if (el.attachEvent) { // IE
		el.attachEvent("on" + evname, func);
	} else if (el.addEventListener) { // Gecko / W3C
		el.addEventListener(evname, func, true);
	} else {
		el["on" + evname] = func;
	}
};
//--------------------------------------------------------------------------
function removeEvent(el, evname, func) {
	if (el.detachEvent) { // IE
		el.detachEvent("on" + evname, func);
	} else if (el.removeEventListener) { // Gecko / W3C
		el.removeEventListener(evname, func, true);
	} else {
		el["on" + evname] = null;
	}
};

function stopEvent(ev) {
	ev || (ev = window.event);
	if (Calendar.is_ie) {
		ev.cancelBubble = true;
		ev.returnValue = false;
	} else {
		ev.preventDefault();
		ev.stopPropagation();
	}
	return false;
};

function psCreateDiv(idName){
	var obj = document.createElement('div');
	document.getElementsByTagName("body")[0].appendChild(obj);
	obj.id=idName;
	return $(idName);
}

function psDeleteObj(obj){
	if (typeof(obj) == 'string'){
		obj = $(obj);
	}
	if (obj){
		document.body.removeChild(obj);
	}
}

function psPreloadImg(url){
    var image = new Image();
    image.src = url;    
}

var psPage = {
	pageWidth:0,
	pageHeight:0,
	windowWidth:0,
	windowHeight:0,
	
	getSize:function(){
		var xScroll, yScroll;
		
		if (window.innerHeight && window.scrollMaxY) {	
			xScroll = document.body.scrollWidth;
			yScroll = window.innerHeight + window.scrollMaxY;
		} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
		} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
			xScroll = document.body.offsetWidth;
			yScroll = document.body.offsetHeight;
		}
		
		if (self.innerHeight) {	// all except Explorer
			this.windowWidth = self.innerWidth;
			this.windowHeight = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
			this.windowWidth = document.documentElement.clientWidth;
			this.windowHeight = document.documentElement.clientHeight;
		} else if (document.body) { // other Explorers
			this.windowWidth = document.body.clientWidth;
			this.windowHeight = document.body.clientHeight;
		}	
		
		// for small pages with total height less then height of the viewport
		if(yScroll < this.windowHeight){
			this.pageHeight = this.windowHeight;
		} else { 
			this.pageHeight = yScroll;
		}
	
		// for small pages with total width less then width of the viewport
		if(xScroll < this.windowWidth){	
			this.pageWidth = this.windowWidth;
		} else {
			this.pageWidth = xScroll;
		}
	}
//	
//	return arrayPageSize;
}

// временная функция
function psGetPageSize(){
	return [psPage.pageWidth,psPage.pageHeight,psPage.windowWidth,psPage.windowHeight];
}

function psGetPageScroll(){
	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}

	return yScroll;
}
//--------------------------------------------------------------------------
// Browser Check
//--------------------------------------------------------------------------
function LibBWCheck(){ //Browsercheck (needed)
	this.ver=navigator.appVersion
	this.agent=navigator.userAgent
	this.dom=document.getElementById?1:0
	this.opera5=this.agent.indexOf("Opera 5")>-1
	this.ie5=(this.ver.indexOf("MSIE 5")>-1 && this.dom && !this.opera5)?1:0; 
	this.ie6=(this.ver.indexOf("MSIE 6")>-1 && this.dom && !this.opera5)?1:0;
	this.ie4=(document.all && !this.dom && !this.opera5)?1:0;
	this.ie=this.ie4||this.ie5||this.ie6
	this.mac=this.agent.indexOf("Mac")>-1
	this.ns6=(this.dom && parseInt(this.ver) >= 5) ?1:0; 
	this.ns4=(document.layers && !this.dom)?1:0;
	this.bw=(this.ie6 || this.ie5 || this.ie4 || this.ns4 || this.ns6 || this.opera5)
	this.ieTrueBody = function(){
		return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
	}
	
	return this
}
//--------------------------------------------------------------------------
// Mouse
//--------------------------------------------------------------------------
function _psMouse(){
	this.move = function(e){
		psMouse.x=(psBW.ns4 || psBW.ns6)?e.pageX:event.clientX + psBW.ieTrueBody().scrollLeft;
		psMouse.y=(psBW.ns4 || psBW.ns6)?e.pageY:event.clientY + psBW.ieTrueBody().scrollTop;
	}
}

//--- Flash --------------------------------------------------
function Flash( ) {

    this.transparent = true;
    this.needFlashVerion = 8;          
    this.flashvars = "xml_source=/ixml/&menuselid=0";
    this.BrowserInfo( );

}


Flash.prototype.BrowserInfo = function( ) { 
 
    var userAgent = navigator.userAgent.toLowerCase( );
       this.browserIE = ( userAgent.indexOf( 'msie' ) != -1 );

}

Flash.prototype.CheckVersionFlash = function( needVersion ) {
 
    if ( navigator.plugins[ 'Shockwave Flash' ] ) {
        matchStr = new RegExp ( '^[A-Za-z ]*(.*) .*$' );
        this.flashVerion = parseInt( navigator.plugins[ 'Shockwave Flash' ].description.replace( matchStr, '$1' ) );
        return ( needVersion <= this.flashVerion );
    }
    else if ( this.browserIE ) {
        for( var i = needVersion; i < needVersion + 10; i++ ) {
            try {
                    flashPlayer = new ActiveXObject( 'ShockwaveFlash.ShockwaveFlash.' + i );
                    this.flashVerion = i;
                    return true;
            }
            catch( e ) {
                continue;
            }
        }
        return false;     
    }
    else {
        this.flashVerion = 0;
        return false;
    }
 
}

Flash.prototype.Insert = function( ) {
	if ( this.CheckVersionFlash( this.needFlashVerion ) ) { 
		document.write( this.GenerateHtmlFlash( ) );
	}else{
		document.write( this.GenerateHtmlImage( ) );
	}
}

Flash.prototype.GenerateHtmlFlash = function( ) {
 
    var flashCode = '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" ';
    flashCode += 'codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version= ' + this.flashVerion + ',0,0,0"' + ' width="' + this.width + '" height="' + this.height + '" align="middle">';
    flashCode += '<param name="allowScriptAccess" value="always"/>';
    flashCode += '<param name="movie" value="' + this.srcFlash + '"/>';
    flashCode += '<param name="quality" value="high"/>';
    if ( this.base ) {
        flashCode += '<param name="base" value="' + this.base + '"/>';
    }
    if ( this.background ) {
        flashCode += '<param name="bgcolor" value="' + this.background + '"/>';
    }
    if ( this.transparent ) {
        flashCode += '<param name="wmode" value="transparent"/>';
    }
    if ( this.flashvars ) {
        flashCode += '<param name="flashvars" value="' + this.flashvars + '"/>';
    }
 
    flashCode += '<embed quality="high" allowScriptAccess="always" type="application/x-shockwave-flash"' + ' pluginspage="http://www.macromedia.com/go/getflashplayer" ';
    flashCode += 'src="' + this.srcFlash+'" ' + '" width="' + this.width + '" height="' + this.height + '" ';
    if ( this.base ){
        flashCode += ' base="' + this.base + '"';
    }
    if ( this.background ){
        flashCode += ' bgcolor="' + this.background + '"';
    }
    if ( this.transparent ){
        flashCode += ' wmode="transparent"'
    }
    if ( this.flashvars ){
        flashCode += ' flashvars="' + this.flashvars + '"';
    }
    flashCode += '></embed>';
 
    flashCode += '</object>';
    return flashCode;
 
}

Flash.prototype.GenerateHtmlImage = function( ) {
 
    var imageCode = '<img src="' + this.srcImage + '"/>';
    return imageCode;
 
}
//--- Flash --------------------------------------------------



//-------------------------------------------------------------------------
//	Результаты голосования
//-------------------------------------------------------------------------
function setVotingResult(div, href, block, form)
{
	var sdata = {};

	vForm = $(form);
	theGroup = vForm.send_vote;
	for (i=0; i<= theGroup.length; i++)	{
		if(theGroup[i].checked)	{
			sdata['value'] = theGroup[i].value;
			break;
		}		
	}
	sdata['cmd'] = vForm.cmd.value;
	sdata['id'] = vForm.voteid.value;
	sdata['ajax'] = 1;
	
	this.request = new Request.JSON({
		url: href,
		onComplete: function(jsonObj) {
			if(this.response.json)	{				
				$('vMsg').innerHTML = this.response.json.js.msg;
				if(this.response.json.js.vote)	{
					href = href + '&cmd=popup&id='+sdata['id'];
					getVotingResult(div, href, block);
				}
			}
		}
	}).get(sdata);
}


function getVotingResult(div, href, block)
{
	this.request = new Request.JSON({
		url: href,
		onComplete: function(jsonObj) {
			if(this.response.json)	{
				$('vName').innerHTML = 	'<div id="voteIndex" style="font-size: 1.4em; line-height: 1.2; font-weight: bold;	padding: 11px 0px 5px 0px;"><p>'+this.response.json.js.vName+'</p></div>';
				$('vVotes').innerHTML = 'Всего голосов:&nbsp;' + this.response.json.js.vVotes;
				var msg = '<table>';
				for(i=1;i<this.response.json.js.vItems;i++){
					doc_vote = 0;
					doc_vote_percent = 0;
				msg = msg + '<tr><td align="left" nowrap >'+this.response.json.js['value'+i]+'</td><td width="100%" style="padding-left:20px; padding-bottom:5px;"><img src="'+psIMG+'0.gif" width="'+this.response.json.js['width'+i]+'%" height="10" style="background:url('+psIMG+'voting_result.gif) 0px 0px repeat-x; border:#CA7A94 1px solid;" alt="" />&nbsp;'+this.response.json.js['percent'+i]+'%&nbsp;<span class="vote">('+this.response.json.js['vote'+i]+')</span></td></tr>';
				}				
				msg = msg + '</table>';
				$('vItems').innerHTML = msg;
				
				$(div).style.display  = "block";//временно
				
				if(block)	{
					$(block).style.display  = "none";//временно
				}
			}
		}
	}).get();
}