brName = navigator.appName; brVer = navigator.appVersion; brAgent = navigator.userAgent;

isDOM = document.getElementById ? true:false;
isOpera = (window.opera && isDOM) ? true:false;
isOpera6 = (isOpera && window.print)? true:false;
isOpera7= (isOpera && document.readyState)? true:false;
isOpera8= /Opera[^\d]*8/.test(brAgent);
isOpera9= /Opera[^\d]*9/.test(brAgent);
isIE = (document.all && document.all.item && !isOpera)? true:false
isSafari=/\WSafari\W/.test(brAgent);
isNN= (brName=="Netscape")? true:false;
isMozilla= (isNN && isDOM)? true:false;

var menu=function(){
	var t=5,z=50,s=6,a;
	function dd(n){this.n=n; this.h=[]; this.c=[]}
	dd.prototype.init=function(p,c){
		a=c; var w=document.getElementById(p), s=w.getElementsByTagName('ul'), l=s.length, i=0;
		for(i;i<l;i++){
			var h=s[i].parentNode; this.h[i]=h; this.c[i]=s[i];
			h.onmouseover=new Function(this.n+'.st('+i+',true)');
			h.onmouseout=new Function(this.n+'.st('+i+')');
		}
	}
	dd.prototype.st=function(x,f){
		var c=this.c[x], h=this.h[x], p=h.getElementsByTagName('a')[0];
		clearInterval(c.t); c.style.overflow='hidden';
		if(f){
			p.className+=' '+a;
			if(!c.mh){c.style.display='block'; c.style.height=''; c.mh=c.offsetHeight; c.style.height=0}
			if(c.mh==c.offsetHeight){c.style.overflow='visible'}
			else{c.style.zIndex=z; z++; c.t=setInterval(function(){sl(c,1)},t)}
		}else{p.className=p.className.replace(a,''); c.t=setInterval(function(){sl(c,-1)},t)}
	}
	function sl(c,f){
		var h=c.offsetHeight;
		if((h<=0&&f!=1)||(h>=c.mh&&f==1)){
			if(f==1){c.style.filter=''; c.style.opacity=1; c.style.overflow='visible'}
			clearInterval(c.t); return
		}
		var d=(f==1)?Math.ceil((c.mh-h)/s):Math.ceil(h/s), o=h/c.mh;
		c.style.opacity=o; c.style.filter='alpha(opacity='+(o*100)+')';
		c.style.height=h+(d*f)+'px'
	}
	return{dd:dd}
}();

function slideshow() {
	
	//if no IMGs have the show class, grab the first image
	var current = ($('#slideshow a.show')?  $('#slideshow a.show') : $('#slideshow a:first'));

	//Get next image, if it reached the end of the slideshow, rotate it back to the first image
	var next = ((current.next().length) ? ((current.next().hasClass('caption'))? $('#slideshow a:first') :current.next()) : $('#slideshow a:first'));	
	
	//Get next image caption
	var caption = next.attr('rel');	
	//alert(caption);
	
	//Set the fade in effect for the next image, show class has higher z-index
	next.css({opacity: 0.0})
	.addClass('show')
	.animate({opacity: 1.0}, 1000);

	//Hide the current image
	current.animate({opacity: 0.0}, 1000)
	.removeClass('show');
	
	//Set the opacity to 0 and height to 1px
	$('#slideshow .caption').animate({opacity: 0.0}, { queue:false, duration:0 }).animate({height: '1px'}, { queue:true, duration:300 });	
	
	//Animate the caption, opacity to 0.7 and heigth to 100px, a slide up effect
	$('#slideshow .caption').animate({opacity: 0.9},100 ).animate({height: '50px'},1000 );
	
	//Display the content
	$('#slideshow .content').html(caption);
	
	
}

// Opacity change
var opacityTo = new Array();
var filterStep = 5;
var filterTimeOut = 0.01;
var activeTo = new Array();
var resizeTO = new Array();
var moveTO = new Array();
var opened_phone = false;
var opened_request = false;

