//  (c) 2010 Dmitry Ovod-Marchuk
//  http://helloworld.by/

function switchProjects() {
	
	if (window.widgetClicked) {
		return false;
	}
	pclass = parseInt($('.w-active').attr('class').replace(' ','').replace('project-','').replace('w-active','').replace('w-hover',''));
	pclass++;
	
	if (pclass > 4) {
		pclass = 0;
	}
	
	//$('.screen.projeeq(0)').addClass('w-active').removeClass('w-hover');
	$('.w-active').removeClass('w-active');
	$('.widget dl .project-' + pclass).addClass('w-active');
	$('.screen:visible').fadeOut(200,function(){
		pclass= $('.w-active').attr('class').replace('w-active','').replace('w-hover','').replace(' ','');
		$('.screen.' + pclass).fadeIn(500);		
	});
	//window.setTimeout('switchProjects()',7000);
	
}

function imgSlideLoaded($img) {
	$img.hide().css('visibility','visible').fadeIn(300);
}

function imageLoaded() {
	
	if (typeof $('.lightbox:visible')[0] == 'undefined') {
		return false;
	}
	
	h = ($('.picture img')[0].width);
	
	$('.lightbox:visible').animate({width:h,marginLeft:'-' + Math.round(h/2) + 'px'},200);
	$('.picture').animate({width:h},200,function(){
		$('.picture img').hide().css('visibility','visible').fadeIn(300,function(){
			window.lightboxProgress = 0;
		});
	});
	
}

function shiftLightbox() {

	shift = $(window).scrollTop();
	//block = $('.picture').innerHeight() + 138 + 20;
	block = 670;
	if (shift + block > $('#container').innerHeight()) {
		shift = $('#container').innerHeight() - block;// - 100;
	} else {
		
	}
	$('.lightbox-top').css({top:shift + 'px'});
	$('.lightbox').css({top:(shift + 138) + 'px'});
	
}

function shiftLightboxPreviews() {

	qty = $('.thumbs td').length;
	if (qty < 8) {
		
	} else {
		num = parseInt($('.thumbs-active').attr('class').replace('thumbs-active','').replace(' ','').replace('picture-',''));
		if (num < 5) {
			l = 0;
		} else if (num > qty - 4) {
			l = (qty - 7) * -88;
		} else {
			l = (num - 4) * -88;
		}
		$('.thumbs table').animate({left: l + 'px'},300,function(){
			toggleLightboxArrows();
		});
	}
	
}

function toggleLightboxArrows() {
	
	center = Math.round(parseInt($('.thumbs table').css('left')) / -88) + 4;
	qty = $('.thumbs td').length;
	if (qty < 8) {
		return;
	}
	$('.right-arr,.left-arr').show();
	if (center == 4) {
		$('.left-arr').hide();
	} else if (center == qty - 3) {
		$('.right-arr').hide();
	} 
	
}

function initLightbox() {
	
	if ($('.thumbs-active').length != 1) {
		$('.thumbs td:eq(0)').addClass('thumbs-active');
	}
	qty = $('.thumbs td').length;
	
	if (qty < 7) {
		l = (7 - qty) * 44;
		$('.thumbs table').css({left: l + 'px'});
	} else {
		toggleLightboxArrows();
	}
	
	src = $('.thumbs-active a').attr('href');
	alt = $('.thumbs-active a img').attr('alt');
	code = '<img src="' + src + '" onload="imageLoaded()" style="visibility:hidden" height="500" alt="' + alt + '" title="' + alt + '" />';
	$('.picture').html(code).css({width:'300px'});
	$('.overlay,.lightbox,.lightbox-top').show();

	if (qty == 1) {
		$('.thumbs').hide();
	}
	shiftLightboxPreviews();
	shiftLightbox();
	
}

function partnersSwitch() {
	
	if (!window.partnersAnimation) {
		window.partnersAnimation = 1;
		if (Math.round(parseInt($('.partners-frames table').css('left')) / -200) == ($('.partners-frames table td').length - 1)) {
			$('.partners-frames table').css({left:0});
		}
		l = parseInt($('.partners-frames table').css('left')) - 200;
		$('.partners-frames table').animate({left:l},600,function(){
			window.partnersAnimation = 0;
		});
	}
	window.setTimeout("partnersSwitch()",3000);
	
}

function clientsSwitch() {
	
	if (!window.clientsAnimation) {
		window.clientsAnimation = 1;
		if (Math.round(parseInt($('.clients-frames table').css('left')) / -200) == ($('.clients-frames table td').length - 1)) {
			$('.clients-frames table').css({left:0});
		}
		l = parseInt($('.clients-frames table').css('left')) - 200;
		$('.clients-frames table').animate({left:l},600,function(){
			window.clientsAnimation = 0;
		});
	}
	window.setTimeout("clientsSwitch()",3000);
	
}

