function o(name, prefix) {
	var id=name;
	if (prefix!=null && prefix.length > 0 ) id=prefix+name;
	var res=document.getElementById(id) || document.all[id] || null;
	return res;
};

function openImage(url, width, height) {
    var sc=(width>screen.width-50 || height>screen.height-50 ? 'yes':'no')
    var ow=(width>screen.width-50 ? screen.width:width*1+50);
    var oh=(height>screen.height-50 ? screen.height:height*1+50);
	window.open(url, '', 'width='+ow+'px, height='+oh+'px, scrollbars='+sc);
	return false;
};

function _patchManualImagesClasses() {
    var images=document.getElementsByTagName('img');
    var iC=0;
    for(var i=0; i<images.length; i++ ) {
        var id=images[i].id;
        if (images[i].getAttribute('mediaid') != null || images[i].getAttribute('uid') != null ) {
            var _m=images[i];
            var align=_m.getAttribute('align');
            var iClass=_m.getAttribute('className');
            if (iClass == null || iClass.length==0 ) {
                iClass=_m.getAttribute('class');
            };

            if (iClass==null || iClass.length==0 ) {
                _m.setAttribute('class', (align == 'right' ? 'main_':'')+'img_'+align);
                _m.setAttribute('className', (align == 'right' ? 'main_':'')+'img_'+align);
            };

            _m.onblur=new Function('return false; ');
            _m.onmousedown=new Function('return false; ');
            _m.onresizestart=new Function('return false; ');
            var t=_m.parentNode;
            if (t.tagName == 'P' ) {
            	if (t.id == null || !t.id.length ) {
            		t.setAttribute('id', 'galleryP_'+iC );
            		t.setAttribute('cnt', 1);
            		iC++;
            	} else {
            		t.setAttribute('cnt', t.getAttribute('cnt')*1+1 );
            	};
            };
        };
    };

    for(var i=0; i<iC; i++ ) {
    	var t=o('galleryP_'+i);
    	if (t.getAttribute('cnt') > 1 ) {
    		var tmp=t.innerHTML+'';
    		var re=new Array(new RegExp(">(?:\s|\&nbsp\;|\r*\n)+<", "gim"), '><', new RegExp("^(?:\s|\&nbsp\;|\r*\n)+<", "gim"), '<', new RegExp(">(?:\s|\&nbsp\;|\r*\n)+$", "gim"), '>' );
    		for(var j=0; j<re.length/2; j++ ) {
	    		tmp=tmp.replace(re[j*2], re[j*2+1]);
    		};
    		cleanTree(t);
    		t.innerHTML=tmp;
    		t.setAttribute('class', 'gallery');
    		t.setAttribute('className', 'gallery');
    	};

    	var flag=1;
    	if (flag == 1) {
	    	for(var j=0; j<t.childNodes.length; j++ ) {
    			var img=t.childNodes[j];
    			if(img.tagName == 'IMG' ) {
    				var link=document.createElement('A');
    				link.href=img.getAttribute('fullPath') || img.src;
	    			link.rel='prettyPhoto[vImages]';
    				link.title=img.alt;
    				var originalSource=img.src;
    				var newImg=document.createElement('IMG');
    				newImg.src=originalSource;
	    			newImg.setAttribute('class', img.getAttribute('class') );
    				newImg.setAttribute('className', img.getAttribute('className') );
    				link.appendChild(newImg );
    				t.replaceChild(link, t.childNodes[j]);
	    		};
    		};
    	};
    };
};

var dCnt=0;
function debug(msg) {
//	return false;
	dCnt++;
	o('debug').innerHTML=dCnt+':'+msg+'<br/>'+o('debug').innerHTML;
};

function getActiveChilds(obj, tagName) {
	var result=new Array();
	for(var i=0; i<obj.childNodes.length; i++ ) {
		if (obj.childNodes[i].tagName==tagName ) result.push( obj.childNodes[i] );
	};
	return result;
};

function cleanTree(obj) {
	while(obj.firstChild ) {
		obj.removeChild(obj.firstChild);
	};
};

