$(document).ready(function() {
	if ($.browser.msie) {
		if ($.browser.version == "6.0") {
			var ie6Width = $(window).width();
			if (ie6Width > "1400") {
				$('#header, #menu_top, #icons_top, #footer_block').css('width', '1400px');
				$('#content').css('width', '1396px');
			}
			else if (ie6Width < "987") {
				$('#main, #header, #menu_top, #icons_top, #content, #footer_block').css('width', '987px');
			}
			else {
				$('#main, #header, #menu_top, #icons_top, #content, #footer_block').css('width', ie6Width+'px');
			}
			
			$(window).resize(function() {
				var ie6Width = $(window).width();
				if (ie6Width > "1400") {
					$('#header, #menu_top, #icons_top, #footer_block').css('width', '1400px');
					$('#content').css('width', '1396px');
				}
				else if (ie6Width < "987") {
					$('#main, #header, #menu_top, #icons_top, #content, #footer_block').css('width', '987px');
				}
				else {
					$('#main, #header, #menu_top, #icons_top, #content, #footer_block').css('width', ie6Width+'px');
				}
			});
		}
	}
	
	$('#lk_and_reg > span').bind('click', function() {
		$('#form_lk').show();
	})
	$('#lk_close').bind('click', function() {
		$('#form_lk').hide();
	})
	
	/*var windowWidth = $(document).width();
	if (windowWidth > "1400") {
		$('#lk_fon').css('right', '325px');
		$('#lk_close').css('right', '345px');
		$('#lk_title').css('right', '355px');
		$('#form_lk_send').css('right', '355px');
	}
	else {
		$('#lk_fon').css('right', '190px');
		$('#lk_close').css('right', '220px');
		$('#lk_title').css('right', '220px');
		$('#form_lk_send').css('right', '220px');
	}
	
	$(window).resize(function() {
		var windowWidth = $(document).width();
		if (windowWidth > "1400") {
			$('#lk_fon').css('right', '325px');
			$('#lk_close').css('right', '345px');
			$('#lk_title').css('right', '355px');
			$('#form_lk_send').css('right', '355px');
		}
		else {
			$('#lk_fon').css('right', '190px');
			$('#lk_close').css('right', '220px');
			$('#lk_title').css('right', '220px');
			$('#form_lk_send').css('right', '220px');
		}
	});*/
	
	/*POLE LOGIN*/
	var value_login = $('#login').val();
	$('#login').click(function() {
		if ($('#login').val() == value_login) {
			$('#login').attr('value', '');
		}
	});
	$('#login').blur(function() {
		if ($('#login').val() == "") {
			$('#login').attr('value', value_login);
		}
	});
	/*CLOSE POLE LOGIN*/
	
	/*POLE PASSWORD*/
	var value_pass = $('#pass').val();
	$('#pass').click(function() {
		if ($('#pass').val() == value_pass) {
			$('#pass').attr('value', '');
		}
	});
	$('#pass').blur(function() {
		if ($('#pass').val() == "") {
			$('#pass').attr('value', value_pass);
		}
	});
	/*CLOSE POLE PASSWORD*/
	
	/*POLE POISKA*/
	var value = $('#search_pole').val();
	$('#search_pole').click(function() {
		if ($('#search_pole').val() == value) {
			$('#search_pole').attr('value', '');
		}
	});
	$('#search_pole').blur(function() {
		if ($('#search_pole').val() == "") {
			$('#search_pole').attr('value', value);
		}
	});
	/*CLOSE POLE POISKA*/
	
	/*POLE MAIL*/
	var value_news_mail_pole = $('#news_mail_pole').val();
	$('#news_mail_pole').click(function() {
		if ($('#news_mail_pole').val() == value_news_mail_pole) {
			$('#news_mail_pole').attr('value', '');
		}
	});
	$('#news_mail_pole').blur(function() {
		if ($('#news_mail_pole').val() == "") {
			$('#news_mail_pole').attr('value', value_news_mail_pole);
		}
	});
	/*CLOSE POLE MAIL*/
	
	/*if ($.browser.webkit) {*/
	catBlockTable = 0;
	$('.catalogue_block').each(function() {
		$(this).css('width', '200px');
		$(this).children('.catalogue_div').children('.catalogue_img').children('img').css('max-width', '180px');
		//$(this).css('height', '300px');
	});	
	/*}
	else {
		catBlockTable = 0;
		$('.catalogue_block').each(function() {
			var widthTable = $(this).children('.catalogue_div').children('.catalogue_img').children('img').width();
			var heightTable = $(this).children('.catalogue_div').children('.catalogue_img').children('img').height();
			$(this).css('width', widthTable+'px');
			if (catBlockTable < heightTable) {
				catBlockTable = heightTable;
			}
		});
		$('.catalogue_block').css('height', catBlockTable+100+'px');
	}*/
	
	
	/*showCol = function() {
		$('.catalogue_cart').unbind('click', showCol);
		$('.catalogue_block').css('z-index', '0');
		$('.form_col').css('display', 'none');
		$(this).parent().parent().css('z-index', '100');
		$(this).next('.form_col').css('display', 'block');
		$(this).next('.form_col').children('div').children('.col_form_cart').children('.col_form_cart2').children('.input').focus();
		$(this).bind('click', hideCol);
		$(this).next('.form_col').children('div').children('.col_close').bind('click', hideCol2);
		$('.catalogue_cart').bind('click', showCol);
		$(this).next('.form_col').children('div').children('.col_form_cart').children('.col_form_cart2').children('.input').bind('keypress', enterPress);
	}
	
	hideCol = function() {
		$(this).unbind('click');
		$('.catalogue_block').css('z-index', '0');
		$(this).next('.form_col').css('display', 'none');
		$('.catalogue_cart').bind('click', showCol);
	}
	
	hideCol2 = function() {
		$(this).unbind('click');
		$('.catalogue_block').css('z-index', '0');
		$(this).parent('div').parent('.form_col').css('display', 'none');
		$('.catalogue_cart').bind('click', showCol);
	}
	
	enterPress = function(event) {
		if (event.keyCode == 13) {
			var id = $(this).next('.cart_hidden').attr('id');
			var name = $(this).next('.cart_hidden').attr('name');
			var price = $(this).next('.cart_hidden').attr('alt');
			var quantity = $(this).val();
			addItem2Cart(name, id, price, quantity); 
			return false;
		}
	}
	
	$('.catalogue_cart').bind('click', showCol);*/
	
	
	showCol = function() {
		$('.catalogue_cart span').unbind('click', showCol);
		$('.catalogue_block').css('z-index', '0');
		$('.form_col').css('display', 'none');
		$(this).parent().parent().parent().css('z-index', '100');
		$(this).parent().next('.form_col').css('display', 'block');
		$(this).parent().next('.form_col').children('div').children('.col_form_cart').children('.col_form_cart2').children('.input').focus();
		$(this).bind('click', hideCol);
		$(this).parent().next('.form_col').children('div').children('.col_close').bind('click', hideCol2);
		$('.catalogue_cart span').bind('click', showCol);
		$(this).parent().next('.form_col').children('div').children('.col_form_cart').children('.col_form_cart2').children('.input').bind('keypress', enterPress);
	}
	
	hideCol = function() {
		$(this).unbind('click');
		$('.catalogue_block').css('z-index', '0');
		$(this).next('.form_col').css('display', 'none');
		$('.catalogue_cart span').bind('click', showCol);
	}
	
	hideCol2 = function() {
		$(this).unbind('click');
		$('.catalogue_block').css('z-index', '0');
		$(this).parent('div').parent('.form_col').css('display', 'none');
		$('.catalogue_cart span').bind('click', showCol);
	}
	
	enterPress = function(event) {
		if (event.keyCode == 13) {
			var id = $(this).next('.cart_hidden').attr('id');
			var name = $(this).next('.cart_hidden').attr('name');
			var price = $(this).next('.cart_hidden').attr('alt');
			var quantity = $(this).val();
			addItem2Cart(name, id, price, quantity);
			changeInputInf(id);
			$('.pl_close').trigger('click');
			return false;
		}
	}
	
	$('.catalogue_cart span').bind('click', showCol);
	
	
	
	
	/*PLASHKA*/
	$('.pl_text_table tr:even').css('background-color', '#efefef');
	window.srcOld = 0;
	plashkaShow = function() {
		$('.plashka_on').unbind('click');
		var plashka = $(this).parent().parent().children('.plashka');
		window.src = $(this).parent().parent().children('.catalogue_img').children('.plashka_on');
		plashka.css('display', 'block');
		
		var widthPosition = $(window).width() / 2 - plashka.children('.pl_table').width() / 2,
			heightPosition = $(window).height() / 2 - plashka.children('.pl_table').height() / 2;
		
		plashka.css({'left' : widthPosition + 'px', 'top' : heightPosition + 'px'}).appendTo('body');
		$('body').children('.plashka').find('.input2').focus().bind('keypress', enterPress);
		$(this).bind('click', plashkaHide);
		plashka.find('.pl_close').bind('click', plashkaClose);
	}
	
	$(window).resize(function() {
		var widthPosition = $(window).width() / 2 - $('body').children('.plashka').children('.pl_table').width() / 2,
			heightPosition = $(window).height() / 2 - $('body').children('.plashka').children('.pl_table').height() / 2;
		
		$('body').children('.plashka').css({'left' : widthPosition + 'px', 'top' : heightPosition + 'px'});
	});
	
	plashkaHide = function() {
		$(this).unbind('click');
		$('body').children('.plashka').css('display', 'none').appendTo($(this).parent().parent());
		if (srcOld != 0) {
			$(this).parent().parent().children('.plashka').children().children().children().next().children().next().children().children('img').attr('src', srcOld);
			srcOld = 0;
		}
		$('.plashka_on').bind('click', plashkaShow);
	}
	
	plashkaClose = function() {
		$(this).unbind('click');
		if (srcOld != 0) {
			$(this).parent().prev().children('img').attr('src', srcOld);
			srcOld = 0;
			$(this).parent().prev().children('div').attr('class', 'pl_other_img');
			$(this).parent().prev().children('img').next().next().attr('class', 'pl_other_img_active');
			$('.pl_other_img').bind('click', imgOtherShow);
		}
		src.parent().parent().parent().parent().parent().parent().parent().parent().parent().parent().parent().children('.plashka').css('display', 'none');
		src.parent().parent().parent().parent().parent().parent().parent().parent().parent().parent().parent().children('.plashka').appendTo(src.parent().next().parent());
		$('.plashka_on').bind('click', plashkaShow);
	}
	$('.plashka_on').bind('click', plashkaShow);
	
	imgOtherShow = function() {
		$('.pl_other_img').unbind('click', imgOtherShow);
		if (srcOld == 0) {
			srcOld = $(this).parent().children('a').children('img').attr('src');
		}
		var src = $(this).children().attr('src');
		$('.pl_img').children('div').attr('class', 'pl_other_img');
		$(this).attr('class', 'pl_other_img_active');
		$(this).parent().children('img').attr('src', src);
		$('.pl_other_img').bind('click', imgOtherShow);
	}
	$('.pl_other_img').bind('click', imgOtherShow);
	
	/*MENU TOP OVER AND OUT*/
	menuTopOver = function() {
		$('.menu_top_over').unbind('mouseover');
		$(this).css({
			'background': 'url(/images/over.jpg) repeat-x',
			'height': '37px'
		});
		$('.menu_top_over').bind('mouseout', menuTopOut);
	}
	
	menuTopOut = function() {
		$('.menu_top_over').unbind('mouseout');
		$(this).css({
			'background': '',
			'height': '37px'
		});
		$('.menu_top_over').bind('mouseover', menuTopOver);
	}
	
	$('.menu_top_over').bind('mouseover', menuTopOver);
	
	
	
	
	
	fixImgIE6();
});


