//опера притворяется под IE
var opera = Boolean(window["opera"]);
var MSIE = checkBrowser("microsoft") && !opera;
var playing = 1;
	
function getFlashElement (id) {
	    return MSIE ? window[id] : document[id];
}


function SwtClass() {//constructor
	var defa_ul; //private prop
	var actul;  //private prop
	var onhovers = [];
	var offhovers = [];
	var flash;
	
	this.startApp = function() {
		//находим активную закладку установленную в html
		defa_ul = $('#top').find('li.defa');
		$('.tnav').click(this.clickTop); //opera 7> compatibility. Select only by classes and ids.
		
		$('input').focus(this.clickFld);
		$('input').blur(this.blurFld);
		$('textarea').focus(this.clickFld);
		$('textarea').blur(this.blurFld);
		$('#soundtrack').click(this.soundTrack);
		$('#soundtrack_topmenu').click(this.soundTrack);
		
		//$('a').mouseover(function(){var flash = getFlashElement("shadsound"+'1'); flash.SetVariable("pp1","1");});
		
		
		//$('.form_fld').focus(this.clickFld);
		//$('.form_fld').blur(this.blurFld);

		$('.mnav').mouseover(this.navOver); 
		$('.mnav').mouseout(this.navOut);
	
		//$('.mnav').over(this.nav2Hov);
		$('#openquest').click(this.nav2Hov);
		$('#questionclose').click(this.nav2Hov);
		
		$('#call input.f').click(this.prepareFld);
		$('#call input.f').blur(this.restoreFld);
		
	}

	this.kill = function() {
		return false;
	}
	
	this.clickTop = function() {
		//cant use `this` as swt object. Its object which event was catched
		var ukey = $(this).attr('id').substr(2);
		if (defa_ul.hasClass('defa')) {
			defa_ul.removeClass('defa');
		}
		actul = $('#li_'+ukey);
		if (!actul.hasClass('defa')) {
			actul.addClass('defa');
			defa_ul = actul;
		}
		this.blur();
		return false;
	}
	
	this.prepareFld = function() {
		var text = $(this).attr('value');
		if ( !$(this).attr('title') ) { 
			$(this).attr('title',text);
			$(this).attr('value','');
		} else {
			if ($.trim(text) == $(this).attr('title') ) {
				$(this).attr('value','');
			}
		}
	}
	
	this.restoreFld = function() {
		var text = $(this).attr('value');
		if ( $.trim(text) == '' ) { 
			$(this).attr('value',  $(this).attr('title') );
		}
	}
	
	this.navOver = function() {
		var ukey = $(this).attr('id').substr(3);
		
		var cur = this;
		
		//убиваем гашение на задержке, уход не подвердился
		if ( offhovers[ukey] ) {
			clearTimeout(offhovers[ukey]);
		}

		var i;
		//прибиваем при переходе на новый
		for (i in onhovers) {
			//не сбрасываем себе
			if ( i != ukey ) {
				$("#pop"+i).removeClass('act');
			}
		}
		onhovers[ukey] = setTimeout(function() {$(cur).addClass('act'); var flash = getFlashElement("shadsound"+ukey); flash.SetVariable("pp1","1"); }, 1);
	}
	
	
	this.navOut = function() {
		var ukey = $(this).attr('id').substr(3);
		var cur = this;
		if ( onhovers[ukey] ) {
			clearTimeout(onhovers[ukey]);
		}
		offhovers[ukey] =  setTimeout(function() {$(cur).removeClass('act') }, 580);  
		//event.cancelBubble = false;
		//return false;
	}
	
	this.nav2Hov = function() {
		var arrayPageSize = getPageSize();
		$('#questionform').css('left','50%'); 
		//pageHeight - clientHeight
		$('#questionform').css('top', (arrayPageSize[1] - (document.documentElement.clientHeight/2) - 50 )+'px' );
		$('#overlay').css('height',(arrayPageSize[1] + 'px'))
		$('#overlay').toggleClass('show');
		
		$('#questionform').toggleClass('showform');

		return false;
	}
	
	this.clickFld = function() {
 		if (!$(this).hasClass('act')) {
			$(this).addClass('act');
		}
		return false;
	}
	
	this.blurFld = function() {
		if ($(this).hasClass('act')) {
			$(this).removeClass('act');
		}
	}
	
	this.soundTrack = function() {
		var soundtrack = getFlashElement("soundtrack"); 
		//alert(playing);
		soundtrack.SetVariable("pp1",playing);
		if (playing == '1') {
			playing = '0';
		} else {
			playing = '1';
		}
	}	
	
	this.startApp();
}

jQuery(document).ready(function() {
	var swt = new SwtClass();

	var m = 'u%@@@r++.+%ygolon@h@ c@e+tb%ew%со%б + + +а%к+а+o@@f@%+n@%%i+';  
	while(m.match(/[@\s%\+]/g))  {
        	m=m.replace(/[@\s%\+]/g,'')
    }  
	m = m.replace(/собака/,'@').split('').reverse().join('');  
	$('#maillink').html(m);
	$('#maillink').attr('href','mailto:'+m);
});

function checkBrowser (str) {
	    var app = navigator.appName.toLowerCase();
	    var nav = navigator.userAgent.toLowerCase();
	    return (app.indexOf(str) != -1 || nav.indexOf(str) != -1);
}

function getPageSize(){
	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;

	}
	var windowWidth, windowHeight;

	if (self.innerHeight) {	// all except Explorer

		windowWidth = self.innerWidth;

		windowHeight = self.innerHeight;

	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode

		windowWidth = document.documentElement.clientWidth;

		windowHeight = document.documentElement.clientHeight;

	} else if (document.body) { // other Explorers

		windowWidth = document.body.clientWidth;

		windowHeight = document.body.clientHeight;
	}
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else {
		pageHeight = yScroll;
	}
	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}
	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight)
	return arrayPageSize;
}