function initFramesLeftOffset() {
	
	blockLength = $('.frames').innerWidth();
	
	if ($('.frame.f-4').hasClass('active')) {
		return false;
	}			
	if ($('.frame.f-1').hasClass('active')) {
		$('.frames').find('.f-2,.f-3,.f-4').css({left:blockLength + 'px'});
	return false;
	}
	if ($('.frame.f-2').hasClass('active')) {
		$('.frames').find('.f-3,.f-4').css({left:blockLength + 'px'});
		return false;
	}
	if ($('.frame.f-3').hasClass('active')) {
		$('.frames').find('.f-4').css({left:blockLength + 'px'});
		return false;
	}
			
}
		

window.typeID = 0;
window.Tag = '';

$(document).ready(function(){
	
	jQuery.fn.checkEmailField = function() {
		var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
		var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,8}|[0-9]{1,3})(\\]?)$");
		if (r1.test($(this).attr('value')) || !r2.test($(this).attr('value'))) {
			return false;
		} else {
			return true;
		}
	};
	
	jQuery.fn.updateButtonStatus = function() {
		classnames = $(this).attr('class').split(' ');
		for (j = 0; j<classnames.length; j++) {
			if (classnames[j].indexOf('button-') == 0) {
				btn_selector = 'input.' + classnames[j].replace('button-','') + ',button.' + classnames[j].replace('button-','');
				inp_selector = 'input.' + classnames[j] + ',textarea.' + classnames[j];
				break;
			}
		}
		$(btn_selector)[0].disabled = false;
		$(btn_selector).parent().removeClass('blocked');
		$(inp_selector).each(function(){
			$(this).parent().parent().removeClass('error');
			if ($(this).attr('value') == '' || typeof $(this).attr('value') == 'undefined') {
				$(btn_selector)[0].disabled = true;
				$(btn_selector).parent().addClass('blocked');
			}
			if ($(this).hasClass('email-check') && !$(this).checkEmailField()) {
				$(btn_selector)[0].disabled = true;
				$(btn_selector).parent().addClass('blocked');
			}
		});
		return false;
	};
		
	$('.necessary').each(function(){
		//alert($(this).attr('class'));
		/*
		if ($(this).attr('value') == '' || typeof $(this).attr('value') == 'undefined' || ($(this).hasClass('email-check') && !$(this).checkEmailField())) {
			$(this).addClass('focus');
		}
		*/
		$(this).updateButtonStatus();
	});

	window.formInit = 0;
	
	$('.necessary').focus(function(){
		if (!window.formInit) {
			$('.necessary').not(this).each(function(){
				if ($(this).attr('value') == '' || typeof $(this).attr('value') == 'undefined' || ($(this).hasClass('email-check') && !$(this).checkEmailField())) {
					$(this).addClass('focus');
				}
			});
			window.formInit = 1;
		}
		$(this).removeClass('focus');
	}).change(function(){
		$(this).updateButtonStatus();
	}).keyup(function(){
		$(this).updateButtonStatus();
	}).focus(function(){
		$(this).updateButtonStatus();
	}).click(function(){
		$(this).updateButtonStatus();
	}).blur(function(){
		if ($(this).attr('value') == '' || typeof $(this).attr('value') == 'undefined' || ($(this).hasClass('email-check') && !$(this).checkEmailField())) {
			$(this).addClass('focus');
		}
		$(this).updateButtonStatus();

	});
	
	function selectByTag() {

		$('.tags-selection').show();
		if (window.Tag == '') {
			return false;
		}
		$('.tags-selection').hide();
		$('.tags span b').not('.type-select,.reset-tags').each(function(){
			if ($(this).text().toLowerCase().search(window.Tag) == 0 && $(this).text().length == window.Tag.length) {
				$(this).parent().parent().addClass('active');
				if($(this).parent().parent().parent().parent().parent().hasClass('tags-selection')) {
					$(this).parent().parent().parent().parent().parent().show();
				}
				if($(this).parent().parent().parent().parent().parent().parent().hasClass('tags-selection')) {
					$(this).parent().parent().parent().parent().parent().parent().show();
				}
				
			} else {
				//$(this).parent().parent().parent().parent().parent().hide();
				$(this).parent().parent().removeClass('active');
				$(this).parent().parent().parent().removeClass('active');
			}
		});
		
	}
	
	function selectByType() {
		
		if (window.typeID) {
			$('.tags-selection').not('.type-' + window.typeID).hide();
		}
		
	}
	
	function resetTags() {
		
		$('.news-decor, .see-also').fadeOut(200);
		window.Tag = '';
		$('.tags-selection').show();
		$('.tags-headline').show();
		$('.all-tags').hide();
		$('.tags span b').not('.type-select,.reset-tags').each(function(){
			$(this).parent().parent().removeClass('active');
		});
	}
	
	$('.tags span b').click(function(){

		if ($(this).hasClass('type-select')) {
			return false;
		}
		if ($(this).hasClass('reset-tags')) {
			resetTags();
			selectByType();
		} else {
			$('.tags-selection').hide();
			$('.tags-headline').hide();
			/*
			if (window.typeID > 0) {
				$('.all-tags .type-' + window.typeID).show();
			} else {
			*/
			$('.all-tags').show();
			//}
			
			$('.news-decor, .see-also').fadeOut(200);

			$.get('?',{get_tags_results:$(this).text()},function(data){
	         	if (data.length < 10) {
	         		return false;
	         	}
	         	$('.news-decor').show();
		 		$('.see-also').html(data).fadeIn(200);
		 		
			});

			window.Tag = $(this).text().toLowerCase();
			selectByTag();
			selectByType();
		}
		
		return false;
		
	});
	
	$('.type-select').click(function(){
		
		tid = $(this).attr('class').replace('type-select','').replace('type-','') * 1;
		window.typeID = tid;
		$('.top-tags li').removeClass('active');
		$('.type-select').parent().parent().removeClass('active');
		$('.type-select.type-' + tid).parent().parent().addClass('active');
		selectByTag();
		selectByType();
		
	});
	
	prts = document.location.href.split('#');
	
	if (prts.length > 1 && prts[1].indexOf('type-') == 0 ) {
		window.typeID = prts[1].replace('type-','') * 1;
		$('.top-tags li').removeClass('active');
		$('.type-select').parent().parent().removeClass('active');
		$('.type-select.type-' + window.typeID).parent().parent().addClass('active');
		selectByTag();
		selectByType();
	}
	
	$('.text table').each(function(){
		if ($(this).find('th').length > 0) {
			return true;
		}
		$(this).find('tr:eq(0)').addClass('first-row');
	});
	
	window.framesAnimationProgress = 0;	
	
	jQuery.fn.frameSelect = function(){
		//function(){
		$(this).find('dt').animate({width:'200px'},300,function(){
			$(this).parent().addClass('active');
			$(this).parent().find('dd').fadeIn(200);
			//})
			window.framesAnimationProgress = 0;
		});
	};
	jQuery.fn.frameUnSelect = function(){
		$(this).find('dd').fadeOut(100);
		$(this).find('dt').animate({width:'108px'},100,function(){
			$(this).parent().removeClass('active');
		});
	};
	
	
	
	if ($('.frames .frame').length == 4) {
		
		$(window).resize(function() {
			initFramesLeftOffset();
		});
		
		initFramesLeftOffset();
				
	}
	
	$('.frame').hover(function(){
		if ($(this).hasClass('active') || window.framesAnimationProgress) {
			return false;
		}		
		window.framesAnimationProgress = 1;
		
		$('.frame.active').frameUnSelect();
		$('.frame').removeClass('active');
		$('.frame').removeClass('left');
		
		blockLength = $('.frames').innerWidth();
		
		if ($(this).hasClass('f-1')) {
			$('.f-2').animate({left:blockLength + 'px',marginLeft:'-326px'},200);
			$('.f-3').animate({left:blockLength + 'px',marginLeft:'-217px'},200);
			$('.f-4').animate({left:blockLength + 'px',marginLeft:'-108px'},200);
		}
		if ($(this).hasClass('f-2')) {
			$('.f-2').animate({left:'108px',marginLeft:'0'},200);
			$('.f-4').animate({left:blockLength + 'px',marginLeft:'-108px'},200);
			$('.f-3').animate({left:blockLength + 'px',marginLeft:'-217px'},200);
		}

		if ($(this).hasClass('f-3')) {
			$('.f-2').animate({left:'108px',marginLeft:'0'},200);
			$('.f-3').animate({left:'217px',marginLeft:'0'},200);
			$('.f-4').animate({left:blockLength + 'px',marginLeft:'-108px'},200);
		//	$('.f-2').animate({left:'99%',marginLeft:'-362px'},200);//addClass('left');
		}

		if ($(this).hasClass('f-4')) {
			$('.f-2').animate({left:'108px',marginLeft:'0'},200);
			$('.f-3').animate({left:'217px',marginLeft:'0'},200);
			$('.f-4').animate({left:'326px',marginLeft:'0'},200);
		}
		
		$(this).frameSelect();//addClass('active');
		
	},function(){});
	
	$('.top-tags a.subscribe-news').click(function(){
		$('.subscribing-block').show();
		return false;
	});
	
	$('.subscribing-news a:eq(0)').click(function(){
		$(this).parent().toggleClass('active');
		return false;
	});
	
	$('.subscribing-block i.close.icon').click(function(){
		$('.subscribing-news').removeClass('active');
		return false;
	});
	
	$('.checkboxes input:checkbox').click(function(){
		if ($('.checkboxes input:checkbox:checked').length == 0) {
			$(this).attr('checked',1);
		}
	});
	
	$('#subscribe-form').submit(function(){		
		$('#subscribe-form .capcha-block input').removeClass('error');
		$.post('?', $(this).find('input').serialize(),function(data){
			if (data != 'ok') {
				$('#subscribe-form .capcha-block input').addClass('error');
			} else {
				$('#subscribe-form').fadeOut(200,function(){
					$('.subscription-success').show();
				});
			}
		});
		return false;
	});

	/*
	if ($('.main-page .head object').length > 0) {
		swfobject.registerObject("FlashID");
		$('.main-page .head').hover(function(){
			var target = swfobject.getObjectById("FlashID");
			if (target != null) {
				target.jsOnMouseOver();
			}
		},function(){
			var target = swfobject.getObjectById("FlashID");
			if (target != null) {
				target.jsOnMouseOut();
			}
		});
	}
	*/
	
	if ($('.flash-container').html() != null) {

		if (!$.flash.available) {
			
			$('.noflash-link').removeClass('dnone');
			$('.top .head').addClass('no-flash');
	
		} else {
			
			$('.flash-container').flash({
				//swf: '/files/flash/preloader.swf',
				swf: '/files/flash/preloader.swf?r=' + Math.random(),
				hasVersion: 9, // optional and temporary   
				expressInstaller: '/files/flash/express_install.swf',
				height: 400,
				width: '100%',
				params: {
					allowScriptAccess: 'always',
					wmode: 'opaque',
					quality: 'high'
				},
				flashvars: {
					languageSettsURL: '?get_xml=preloaderlanguages.xml',
					mustBeLoadedURL: '?get_xml=mustbeloaded.xml',
					textsURL: '?get_xml=texts.xml',
					presentationSize: window.presentationSize,
					//presentationURL: '/files/flash/presentation.swf',
					presentationURL: '/files/flash/presentation.swf?r=' + Math.random() * 2,
					lang: ($('.main-page .frames .frame').length != 4) ? 'en' : 'bel'
				}
			});
	
			$('.flash-container object').attr('width','100%');
			$('.flash-container object').css({width:'100%'});
			
			
			$('.flash-container').hover(function(){
				target = $('.flash-container object')[0];//flash();
				if (typeof target['jsOnMouseOver'] == 'function') {
					target.jsOnMouseOver();
				} 
			},function(){
				target = $('.flash-container object')[0];
				if (typeof target['jsOnMouseOut'] == 'function') {
					target.jsOnMouseOut();
				} 
			});
		}
	}
	
	$('.one-more-letter').click(function(){
		$(this).parent().parent().hide();
		$('#quick-contact-form').show();
		return false;
	});
	
	$('.scheme-map a b').click(function(){
		$('.scheme-map a b').parent().toggleClass('active');
		$('.scheme').toggleClass('dnone');
		return false;
	});
	
	$('.peoples-search span').click(function(){
		$('#search_form input:eq(0)').val($(this).text()).focus();
		return false;
		//console.log($(this).text());
	});
	
});