function setElementOpacity(sElemId, nOpacity){
	var opacityProp = getOpacityProperty(), elem = document.getElementById(sElemId);
	if (!elem || !opacityProp) return;
	if (opacityProp=="filter"){
		nOpacity *= 100;
		var oAlpha = elem.filters['DXImageTransform.Microsoft.alpha'] || elem.filters.alpha;
		if (oAlpha) oAlpha.opacity = nOpacity;
		}
	else elem.style[opacityProp] = nOpacity;
}

function getElementOpacity(sElemId){
	var opacityProp = getOpacityProperty(), elem = document.getElementById(sElemId);
	if (!elem || !opacityProp) return;
	if (opacityProp=="filter"){
		var oAlpha = elem.filters['DXImageTransform.Microsoft.alpha'] || elem.filters.alpha;
		if (oAlpha) _opacity = oAlpha.opacity/100;
		}
  	else _opacity = elem.style[opacityProp];
	return _opacity;
}

function getOpacityProperty() {
	if (typeof document.body.style.opacity == 'string') return 'opacity';
	else if (typeof document.body.style.MozOpacity == 'string') return 'MozOpacity';
	else if (typeof document.body.style.KhtmlOpacity == 'string') return 'KhtmlOpacity';
	else if (document.body.filters && navigator.appVersion.match(/MSIE ([\d.]+);/)[1]>=5.5) return 'filter';
	return false; //нет прозрачности
}

function filterColor(layer,dir,limit){
	_opacity = getElementOpacity(layer);
	_opacity = parseFloat(0+_opacity);
	document.getElementById(layer).style.display = (_opacity == 0 && dir<0)? 'none' : 'block';
	if((isIE || isMozilla || isSafari || isOpera) && ((_opacity < limit && _opacity < 1 && dir>0) || (_opacity > 0 && _opacity > limit && dir<0 && !P))) {
		clearTimeout(opacityTo[layer]);
		_opacity += (dir*filterStep/100);
		setElementOpacity(layer,_opacity);
		opacityTo[layer]=setTimeout("filterColor('"+ layer + "','"+dir+"','"+limit+"')",  filterTimeOut);
	}
	else clearTimeout(opacityTo[layer]);
}

function moveLayer(layer,_x,_y,_xTO,_yTO){
	clearTimeout(moveTO[layer]);
	this_x = document.getElementById(layer).offsetLeft;
	this_y = document.getElementById(layer).offsetTop;
	
	if((Math.abs(this_x - _x)>1 && _xTO>0) || (Math.abs(this_y - _y)>1 && _yTO>0)){
		if(_xTO>0) document.getElementById(layer).style.left = this_x + Math.ceil((_x - this_x)/_xTO) + (_x - this_x)/Math.abs(this_x - _x) + 'px';
		if(_yTO>0) document.getElementById(layer).style.top = this_y + Math.ceil((_y - this_y)/_yTO) + (_y - this_y)/Math.abs(this_y - _y) + 'px';
		moveTO[layer]=setTimeout("moveLayer('"+ layer + "', " +_x+", " +_y+", " +_xTO+", " +_yTO+")",  20);
	}
	else {
		if(_xTO>0) document.getElementById(layer).style.left = _x + 'px';
		if(_yTO>0) document.getElementById(layer).style.top = _y + 'px';
	}
}

function resizeLayer(layer,_x,_y,_xTO,_yTO){
	clearTimeout(resizeTO[layer]);
	this_x = document.getElementById(layer).offsetWidth;
	this_y = document.getElementById(layer).offsetHeight;
	
	if((Math.abs(this_x - _x)>1 && _xTO>0) || (Math.abs(this_y - _y)>1 && _yTO>0)){
		if(_xTO>0) document.getElementById(layer).style.width = this_x + Math.ceil((_x - this_x)/_xTO) + (_x - this_x)/Math.abs(this_x - _x) + 'px';
		if(_yTO>0) document.getElementById(layer).style.height = this_y + Math.ceil((_y - this_y)/_yTO) + (_y - this_y)/Math.abs(this_y - _y) + 'px';
		resizeTO[layer]=setTimeout("resizeLayer('"+ layer + "', " +_x+", " +_y+", " +_xTO+", " +_yTO+")",  50);
	}
	else {
		if(_xTO>0) document.getElementById(layer).style.width = _x + 'px';
		if(_yTO>0) document.getElementById(layer).style.height = _y + 'px';
	}
}

