$(document).ready(function(){

	/* resize */
	var mw = 239;
	function floatBlocks() {
		$(".floats").each(function(){
			full = $(this).width();
			fit = full/mw;
			if (fit!=0) {
				if (fit >= Math.round(fit)) {
					fit = Math.round(fit)
				} else {
					fit = Math.round(fit-1);
				}
				fit = 100/fit + '%';
				
				$(this).find("li").css("width", fit);
			}
		});
	}
	$(window).bind('resize', function() {floatBlocks();});
	floatBlocks();

	$('.tabs').removeClass("static");

				
	$('a.tab_previews').click(function() { 
		$('a.tab_list').parent().removeClass("tabs-selected"); 
		$(this).parent().addClass("tabs-selected");
		$('#fragment-1').show();
		$('#fragment-2').hide();
		floatBlocks();
		return false;
	});
	$('a.tab_list').click(function() {
		$('a.tab_previews').parent().removeClass("tabs-selected");
		$(this).parent().addClass("tabs-selected");
		$('#fragment-2').show();
		$('#fragment-1').hide();
		floatBlocks();
		return false;
	});
	

	/* punctuation*/
	$.fn.punctuation = function(){
		return this.each(function(){
			var jqCode = $(this);
			var code = jqCode.html();
			code = code
				.replace(/^\(\«/, '<span class="bracket-laquo">(&laquo;</span>')
				.replace(/^\«\(/, '<span class="bracket-laquo">&laquo;(</span>')
				.replace(/^«/, '<span class="laquo">&laquo;</span>')
				.replace(/^\(\„/, '<span class="bracket-bdquo">(&bdquo;</span>')
				.replace(/^\„\(/, '<span class="bracket-bdquo">&bdquo;(</span>')
				.replace(/^„/, '<span class="bdquo">&bdquo;</span>')
				.replace(/^\(\“/, '<span class="bracket-ldquo">(&ldquo;</span>')
				.replace(/^\“\(/, '<span class="bracket-ldquo">&ldquo;(</span>')
				.replace(/^“/, '<span class="ldquo">&ldquo;</span>');
			jqCode.html(code);
		});
	}
	$.fn.punctuation2 = function(){
		return this.each(function(){
			var jqCode = $(this);
			var code = jqCode.html();
			code = code.replace(/^—\s/, '<span class="mdash">&mdash;</span>').replace(/^—/, '<span class="mdash">&mdash;</span>');
			jqCode.html(code);
		});
	}

	$("h1, h2, h3, h4, h5, h6, p, dt, dd, li").not(".tabs li, .works dd").punctuation();
	$("h1, h2, h3, h4, h5, h6, .services-type2 h4, .vacancies h3").punctuation2();


	$(".nav-prev-next li a, .works dd a, .img2 a, .again a, .latest-works li a").hover(
		function() { $(this).parents("li, dl, p").addClass("hover2"); },
		function() { $(this).parents("li, dl, p").removeClass("hover2");
	});
	
	$("a .pic").animate({opacity: 1}, 0).hover(
		function() { $(this).stop(true).animate({opacity: .75}, {duration: 200, queue: false})},
		function() { $(this).stop(true).animate({opacity: 1}, {duration: 400, queue: false})}
	);
	
	$(".nav-prev-next li a:not(:has(.pic)), .works dd a:not(:has(.pic)), .img2 a:not(:has(.pic)), .main-holder4 .latest-works li a:not(:has(.pic))").hover(
		function() { $(this).parents("li, dl").find(".pic").stop(true).animate({opacity: .75}, {duration: 200, queue: false})},
		function() { $(this).parents("li, dl").find(".pic").stop(true).animate({opacity: 1}, {duration: 400, queue: false})}
	);
	$(".rss").animate({opacity: 1}, 0).hover(
		function() { $(this).stop(true).animate({opacity: .75}, {duration: 200, queue: false})},
		function() { $(this).stop(true).animate({opacity: 1}, {duration: 400, queue: false})}
	);
	$(".articles h3 a").hover(
		function() { $(this).parent().prev().stop(true).animate({color: "#444444"}, {queue:false, duration:250 })},
		function() { $(this).parent().prev().stop(true).animate({color: "#B4B4B4"}, {queue:false, duration:250 })}
	);
	
	    $(".works-text a").hover(
        function() { $(this).parent().children(".date").stop(true).animate({color: "#444444"}, {queue:false, duration:250 })  },
        function() { $(this).parent().children(".date").stop(true).animate({color: "#B4B4B4"}, {queue:false, duration:250 })}
    );
	
	if ($(".form-order").length) {
		var re = /^\w+([\.-]?\w+)*@(((([a-z0-9]{2,})|([a-z0-9][-][a-z0-9]+))[\.][a-z0-9])|([a-z0-9]+[-]?))+[a-z0-9]+\.([a-z]{2}|(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum))$/i;
		var name, mail, task;
			
		var activateIfValid = function() {
			if (name && mail && task) {
				$(".form-order input[type=submit]").removeAttr('disabled').removeClass("disabled");
			} else {
				$('.form-order input[type=submit]').attr("disabled","disabled").addClass("disabled");
			}
		};
				
		$(".form-order input[type=submit]").attr("disabled","disabled").addClass("disabled");
			
		$(".form-order .field-mail .type-text")
			.bind("focus", function() {
				$(this).parents("dl").removeClass("error");
			})
				
			.bind("blur", function() {
				var new_value = $(this).attr("value");
				if (new_value == '') {
					mail = false;
				} else if (!re.test(new_value)) {
					mail = false;
					$(this).parents("dl").addClass("error");
				} else {
					mail = true;
				}
			})
				
			.bind("keyup", function() {
				var new_value = $(this).attr("value");
				if (new_value == '' || !re.test(new_value)) {
					mail = false;
				} else {
					mail = true;
				}
				activateIfValid();
			});
			
		$(".form-order .field-name .type-text")
			.bind("keyup", function(){
				name = $(this).val() != "";
				activateIfValid();
			});
				
		$(".form-order .field-task textarea")
			.bind("keyup", function(){
				task = $(this).val() != "";
				activateIfValid();
			});
	}
	
	/* overlabel  */
	$.fn.overlabel = function( options ) {
		var opts = $.extend( {}, $.fn.overlabel.defaults, options );
		var selection = this.filter( 'label[for]' ).map( function() {
			var label = $(this);
			var id = label.attr('for');
			var field = document.getElementById( id );
			if (!field) return;
			var o = $.meta ? $.extend( {}, opts, label.data() ) : opts;
			label.addClass( o.label_class );
			var hide_label = function() { label.css( o.hide_css ) };
			var show_label = function() { this.value || label.css( o.show_css ) };
			$( field ).parent().addClass( o.wrapper_class ).end().focus( hide_label ).blur( show_label ).each( show_label );
			return this;
		});
		return opts.filter ? selection : selection.end();
	};

	$.fn.overlabel.defaults = {
		label_class:   'overlabel-apply',
		wrapper_class: 'overlabel-wrapper',
		hide_css:      { 'display': 'none' },
		show_css:      { 'display': 'block', 'cursor': 'text' },
		filter:        false
	};

	$("label.overlabel").show().overlabel();
	
});