function cloneTR(src, dst) {
	cleanTree( dst );
	for(var i=0; i<src.childNodes.length; i++ ) {
		if (src.childNodes[i].tagName == 'TD' ) {
			var newTD=document.createElement('TD');
			newTD.innerHTML=src.childNodes[i].innerHTML;
			newTD.colSpan=src.childNodes[i].colSpan;
			newTD.style.display=src.childNodes[i].style.display;
			newTD.id=src.childNodes[i].id;
			newTD.setAttribute('requiredDisplay', src.childNodes[i].getAttribute('requiredDisplay') );
			dst.appendChild(newTD);
		};
	};
};

var loadedFiles=new Array();
function loadFile(src) {
	for(var i=0; i<loadedFiles.length; i++ ) {
		if ( loadedFile[i] == src ) return true ;
	};
	var re=/\.(\w+)$/i;
	var type=src.match(re)[1];
	var ref;
	if (type == 'js' ) {
		ref=document.createElement('SCRIPT');
		ref.setAttribute('type', 'text/javascript');
		ref.src=src;
	} else {
		if (type == 'css' ) {
			ref=document.createElement('LINK');
			ref.setAttribute('rel', 'stylesheet');
			ref.setAttribute('type', 'text/css');
			ref.src=src;
		} else {
			return false;
		};
	};

	if (typeof ref != 'undefined' ) {
		document.getElementsByTagName('HEAD')[0].appendChild(ref);
		return true;
	};

	return false;
};

function loCase(str)
{
    var ru_alpha='ÀÁÂÃÄÅ¨ÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞß';
    lo_ru_alpha='àáâãäå¸æçèéêëìíîïðñòóôõö÷øùúûüýþÿ';
    var res='';
    for (i=0; i<str.length; i++) {
        var c=str.substring(i,i+1);
        var n=ru_alpha.indexOf(c);
        if ( n != -1 ) {
            res = res + lo_ru_alpha[n];
        } else {
            res=res+c;
        };
    };
    return res;
};


function overNav(obj) {
	var img=o('leftLinkedImage');
	var nSRC=obj.getAttribute('linkedImagePath');
	if (nSRC.length > 0 && img.src!=nSRC) {
		obj.setAttribute('oldImage', img.src);
		img.src=nSRC;
	}
};

function outNav(obj) {
	var img=o('leftLinkedImage');
	var nSRC=obj.getAttribute('oldImage');
	if (nSRC.length > 0 && img.src!=nSRC) {
		img.src=nSRC;
	}
};

function checkForm() {
    var inputs=document.getElementsByTagName('INPUT');
    var i=0;
    while( i < inputs.length ) {
         if (inputs[i].getAttribute('required') == '1' && ( ( inputs[i].value == null || inputs[i].value.length == 0) || inputs[i].value==inputs[i].getAttribute('defaultValue') ) ) {
         	  if ( 
         	  	!(inputs[i].getAttribute('id') == 'form_email' && o('form_phone').value.length > 0 && o('form_phone').value !=o('form_phone').getAttribute('defaultValue') ) 
         	  	&&
         	  	!(inputs[i].getAttribute('id') == 'form_phone' && o('form_email').value.length > 0 && o('form_email').value !=o('form_email').getAttribute('defaultValue') ) 
         	  ) {
	              alert('Çàïîëíèòå ïîëå '+(inputs[i].getAttribute('fieldName') || inputs[i].getAttribute('defaultValue') ) );
	              inputs[i].focus();
    	          return false;
    	      };
         };
         i++;
    };
    o('JSKEY').value=1;
    o('cForm').submit();
    return true;
};

var GIndex=0;
var GItems=new Array();
var GItemWidth=160;

function GMove(direction) {
	if (GItems.length == 0 ) _GInit();
	_GMove(direction*-1, 0, GItemWidth);
};