function checkFeedbackForm(form) {
	f1 = (document.forms[form].elements['author'] != undefined)? document.forms[form].elements['author'].value : "none";
	f2 = (document.forms[form].elements['email'] != undefined)? document.forms[form].elements['email'].value : "none";
	f3 = (document.forms[form].elements['phone'] != undefined)? document.forms[form].elements['phone'].value : "none";
	f4 = (document.forms[form].elements['text'] != undefined)? document.forms[form].elements['text'].value : "none";
	
	if (f1 == "") {
		alert("Введите, пожалуйста, Ваше имя");
		return false;
	}
 	else if (((f2 == "") || (f2.indexOf('@')<0) || (f2.indexOf('.')<0)) && (f3=="")) {
		alert("Введите, пожалуйста, корректный e-Mail или номер телефона");
		return false;
	}
	else if (f4 == "") {
		alert("Введите, пожалуйста, Ваш запрос");
		return false;
	}
	else return true;
}

function message(form) {
	formElement = document.forms[form].elements;
	isFilled = true;
	for(j=0; j<formElement.length; j++) if(formElement[j].value == '') isFilled = false;
	if (!isFilled)  {
		alert('Все поля формы должны быть заполнены!'); return false;
	}
	else return true;
}

function showLayer(layer){
	document.getElementById(layer).style.display = (document.getElementById(layer).style.display != 'block' || document.getElementById(layer).style.display == 'none')? 'block' : 'none';
}

function strip_tags( str ){
	return str.replace(/<\/?[^>]+>/gi, '');
}

function addFavorites(fav){
			switch(fav){
				case 'fb': window.open('http://www.facebook.com/share.php?u='+location.href); break;
				case 'vk': window.open('http://vkontakte.ru/share.php?url='+location.href); break;
				case 'lj': 
					this_layer = (document.getElementById('block_main'))? 'block_main' : 'block_main_content';
					var this_text = 'http://www.livejournal.com/update.bml?subject='+encodeURIComponent(document.title)+'&event='+encodeURIComponent(strip_tags(document.getElementById(this_layer).innerHTML));
					text_array = this_text.split('%20');
					new_text = new String(); i=0;
					text_add = encodeURIComponent('<br/><br/><a href="'+location.href+'">Подробнее</a>');
					while(i<text_array.length && (new_text+text_array[i]+text_add).length < 2048){
						new_text += text_array[i]+'%20';
						i++;
					}
					new_text += '...'+text_add;
					
					window.open(new_text); break;
				case 'tw': window.open('http://twitter.com/home/?status='+location.href+'+-+'+encodeURIComponent(this_title)); break;
				case 'mm': window.open('http://connect.mail.ru/share?share_url='+location.href); break;
				default: break;
			}
		}		
			
		function printFavorites(){
			var favs_array = new Array('fb','tw','lj','vk');
			var titles_array=new Array('Facebook','Twitter','LiveJournal','В контакте');
			document.write('<table cellpadding="0" cellspacing="0"><tr><td>');
			for(i=0; i<favs_array.length; i++){
				document.write('<a title="'+titles_array[i]+'" style="width:26px; height:26px; background:URL(/pics/'+favs_array[i]+'.png) no-repeat; cursor:pointer; float:left; text-decoration:none; padding:0; margin:0 5px 0 0;" onclick="addFavorites(\''+favs_array[i]+'\')"></a>');
			}
			document.write('</td></tr></table>');
		}
