var is_popup = false;
$(document).ready(function() {

	inputHelper($('#top-search'), 'Поиск');

/*
	inputHelper($('#price-from'), 'руб.');
	inputHelper($('#price-to'), 'руб.');
	inputHelper($('#size-from'), 'см');
	inputHelper($('#size-to'), 'см');
*/

	resizeRoutine();

	if ($('#flash').length > 0) {
		if(!$.flash.available){
			$('#flash').prepend('<div id="no-flash"></div>')
		}
		$('#flash').flash({
			swf:'/flash/main_05_2.swf',
			width:'100%',
			height:'445',
			wmode: 'transparent'
		})
	}

	if ($('.n-zoom').length > 0) $('.n-zoom a').lightBox();

	if ($('.brands').length > 0) {
		$('.brands li').hover(function() {
			if (!$(this).hasClass('c')) {
				$(this).find('.b-logo').stop().animate({top: '0px'});
				if (!$.browser.msie) $(this).find('i').animate({opacity: 0.3});
			}
		}, function() {
			if (!$(this).hasClass('c')) {
				$(this).find('.b-logo').stop().animate({top: '10px'});
				if (!$.browser.msie) $(this).find('i').animate({opacity: 1});
			}
		});
	}

	if ($('.ab-block').length > 0) {
		$('.ab-block').hover(function() {
			$(this).addClass('ab-hover');
		}, function() {
			$(this).removeClass('ab-hover');
		});
	}

	if ($('.popup').length > 0) {
		$('body').append($('<div>').addClass('popup-bg').css({width: $('body').width(), height: ($('.container').height()), opacity: 0.7}));
		$('.popup-bg').after($('.popup'));
		if ($('.has-popup').length > 0) {
			$('.has-popup').click(function() {
			    $p = $('#' + this.id + '-popup');
				$('.popup-bg').show();
				var t = 150;
				var l = parseInt( ( $('body').width() - $p.width() ) / 2 );
				if ($.browser.msie && $.browser.version < 7) $('select').hide();
				$p.css({left: l+'px', top: t+'px'}).show();
				if ($.browser.msie) $p.find('label').hide().show();
				var target = 'html';
				if ($.browser.safari) target = 'body';
				$(target).animate({scrollTop: '50px'}, 1100);
				is_popup = true;
				return false;
			});
		}

		$('.popup-close').click(function() {
		    popupClose();
		});
		$('body').click(function() {
		    popupClose();
		});

		$('.popup').click(function(e) {
			e.stopPropagation();
		});
	}

	if ($('#slider').length > 0) {
		$('#slider').slider({
			value:	120,
			min:	120,
			max:	270,
			step:	5,
			slide: function(event, ui) {
				$('.items .item-pic').each(function(){
					var w = $(this).width();
					var h = $(this).height();
				 	$(this).css({height: ui.value+'px', width: Math.round( (w * ( ui.value / h ) ) ) + 'px'});
					$(this).find('img').css({height: ui.value+'px', width: Math.round( (w * ( ui.value / h ) ) ) + 'px'});
				 	$(this).find('a').css({height: ui.value+'px', width: Math.round( (w * ( ui.value / h ) ) ) + 'px'});
					
					$(this).parent().find('.item-title').css({width: Math.round( (w * ( ui.value / h ) ) ) + 'px'});
					$(this).parent().find('.item-articul').css({width: Math.round( (w * ( ui.value / h ) ) ) + 'px'});
					$(this).parent().find('.price-con').css({width: Math.round( (w * ( ui.value / h ) ) ) + 'px'});
				})
  			}
		});
	}

	if ($('.form-select-toggle').length > 0) {
		$('.toggle-on').click(function() {
			$(this).parents('.form-select').find('.form-options').show();
		});
		$('.toggle-off').click(function() {
			$(this).parents('.form-options').hide();
		});
		$('.form-options li').click(function() {
			$('.form-options li').removeClass('sel');
			$(this).addClass('sel');
			$(this).parents('.form-select').find('.form-field p').html($(this).html());
			$(this).parents('.form-options').hide();
		});
	}

	if ($('.catalog-search').length > 0) {
		$('.catalog-search-form').hide();
		$('.catalog-search-con').hover(function() {
			$('.catalog-search-form').show();
			$('.catalog-search-form input').focus();
		}, function() {
			$('.catalog-search-form').hide();
		});
	}

	if ($('.item-info .item-ico').length > 0) {
		$('.item-info .item-ico li a').click(function() {
			$(this).parents('.item-pic').find('img').attr({src: $(this).attr('href')});
			$('.item-info .item-ico li a').removeClass('c');
			$(this).addClass('c');
			return false;
		});
	}

	if ($('.catalog-cart-submit').length > 0) {
		$('.catalog-cart-submit').click(function() {
			$('.catalog-cart form').submit();
		});
	}

	// Увеличение картинки на странице артикула при наведении курсора
	if ($('.item-pic img').length > 0) {
		$('.item-pic img').mouseenter(function(){
            var clone = $(this).clone()
            clone.prependTo('.item-left').addClass('clone').css({'position':'absolute', 'z-index':'10'}).animate({
              scale : 2
            },300,function(){
              $('.clone').mouseleave(function(){
                $(this).animate({
                  scale : 1
                },300,function(){
                  $(this).remove()
                })
              })
            })
          })
	}

	initButton();
});

/* RESIZE */

$(window).bind('resize', function() {
	resizeRoutine();
});

function popupClose() {
	if (is_popup) {
		$('.popup').hide();
		$('.popup-bg').hide();
		if ($.browser.msie && $.browser.version < 7) $('select').show();
		var target = 'html';
		if ($.browser.safari) target = 'body';
		$(target).animate({scrollTop: /*$('.top-menu-content').offset().top*/0}, 1100);
		is_popup = false;
	}
}

function resizeRoutine() {
	$('.popup-bg').css({width: $('body').width(), height: ($('.container').height())});
	var l = parseInt( ( $('body').width() - $('.popup').width() ) / 2 );
	$('.popup').css({left: l+'px'});
}

function inputIn(obj, text) {
	if ($(obj).val() == text) {
		if (text != 'http://') $(obj).val('');
		$(obj).removeClass('empty');
		var but = '#'+$(obj).attr('id')+'-button';
		if ($(but).length > 0) $(but).attr('disabled', false);
   }
}
function inputUp(obj, text) {
	var but = '#'+$(obj).attr('id')+'-button';
	if ($(but).length > 0) $(but).attr('disabled', ($(obj).val() == '' || $(obj).val() == text));
}
function inputOut (obj, text) {
	if ($(obj).val() == '' || $(obj).val() == text) {
		$(obj).val(text).addClass('empty');
		var but = '#'+$(obj).attr('id')+'-button';
		if ($(but).length > 0) $(but).attr('disabled', true);
	}
}
function inputHelper(obj, text) {
	$(obj)
		.bind ('focus', function () {
			inputIn (this, text);
		})
		.bind ('keyup', function () {
			inputUp (this, text);
		})
		.bind ('blur', function () {
			inputOut (this, text);
		});
	inputOut(obj, text);
}



function initButton() {
	$('.button').hover(function() {
		$(this).addClass('button-hover');
	}, function() {
		$(this).removeClass('button-hover');
	});
	
	$('.button').mousedown(function() {
		$(this).addClass('button-active');
	});
	$('.button').mouseup(function() {
		$(this).removeClass('button-active');
	});
	
	$('.button').click(function() {
		if ($(this).find('input').length > 0) $(this).find('input').click();
	});
	
	$('.button input').click(function(e) {
		e.stopPropagation();
	});
}