$(document).ready(function(){

	$('.content-txt img').each(function(){
		if (typeof $(this)[0].width != undefined) {
			if ($(this)[0].width > 610) {
				r = $(this)[0].width / 610;
				h = Math.round($(this)[0].height / r);
				$(this).css({width: Math.round($(this)[0].width / r)});
				$(this).css({height: h});
			}
		}
	});
	
	$('.content-txt ol li').each(function(){
		code = $(this).html();
		$(this).html('<span>' + code + '</span>');
	});
	
	//$('.content-txt table').each(function(){
	$('.content-txt table tr:eq(0) td').addClass('first-row');
	//});

	$('.content-txt table td').each(function(){
		code = $(this).html().replace('<p>','').replace('</p>','');
		while (code.indexOf('<p>') > -1) {
			code = code.replace('<p>','');
		}
		while (code.indexOf('<p align="center">') > -1) {
			code = code.replace('<p align="center">','');
		}
		while (code.indexOf('</p>') > -1) {
			code = code.replace('</p>','');
		}
		//code = $(this).html().replace('<p>','').replace('</p>','');
		$(this).html(code);
	});

	$('.shadowed').each(function(){
	   $(this).textDropShadow('shadow')
	});
	
	$('.left-btn,.right-btn').hover(function(){
		$(this).addClass('p-hover');
	},function(){
		$(this).removeClass('p-hover');
	});
	
	window.partnersAnimation = 0;
	window.clientsAnimation = 0;
	
	$('.left-btn.partners').click(function(){
		if (window.partnersAnimation) {
			return false;
		}
		window.partnersAnimation = 1;
		if (parseInt($('.partners-frames table').css('left')) == 0){
			l = (-200) *($('.partners-frames table td').length-1);
			$('.partners-frames table').css('left', l + 'px');
		}
		l = parseInt($('.partners-frames table').css('left')) + 200;
		$('.partners-frames table').animate({left:l},600,function(){
			window.partnersAnimation = 0;
		});
	});
	
	$('.right-btn.partners').click(function(){
		if (window.partnersAnimation) {
			return false;
		}
		window.partnersAnimation = 1;
		if (Math.round(parseInt($('.partners-frames table').css('left')) / -200) == ($('.partners-frames table td').length - 1)) {
			$('.partners-frames table').css({left:0});
		}
		l = parseInt($('.partners-frames table').css('left')) - 200;
		$('.partners-frames table').animate({left:l},600,function(){
			window.partnersAnimation = 0;
		});
	});
	
	$('.left-btn.clients').click(function(){
		if (window.clientsAnimation) {
			return false;
		}
		window.clientsAnimation = 1;
		if (parseInt($('.clients-frames table').css('left')) == 0){
			l = (-200) *($('.clients-frames table td').length-1);
			$('.clients-frames table').css('left', l + 'px');
		}
		l = parseInt($('.clients-frames table').css('left')) + 200;
		$('.clients-frames table').animate({left:l},600,function(){
			window.clientsAnimation = 0;
		});
	});
	
	$('.right-btn.clients').click(function(){
		if (window.clientsAnimation) {
			return false;
		}
		window.clientsAnimation = 1;
		if (Math.round(parseInt($('.clients-frames table').css('left')) / -200) == ($('.clients-frames table td').length - 1)) {
			$('.clients-frames table').css({left:0});
		}
		l = parseInt($('.clients-frames table').css('left')) - 200;
		$('.clients-frames table').animate({left:l},600,function(){
			window.clientsAnimation = 0;
		});
	});
	
	if (typeof $('.partners-frames')[0] != 'undefined') {
		window.setTimeout("partnersSwitch()",3000);
	}
	
	if (typeof $('.clients-frames')[0] != 'undefined') {
		window.setTimeout("clientsSwitch()",2500);
		/*
		h = parseInt($('.n-block:eq(0)').height() / 2 - 65);		
		if (h < 0) {
			h = 0;
		}
		$('.clients-frames,.partners-frames').animate({marginTop:h + 'px'},200);
		*/
	}
	
	$('.widget dd').hover(function(){
		if (!$(this).hasClass('w-active')) {
			$(this).addClass('w-hover');
		}
	},function(){
		if (!$(this).hasClass('w-active')) {
			$(this).removeClass('w-hover');
		}
	});
	
	$('.widget dd').click(function(){
		window.widgetClicked = 1;
		if ($(this).hasClass('w-active')) {
			return false;
		}
		$('.w-active').removeClass('w-active');
		$(this).addClass('w-active').removeClass('w-hover');
		$('.screen:visible').fadeOut(200,function(){
			pclass= $('.w-active').attr('class').replace('w-active','').replace(' ','');
			$('.screen.' + pclass).fadeIn(500);		
		});
	});
	
	window.widgetClicked = 0;
	if (typeof $('.widget')[0] != 'undefined') {
		//window.setTimeout('switchProjects()',2000);
	}
	
	$('.thumbs td').hover(function(){
		if (!$(this).hasClass('thumbs-active')) {
			$(this).addClass('thumbs-hover');
		}
	},function(){
		if (!$(this).hasClass('thumbs-active')) {
			$(this).removeClass('thumbs-hover');
		}
	});
	
	$('.right-arr,.left-arr').hover(function(){
		$(this).addClass('th-hover');
	},function(){
		$(this).removeClass('th-hover');
	});
	
	window.lightboxProgress = 0;
	
	$('.thumbs td a').click(function(){
		if ($(this).parent().parent().hasClass('thumbs-active')) {
			return false;
		}
		if (window.lightboxProgress) {
			return false;
		}
		window.lightboxProgress = 1;
		$('.thumbs-active').removeClass('thumbs-active');
		$(this).parent().parent().addClass('thumbs-active').removeClass('thumbs-hover');
		$('.picture img').fadeOut(300,function(){
			src = $('.thumbs-active a').attr('href');
			alt = $('.thumbs-active a img').attr('alt');
			code = '<img src="' + src + '" onload="imageLoaded()" style="visibility:hidden" height="500" alt="' + alt + '" title="' + alt + '" />';
			$('.picture').html(code);
		});

		shiftLightboxPreviews();
		return false;
	});
	
	$('.left-arr').click(function(){
		l = parseInt($('.thumbs table').css('left')) + 88;
		$('.thumbs table').animate({left: l + 'px'},300,function(){
			toggleLightboxArrows();
		});
		return false;
	});
	
	$('.right-arr').click(function(){
		l = parseInt($('.thumbs table').css('left')) - 88;
		$('.thumbs table').animate({left: l + 'px'},300,function(){
			toggleLightboxArrows();
		});
		return false;
	});
	
	$('.overlay,.ico-close,.lightbox-top').click(function(){
		$('.overlay,.lightbox,.lightbox-top').hide();
		return false;
	});
	
	$('.gallery a').click(function(){
		n = parseInt($(this).parent().attr('class').replace('pic-','')) - 1;
		$('.thumbs-active').removeClass('thumbs-active');
		$('.thumbs td:eq(' + n + ')').addClass('thumbs-active');
		initLightbox();
		return false;
	});
	
	if ($('.gallery a').length > 0) {
		$(window).scroll(function(){
			shiftLightbox();
		});
		$(window).resize(function(){
			shiftLightbox();
		});
		$(window).keypress(function(event) {
			if (event.keyCode == 27) {
				$('.overlay,.lightbox,.lightbox-top').hide();
			}
		});
	}
	
	$('.glossary dl a').click(function(){
		if ($(this).parent().parent().hasClass('open')) {
			$('.glossary dl').removeClass('open');
			$(this).parent().parent().removeClass('open');
		} else {
			$('.glossary dl').removeClass('open');
			$(this).parent().parent().addClass('open');
		}
		return false;
	});
		
	
	jQuery.fn.updateButtonStatus = function() {
		classnames = $(this).attr('class').split(' ');
		for (j = 0; j<classnames.length; j++) {
			if (classnames[j].indexOf('button-') == 0) {
				btn_selector = '.' + classnames[j].replace('button-','');// + ',button.' + classnames[j].replace('button-','');
				inp_selector = 'select.' + classnames[j] + ',input.' + classnames[j] + ',textarea.' + classnames[j];
				break;
			}
		}
		$(btn_selector)[0].disabled = false;
		$(btn_selector).removeClass('disabled');
		$(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).addClass('disabled');
				if (window.updateFormFlag) {
					$(this).parent().parent().addClass('error');
				}
			}
			if ($(this).hasClass('email-check')) {
				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'))) {
					$(btn_selector)[0].disabled = true;
					$(btn_selector).addClass('disabled');
					if (window.updateFormFlag) {
						$(this).parent().parent().addClass('error');
					}
				}
			}
		});
		
		if ($('.confirm-check:eq(0)').val() != $('.confirm-check:eq(1)').val()) {
			$('.confirm-check').parent().parent().addClass('error');
			$(btn_selector).addClass('disabled');
		}
		return false;
	};

	$('.necessary').each(function(){
		if ($(this).val() == '' || typeof $(this).val() == 'undefined') {
			$(this).addClass('asterisc');
		}
		$(this).updateButtonStatus();
	});

	window.updateFormFlag = 1;

	$('.necessary').focus(function(){
		$(this).removeClass('asterisc');
	}).change(function(){
		$(this).updateButtonStatus();
	}).keyup(function(){
		$(this).updateButtonStatus();
	}).focus(function(){
		$(this).updateButtonStatus();
	}).click(function(){
		$(this).updateButtonStatus();
	}).blur(function(){
		$(this).updateButtonStatus();
		if ($(this).val() == '' || typeof $(this).val() == 'undefined') {
			$(this).addClass('asterisc');
		}
	});
	
	$('.send-btn a').click(function(){
		//console.log($(this).parents('form').attr('id'));
		if (!$(this).parents('.send-btn').hasClass('disabled')) {
			$(this).parents('form')[0].submit();
		}
		return false;
	});
	
	$('#feedback-form').submit(function(){
		if ($('.send-btn').hasClass('disabled')) {
			return false;
		}
		$('.send-btn strong').css('visibility','hidden');
		$('.send-btn img').css('visibility','visible');
		$.post('?', $(this).find('input,select,textarea').serialize(),function(data){
			$('.send-btn img').css('visibility','hidden');
			$('.send-btn strong').css('visibility','visible');
			$('#feedback-form big').text(data);
			$('#feedback-form big').fadeIn(200);
		});
		return false;
	});
	