function _GMove(dir, x, diff) {
	var offs = Math.floor( Math.sqrt(x) );
	if (diff - offs < 0) offs=diff;
	diff = diff - offs;
	o('GBlock').style.left=(_GGetValue( o('GBlock').style.left )*1+offs*dir)*1+'px';
	if (diff > 0 ) window.setTimeout("_GMove("+dir+", "+(x+1)+", "+diff+")", 10);

	if (diff == 0 ) {
		if (dir < 0 ) {
			o('GBlockContainer').appendChild( o('GBlockContainer').removeChild( o('GBlockContainer').childNodes[0] ) );
		} else {
			o('GBlockContainer').insertBefore( o('GBlockContainer').removeChild( o('GBlockContainer').childNodes[ o('GBlockContainer').childNodes.length-1 ] ), o('GBlockContainer').childNodes[0] );
		};
		o('GBlock').style.left=(_GGetValue( o('GBlock').style.left )*1-GItemWidth*dir)*1+'px';
	};
};

function _GInit() {
	var gbl=o('GBlockContainer');
	for(var i=0; i<gbl.childNodes.length; i++ ) {
		if (gbl.childNodes[i].tagName == 'TD' ) {
			GItems.push(gbl.childNodes[i].childNodes[0] );
		};
	};
	gbl.style.width=(GItems.length * GItemWidth)+'px';
	gbl.style.left='-'+Math.floor(_GGetValue(gbl.style.width)/2)+'px';
	gbl.style.display='block';
};

function _GGetValue(attr) {
	var re=/^(\-?\d+)px$/;
	var res=re.exec(attr);
	return res[1]*1;
};


animation_running=0;
animation_loop=0;
animation_cancelStop=false;
animation_wait=null;

function animation_call(obj) {
	if (animation_wait != null ) {
		window.clearTimeout(animation_wait);
		animation_cancelStop=true;
	};	
    var direction=0;
	if (obj.offsetLeft <= 320 ) {
    	direction=+1;
    } else {
    	if (obj.offsetLeft >=640 ) {
    		direction=-1;
    	};
    };
    if (direction != 0 ) {
    	animation_running=1;
    	animation_wait=window.setTimeout('animation_run('+direction+', true)', 500);
    };
};

function animation_stop() {
	if (!animation_cancelStop) animation_running =0;
//	o('debug').innerHTML='STOP';
};


function animation_run(dir, startFlag) {
	if (animation_running == 0) return true ;
	if (animation_loop==0 || startFlag) { //startFlag && animation_loop == 0 || !startFlag || startFlag==null 
		animation_loop=1;
		if (animation_wait != null ) {
			window.clearTimeout(animation_wait);
		};
		if (startFlag) animation_running=dir;
		var offs=1;
		o('GBlock').style.left=(_GGetValue( o('GBlock').style.left )*1+Math.floor(offs*dir ) )*1+'px';
		if (_GGetValue( o('GBlock').style.left ) % 160 ==0 ) {
			if (dir < 0 ) {
				o('GBlockContainer').appendChild( o('GBlockContainer').removeChild( o('GBlockContainer').childNodes[0] ) );
			} else {
				o('GBlockContainer').insertBefore( o('GBlockContainer').removeChild( o('GBlockContainer').childNodes[ o('GBlockContainer').childNodes.length-1 ] ), o('GBlockContainer').childNodes[0] );
			};
			o('GBlock').style.left=(_GGetValue( o('GBlock').style.left )*1-GItemWidth*dir)*1+'px';
		};                                                                                   
		//o('debug').innerHTML=dir+', '+startFlag+'<br/>'+_GGetValue( o('GBlock').style.left)+'<br/>'+_GGetValue( o('GBlock').style.left)%160;
		if (animation_running == dir || startFlag) {
			animation_running = dir;
			if (animation_wait != null ) {
				window.clearTimeout(animation_wait);
			};
			animation_wait=window.setTimeout('animation_run('+dir+')', 25);
		};
		animation_loop=0;
	} else {
		window.setTimeout('animation_run('+dir+', '+(startFlag || false)+' )', 5);
	};
};
