function ie6_png_fix() {
	function fix(parent, method) {
		var path = $(parent).css('background').replace('../', '');
		alert(path);
		$(parent).css('background', 'none');
		$(parent).css('filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src="http://www.new.techmp.ru/' + path + '", sizingMethod=' + method + ');');
	}

	function b_ins_fix(parent) {
		$(parent).each(function() {
			fix($('.tl, .tr, .bl, .br', parent), 'crop');
			fix($('.t, .b, .l, .r', parent), 'scale');
		});
	}

	b_ins_fix($('.b_ins_png_fix'));
}

function ie_float_bug() {
	$('#layer').width($(window).width());
	$('.float-fix').width($('.float-fix').parent().width() - 100);
}

function add2Fav(target) {
 if (document.all  && !window.opera) {
      if (typeof window.external == "object") {
         window.external.AddFavorite (document.location, document.title);
         return true;
       }
       else return false;

 } else {
	  target.href=document.location;
	  target.title=document.title;
	  target.rel = "sidebar";
	  return true;
	}
}

function search_box() {
	$('input.toogle-hint').each(function() {
		$(this).data('val', $(this).val());
		$(this).click(function() {
			if ($(this).val() == $(this).data('val'))
				$(this).val('');
		}).blur(function() {
			if ($(this).val() == '')
				$(this).val($(this).data('val'));
		});;
	});

	$('#search-box-home, #search-advance-box').each(function() {
		var parent = this;

		$('.example', parent).click(function() {
			$('.q input', parent).val($(this).text());
			return false;
		});

		$('select.root', parent).change(function() {
			$('.sub', parent).hide();
			$('#sub-catalogue-' + $(this).val()).show();
		});

		$('select').change(function() {
			$('#search-category-id').val($(this).val());
		});

		//if (! $('#search-category-id').val()) {
			$('#sub-catalogue-' + $('select.root:first', parent).val()).show();
		//} else {
			//$('select.root[value=' + $('#search-category-id').val() +']').show();
			//$('#sub-catalogue-' + $('#search-category-id').val() +']').show();
		//}

	});
}

function catalogue_column_size( ) {
	if ($('.sub-catalogue').size()) {
		if ($('body').width() < 1100) {
			$('.sub-catalogue').addClass('columns-2');
		} else
			$('.sub-catalogue').removeClass('columns-2');
	}
}

var is_print_mode = false;

$(function() {
	if ( jQuery.browser.msie && jQuery.browser.version == 6) {
		ie_float_bug();

		$(window).resize(function() {
			setTimeout(function() {
				ie_float_bug();
			}, 100);
		})
	}

	$('#to-advance-search').click(function() {
		$('#search-advance-box').toggle();
		$('#search-advance-box .close').click(function() {
			$('#search-advance-box').hide();
		});
		return false;
	});

	$('#add-to-fav').click(function () {
		return add2Fav(this);
	});

	$('#to-back-call, #back-call-popup .java').click(function() {
		$('#back-call-popup').toggle();
	});

	$('#to-print').click(function() {
		$.address.change(function (c) {
			if ($.address.path() == '/') {
				$('#print-css').remove();
				is_print_mode = false;
			}

		});
		$('head').append('<link id="print-css" rel="stylesheet" type="text/css" href="/css/card-print.css" />');
		is_print_mode = true;
	});

	$('#catalogue ul ul ul, #catalogue .sub-catalogue ul ul, #catalogue-left ul ul ul').each(function() {
		var ul = $(this);
		var target = $('.a:first', $(this).parent());

		if (! target.is('.acv')) {
			$(target).addClass('java').click(function() {
				if (ul.is(':hidden')) {
					ul.show();
					$('b', target).html('–');
				} else {
					ul.hide();
					$('b', target).html('+');
				}

				return false;
			});
		}

		if (! $('.acv', ul).size() && ! target.hasClass('acv')) {
			ul.hide()
			target.append('<b class="c">+</b>')
		} else {
			target.append('<b class="c">–</b>')
			target.removeClass('java').unbind('click');
		}
	});

	$('ul:first', $('.acv').parent()).show();

	$('#groups-filter').each(function() {
		var parent = this;
		$('.java', this).click(function() {
			if ($(this).hasClass('to-all')) {
				$('#catalogue .group').show();
			} else {
				$('#catalogue .group').hide();
				$('#catalogue .group-' + $(this).attr('rel')).show();
			}

			$('.acv', parent).removeClass('acv');
			$(this).addClass('acv');
		});
	});

	$(window).bind('resize', function() {
		catalogue_column_size();
	});

	catalogue_column_size();

	search_box();

	$('.text-block-catalogue ul').each(function() {
		$(this).html('<div class="wrapper">' + $(this).html() + '</div>');
		$(this).addClass('decor');
	});
	$('.text-block-catalogue .qu').each(function() {
		$(this).html('<div class="wrapper">' + $(this).html() + '</div>');
		$(this).addClass('decor');
	});

	$('.text-block').each(function() {
		$('.decor', this).append('<noindex><b class="d tl"></b><b class="d tr"></b><b class="d bl"></b><b class="d br"></b><ins class="d t"></ins><ins class="d b"></ins><ins class="d l"></ins><ins class="d r"></ins><div class="d m"><img disabled="" src="/img/text-b-decor-m.png"></div></noindex>');
		$('.qu', this).append('<noindex><b class="larr"></b><b class="rarr"></b>');
	});

	$('#home-swicher, .text-block .swicher').each(function() {
		var parent = this;
		$('.orange-pl span b', this).remove();
		$('.orange-pl span', this).addClass('java').append('<b class="l"></b><b class="r"></b>');
		$('.tools span:first', this).addClass('acv');
		$('.target:not(:first)', this).hide();
		$('.tools span', this).each(function (i) {
			$(this).attr('rel', i);
		});
		$('.tools span', this).click(function() {
			$('.tools .acv', parent).removeClass('acv');
			$(this).addClass('acv');
			$('.target', parent).hide();
			$('.target:eq(' + $(this).attr('rel') + ')').show();
		});
	});
});