jQuery.preloadImages = function () {
    if (typeof arguments[arguments.length - 1] == 'function') {
        var callback = arguments[arguments.length - 1];
    } else {
        var callback = false;
    }
    if (typeof arguments[0] == 'object') {
        var images = arguments[0];
        var n = images.length;
    } else {
        var images = arguments;
        var n = images.length - 1;
    }
    var not_loaded = n;
    for (var i = 0; i < n; i++) {
        jQuery(new Image()).attr('src', images[i]).load(function() {
            if (--not_loaded < 1 && typeof callback == 'function') {
                callback();
            }
        });
    }
}

/* Фикс для картинок в ie6 (max-width, max-height) */
function fixImgIE6()
{
	if ($.browser.msie && $.browser.version <= '6.0')
	{
		var maxWidth,
			maxHeight,
			imgObject,
			imgString = '';
		
		//Собираем фотки через запятую в строку
		$('.catalogue_block img').each(function() {
			if ($(this).css('max-width'))
			{
				imgString += '"' + $(this).attr('src') + '",';
			}
		});
		
		imgString = '[' + imgString.substr(0, imgString.length - 1) + ']';
		$.preloadImages(eval(imgString), function() {
			$('.catalogue_block img').each(function() {
				if ($(this).css('max-width'))
				{
					maxWidth = parseInt($(this).css('max-width'));
					if ($(this).width() >= maxWidth)
						$(this).css('width', maxWidth + 'px');
				}
			});
		});
	}
}
