$(document).ready(function() {
	var bar=setInterval(function(){if($('#bar').width()==940){$('#bar').bar();clearInterval(bar);}},100);
	//BEGUN
	$('#begun a').mouseover(function(){clearInterval(begun);}).mouseout(function(){begun=setInterval('animateBegun();',20);});
	//////////////////////
	$('#chlang').click(function(){$('#langs').fadeIn(100);});
	$('#langs').mouseout(function(){var el=this;dis=setTimeout(function(){$(el).fadeOut(200);},200);});
	$('#langs').find('a,li,ul').mouseover(function(){clearInterval(dis);});

	$('#chlang>span,#langs a').each(function(){$(this).css({'background-image':'url(/images/flag/'+$(this).text()+'.gif)'}).addClass('flag');});
	$('#searchbtn').val($('#searchmsg').text());
    
    $('#menus a.mainmenu').each(function(){
        $(this).mouseover(function(){
            $(this).addClass('hover');
            showSubMenu(this.getAttribute('rel'));
            $('#menus a.mainmenu').not(this).removeClass('hover');
        })
        .mouseout(function(){
            subMenuClose();
        });
        showSubMenu(this.getAttribute('rel'));
    });
});
var returnsubmenu=null;
jQuery.fn.bar = function(options){
	  	// настройки по умолчанию
		var options = jQuery.extend({
			time:500
		},options);
		var container,width=0,change,moved=false;
		var start = function(el){
			container=el;
			var reg=/glossary/gi;
			var num=0;
			if(reg.test(window.location)) {num=1;jQuery('div.title',el).eq(num).parent().css('background-image','none')}
			jQuery('div.title',el).eq(num).addClass('active');
			jQuery('div.content',el).hide(0).css({'opacity':0,'width':0});
			var cont=jQuery('div.content',el).eq(num);
			jQuery('div.title',el).each(function(i,elm){
				var w=jQuery(elm).outerWidth(1);
                width+=w;
				jQuery(elm).click(function(){if(moved) return false;jQuery('div.title',container).removeClass('active');jQuery(this).addClass('active');move(this);});
                jQuery(elm).parent().width(w);
			});
			width=jQuery(el).width()-width-1;
            jQuery('div.title',el).eq(num).parent().width(jQuery('div.title',el).eq(num).outerWidth(1)+width);
			cont.width(width).show(0).animate({'opacity':1},{duration:options.time});
			if(cont.hasClass('begun')) begun=setInterval('animateBegun();',20);
		}
		var move=function(el) {
			if(jQuery(el).parent().find('div.content').is(':visible') || moved) return false;
			moved=true;
			if(jQuery.browser.msie && parseInt(jQuery.browser.version)==8) jQuery('#begun').animate({'opacity':0},options.time);
			jQuery('div.content:visible',container).animate(
				{'opacity':0},
				{
					duration:options.time,
					complete:function(){
						/*if(jQuery(this).hasClass('begun'))*/ clearInterval(begun);
						var close=jQuery(this),open=jQuery(el).parent().find('div.content');
						open.width(0).show(0);
						if(open.parent().find('div.title').hasClass('enter')) open.parent().next().css('background-image','');
						var shag=parseInt(width/options.time*30);
						change=setInterval(
							function(){
								var cl=close.width()-shag,op=open.width()+shag;
								close.width(cl<0?0:cl);
								open.width(op>width?width:op);
                                
                                var closeprev=close.parent().children('div.title').outerWidth(1);
                                var opprev=open.parent().children('div.title').outerWidth(1);
                                close.parent().width(cl<0?closeprev:(cl+closeprev));
                                open.parent().width(op>width?(width+opprev):(op+opprev));
                                
								if(cl<0) {
									clearInterval(change);
									close.hide(0);
									if(close.parent().find('div.title').hasClass('enter')) close.parent().next().css('background-image','none');
									open.animate({'opacity':1},{duration:options.time,complete:function(){moved=false;}});
									if(open.hasClass('begun')) {
										$('#begun').css({'marginLeft':'0px','opacity':1});
										begun=setInterval('animateBegun();',30);
									}
									return;
								}
							},
							30
						);
					}
				});
		}

		return this.each(function() {
			start(this);
		});

	};

var begun,dis;
function animateBegun(){
	var margin=parseInt($('#begun').css('marginLeft'));
	margin=margin?margin:0;
	if($('#begun').width()+margin+$('#begun').parent().width()<0){
		$('#begun').css({
			'marginLeft':'0px'
		});
	}
	else {
		$('#begun').css({
			'marginLeft':(margin-1)+'px'
		});
	}
}
function checkEmail(email){
	if(!email) return false;
	var reg = /[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/;
	return reg.test(email);
}
function send(mode,v){
	var req = new JsHttpRequest();
    req.onreadystatechange = function() {
        if (req.readyState == 4) {
            if(req.responseJS) {
				if(req.responseJS.answer) {
					$('#modalWindow').html(req.responseJS.answer);
					$('#modalWindow').modal({
						overlayClose:true,
						containerCss: {
							width:700,
							height:400
						}
					});
				}
            }
            if(req.responseText!='') alert(req.responseText);
        }
    }
    req.open(null, '/ajax.php', true);
    req.send( { mode: mode,q:(v)?v:null,lang:$('#chlang').text() } );
}

$(document).ready(function() {
	var heights=Array();
	$('div.gallery div.album').each(function(){
		heights.push($(this).height());
	});
	$('div.gallery div.album').height(heights.sort(sortNumber)[0]);
});
function sortNumber(a,b){return b - a;}

function getSubMenu(text){ 
    if(!text || text=='') return;
    var req = new JsHttpRequest();
    req.caching = true;
    req.onreadystatechange = function() {
        if (req.readyState == 4) {
            if(req.responseJS) {
                if(req.responseJS.answer) {
                    $('#menus').append(req.responseJS.answer);
                    if($('#menus>a[rel='+text+']').hasClass('hover')) showSubMenu(text);
                }
            }
            if(req.responseText!='') alert(req.responseText);
        }
    }
    req.open(null, '/ajax.php', true);
    req.send( { mode: 'submenu',lang:text.replace(/^\//,'') } );
}
function showSubMenu(text){
    if(!text || text=='') return;
    clearInterval(returnsubmenu);
    $('#menus div.smgroup').removeClass('active');
    var sub=$('#menus div[rel='+text+']');
    if(sub.length) {
        sub.addClass('active');
        var left=$('#menus>a[rel='+text+']').position().left;
        sub.css({
            left:left+'px'
        });
    }
    else getSubMenu(text);
}
function subMenuOver(el){
    if(!el)return false;
    clearInterval(returnsubmenu);
    returnsubmenu=null;
    $('#menus>a[rel='+$(el).attr('rel')+']').addClass('hover');
}
function subMenuOut(el){
    if(!el)return false;
    subMenuClose();
}
function subMenuClose(){
    returnsubmenu=setTimeout(function(){
        $('#menus>a[rel='+$('#menus div.smgroup.active').attr('rel')+']').removeClass('hover');
        $('#menus div.smgroup').removeClass('active');
        clearInterval(returnsubmenu);
        returnsubmenu=null;
    },3000);
}

//GOOGLE ANALYTICS
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-21468667-1']);
_gaq.push(['_trackPageview']);

(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