/*
	$('.subscribe a').click(function(){
		$('.subscribe strong').not('form strong').hide();
		$('#subscribing').fadeIn(50);
		return false;
	});
*/
	
	$('.subscribe-form input').focus(function(){
		if ($(this).val() == 'E-mail') {
			$(this).val('');
		}
	}).blur(function(){
		if ($(this).val() == '') {
			$(this).val('E-mail');
		}
	});
	
	$('.subscribe-form a').click(function(){
		$('#subscribing').submit();
		return false;
	});
	
	$('#subscribing').submit(function(){
		if ($(this).find('input:visible:eq(0)').val() == 'E-mail' || $(this).find('input:visible:eq(0)').val() == '') {
			return false;
		}
		
		$('.subscribe-form').removeClass('error');
		$('.subscribe-form small').hide();
		
		var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
		var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,8}|[0-9]{1,3})(\\]?)$");
		if (r1.test($('.subscribe-form input:eq(0)').val()) || !r2.test($('.subscribe-form input:eq(0)').val())) {
			$('.subscribe-form').addClass('error');
			$('.subscribe-form .proper').show();
			return false;
		}
		
		$('.loading').css('visibility','visible');
		$('.subscribe-form').hide();
		$.post('?', $(this).find('input').serialize(),function(data){
			$('.loading').hide();
			if (data.indexOf('error') == 0) {
				$('.subscribe-form').addClass('error').show();
				$('.subscribe-form .custom').text(data.replace('error:','')).show();
			} else {
				$('.answer small').text(data);
				$('.answer').show();
			}
		});
		return false;
	});
	
	if ($('#rss-link')[0] != undefined) {
		$('.some-icons .rss-link a').attr('href',$('#rss-link')[0].href);
		$('.some-icons .rss-link').show();
	}
	/*
	if ($('.twi-share-link')[0] != undefined) {
		$('.twi-share-link a').attr('href','http://twitter.com/home?status=' + encodeURIComponent(document.location.href));
	}
	if ($('.fb-share-link')[0] != undefined) {
		$('.fb-share-link a').attr('href','http://www.facebook.com/sharer.php?u=' + encodeURIComponent(document.location.href) + '&t=' + encodeURIComponent(document.title));
	}	
	*/
	
	$('.img-gallery li a').click(function(){
		if ($(this).parent().hasClass('active')) {
			return false;
		}
		was = $(this).parent().parent().find('.active a').attr('href').replace('#','');
		$(this).parent().parent().find('.active').removeClass('active');
		$(this).parent().addClass('active');
		willbe = $(this).attr('href').replace('#','');
		$('.img-gallery .img img').css('visibility','hidden');
		code = $('.img-gallery .img').html().replace(was,willbe);
		$('.img-gallery .img').html(code);
		return false;
	});
	
	$('.img-gallery .img img').live('click',function(){
		if ($('.img-gallery ul .active').next().length) {
			$('.img-gallery ul .active').next().find('a').click();
		} else {
			$('.img-gallery ul li:eq(0)').find('a').click();
		}
		return false;
	});
	
});

(function($) {
	 $.fn.textDropShadow = function(ShdwClass){
	 $(this).css('position','relative').html('<span class='+ShdwClass+'>'+$(this).html()+'</span><span style="position:relative;">'+$(this).html()+'</span>');
	 return $(this);
	 }
})(jQuery);
