/*	Задаем тип документа, в зависимости от размера окна	*/
$(function() {
	var body = $('body');
	if (!body.data('cls')) {
		body.data('cls', $.trim(body.attr('class').replace(/ {2,}/g, ' ')));
	}
	
	$(window).resize(function() {
		var mode = ((1170<=$(window).width()) ? 'full' : 'normal') + '-mode';
		var cls = body.data('cls').split(' ');
		$.each(cls, function(i, v) {
			cls[i] = v + ' ' + v + '-' + mode;
		});
		
		cls.unshift('g-' + mode);
		body.attr('class', cls.join(' '));
		
		/*	Для ie<=6 задаем максимальную и минимальную ширину	*/
		if ($.browser.msie && $.browser.version < 7) {
			var maxWidth = 0;
			$('.g-minimaze_width').each(function() {
				$(this).css('width', '100%');
				maxWidth = Math.max(maxWidth, $(this).width());
			}).each(function() {
				$(this).width(Math.max((mode == 'full' && body.hasClass('g-intro') ? 1170 : 1000), maxWidth));
			});
			
			$('.g-maximaze_width').each(function() {
				$(this).width(Math.min(1600, $(this).width()));
			});
		}
		
	}).triggerHandler('resize');
});

$(function() {
/*	Задаем одинаковую высоту элементам в списке работ	*/ 
	var maxHeight = 0;
	$(".b-works_list-item-content", "#works_list").each(function(){
		maxHeight = Math.max(maxHeight, $(this).height());
	}).height(maxHeight);
});

/*	Делаем label "активным"	*/
$(function() {
	$("input[type='text'], textarea")
		.blur(function(){
			$("label[for='" + $(this).attr('id') + "']").css("textIndent", (($(this).val($.trim($(this).val())).val() == '') ? "0px" : "-9999px"));
		})
		.focus(function(){
			$("label[for='" + $(this).attr('id') + "']").css("textIndent", "-9999px");
		})
	.triggerHandler('blur');
});

/*	"Обвешиваем" изображения "сафари"	*/
$(function() {
	$('img', '.b-content-portfolio').wrap('<div class="b-preview g-inbl"></div>')
		.parent().prepend('<i class="t"><i></i></i>').append('<i class="b"></i>').each(function() {
			if ($.browser.msie && $.browser.version < 8) {
				if ($(this).parent('a').length) {
					$(this).parent('a').width($(this).find('img').attr('offsetWidth')).css({display:'block'})
						.find('.b-preview').removeClass('g-inbl');
				} else {
					$(this).width($(this).find('img').attr('offsetWidth'));
				}
			}
		})
		.parent('a').addClass('b-content-portfolio-wrapper');
})

/*	Скроллим работы	*/
$(function() {
	var root = $("#works_list");
	var scroll = $('.b-works_list-container', root);
	var container = scroll.find('ul');
	
	var endPos = 0;
	var time = 0;
	
	$(window).resize(function() {
		endPos = Math.max(0, container.width() - scroll.width());
		time = endPos * 0.8;
		scroll.stop().attr('scrollLeft', 0);
	}).triggerHandler('resize');
	
	$('.b-works_list-nav-prev', root).hover(function(){
		scroll.animate({scrollLeft:0}, { duration: time, queue:false})
	}, function(){
		scroll.stop();
	});
	
	$('.b-works_list-nav-next', root).hover(function() {
		scroll.animate({scrollLeft:endPos}, { duration: time, queue:false})
	}, function() {
		scroll.stop();
	});
})

$(function() {
	$(window).resize(function() {
		var cols = $("ul.b-smart-columns").css({ 'width' : "100%"});
		var colWrap = cols.width();
		
		cols.css({ 'width' : colWrap})
			.find('li').css({ 'width' : Math.floor(colWrap / Math.floor(colWrap / 256))});
	
	}).triggerHandler('resize');
	
	$('.b-specialoffers-wrapper').easySlider({
		speed: 800,
		auto: true,
		pause: 8000,
		continuous: true,
		controlsShow: false
	});
	
	$("form.right input[type='text']")
		.blur(function(){
			$(this).attr('value', 'Поиск');
		})
		.focus(function(){
			$(this).attr('value', '').focus();
		})
	.triggerHandler('blur');
	$("form.center_form input[type='text']")
		.blur(function(){
			$(this).attr('value', 'Поиск');
		})
		.focus(function(){
			$(this).attr('value', '').focus();
		})
	.triggerHandler('blur');
	
	var w = parseInt($('.b-works_list-container').width());
	
	$.fn.hoverscroll.params = $.extend($.fn.hoverscroll.params, {
		vertical: false,
		width: w,
		height: 400,
		arrowsOpacity:	0.1,
		arrows: true
	});
	$('#slide_menu').hoverscroll();

});
