var map;

function scrollit(){ 
	parent.scroll(0,0)
} 

$(function(){	
	/*
	 * main ie
	 */

	if ($.browser.msie){
		/*$('div.g-main').find('.w1 img, .w2 img, .w4 img, .w3 img, .w2r img').each(function(){*/
		$('div.g-main').find('.w1 img, .w2 img, .w4 img, .w3 img').each(function(){
			var sr = $(this).attr('src');
			/*$(this).attr('src','/assets/templates/nfs/i/blank.png');*/
			$(this).css({
				/*'filter': 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="' + sr + '",sizingMethod="scale")',*/
				'height': $(this).parent().parent().height()+'px',
				'width': $(this).parent().parent().width()+'px',
				'display': 'block'
			});
		});
		$(window).resize(function(){
			$('div.g-main').find('.w1 img, .w2 img, .w4 img, .w3 img, .w2r img').each(function(){
				$(this).css({
					'height': $(this).parent().parent().height()+'px',
					'width': $(this).parent().parent().width()+'px'
				});
			});
		});
	}
	if ($.browser.opera && $('div.g-main').length) {
		function omain(){
			var gmw = $('div.g-main').get(0).offsetWidth;
			var gmh = 0.642 * gmw;
			
			$('div.g-main div.w2').css({
				'width': 0.286 * gmw + 'px',
				'height': 0.667 * gmh + 'px'
			});
			$('div.g-main div.w2r').css({
				'width': 0.286 * gmw + 'px',
				'height': 0.222 * gmh + 'px'
			});
			$('div.g-main div.w2a').css({
				'width': 0.326 * gmw + 'px',
				'height': 0.222 * gmh + 'px'
			});
			$('div.g-main div.w3').css({
				'width': 0.429 * gmw + 'px',
				'height': gmh + 'px'
			});
			$('div.g-main div.w4').css({
				'width': 0.143 * gmw + 'px',
				'height': 0.334 * gmh + 'px'
			});
		}
		omain();
		$(window).resize(omain);
	}

	/*
	 * cat
	 */
	var cat_time = 150;

	$('div.content div.cat img').hover(function(){
		$(this).animate({
			height: 100,
			width: 66
		},cat_time);
	}, function(){
		$(this).animate({
			height: 85,
			width: 56
		},cat_time);
	});
	
	if ($('div.content div.cat img').length > 4){
		var snav = $('div.content div.tc div.nav-st');
		
		//	st nav hover

		$('#hrcat').hover(function(){
			$('#strlcat').css('background-position','-17px 0');
		},function(){
			$('#strlcat').css('background-position','0 0');
		});
		
		$('#hlcat').hover(function(){
			$('#strlcat').css('background-position', '-34px 0');
		},function(){
			$('#strlcat').css('background-position','0 0');
		});

		//	st nav click
		
		$('#hrcat').click(function(){
			$('div.content div.cat').animate({
				scrollLeft: '+=64px'
			},200);
			return false;
		});

		$('#hlcat').click(function(){
			$('div.content div.cat').animate({
				scrollLeft: '-=64px'
			},200);
			return false;
		});
	}

	/*
	 * main
	 */

	if (!$.browser.msie){
		$('div.g-main div.w4 a').hover(function(){
			$(this).find('img').animate({'opacity':0.8},150);
		},function(){
			$(this).find('img').animate({'opacity':1},150);
		});
	}
	
	/*
	 * catalog-search
	 */
	if ($('div.catalog-search').length) {

		var t_hover = 100;

		$('div.catalog-search a.j img').mouseenter(function(){
			
			var rrRel = $(this).attr('rel');
			var rrSrc = $(this).attr('src');
	
			//jQuery.preLoadImages(rrRel);
			
			var ul = $(this).parents('ul');
			var li = $(this).parents('li');
	
			var t = $(this).parents('ul').find('li').index(li);
			var l = $(this).parents('div.list').find('ul').index(ul);
	
			li.css({
				'z-index': 100,
				'overflow': 'visible'
			});
			ul.css({'z-index': 100});
	
			$(this).stop().animate({
				'width': 110,
				'height': 166,
				'left': -5,
				'top': -8
			},t_hover);
			
			$(this).attr('src',rrRel);
			$(this).attr('rel',rrSrc);
			
		}).mouseleave(function(){
			
			var rrRel = $(this).attr('rel');
			var rrSrc = $(this).attr('src');
			
			$(this).parents('li').css({'z-index': 0});
			$(this).parents('ul').css({'z-index': 0});
			$(this).animate({
				'width': 100,
				'height': 151,
				'left': 0,
				'top': 0
			},t_hover,'',function(){
				$(this).parents('li').css({'overflow': 'hidden'});
			});
			
			$(this).attr('src',rrRel);
			$(this).attr('rel',rrSrc);
			
		});
	}

	/*
	 *	catalog
	 */
	
	if ($('div.catalog').length) {
		
		//	position
		
		$('div.catalog div.list>ul').each(function(i){
			$(this).css({
				'position': 'absolute',
				'left': i*100 
			});
		});
	
		//	hover
		
		var t_hover = 100;
		var s_column = 3;
		var total_column = $('div.catalog div.list>ul').length;
		if (s_column > total_column){
			s_column = total_column;
		} 

		$('div.catalog a.j img').mouseenter(function(){
			var ul = $(this).parents('ul');
			var li = $(this).parents('li');
	
	
			var rrRel = $(this).attr('rel');
			var rrSrc = $(this).attr('src');
	
			//jQuery.preLoadImages(rrRel);
	
			var t = $(this).parents('ul').find('li').index(li);
			var l = $(this).parents('div.list').find('ul').index(ul);
	
			li.css({
				'z-index': 100,
				'overflow': 'visible'
			});
			ul.css({'z-index': 100});

			$(this).stop().animate({
				'width': 110,
				'height': 166,
				'left': l == 0 ? 0 : (l == (s_column-1)? -10: -5),
				'top': t == 0 ? 0 : (t == 2? -16 : -8)
			},t_hover);
			
			$(this).attr('src',rrRel);
			$(this).attr('rel',rrSrc);
			
		}).mouseleave(function(){
		
			var rrRel = $(this).attr('rel');
			var rrSrc = $(this).attr('src');
			
			//jQuery.preLoadImages(rrRel);
		
			$(this).parents('li').css({'z-index': 0});
			$(this).parents('ul').css({'z-index': 0});
			$(this).animate({
				'width': 100,
				'height': 151,
				'left': 0,
				'top': 0
			},t_hover,'',function(){
				$(this).parents('li').css({'overflow': 'hidden'});
			});
			
			$(this).attr('src',rrRel);
			$(this).attr('rel',rrSrc);
			
		});

		//	click

		var t_image = 200;

		$('div.catalog a.j').click(function(){
			var c_img = $('<img />');
			var cv = $('div.catalog div.cat-view');
			cv.find('div.h-image').remove();
			cv.find('img').stop(true, true).fadeOut(t_image);
			cv.addClass('loading');

			var cd = $('div.catalog div.desc');
			cd.find('div.h-desc');
			cd.stop(true,true).fadeOut(t_image);
			var ncd = $(this).parent().find('div.h-desc');
			var hi = $(this).parent().find('div.h-image');

			c_img.load(function(){
				cv.removeClass('loading');
				cv.find('img').attr('src',this.src)
				.stop(true, true).fadeIn(t_image,function(){
					hi.clone().appendTo(cv);
				});
				cd.find('div.h-desc').html(ncd.html());
				cd.stop(true, true).fadeIn(t_image);
			});
			c_img.attr('src',$(this).attr('href'));
			return false;
		});

		//	st nav

		var t_nav = 300;

		function st_active(){
			$('#hr, #hl').removeClass('disabled');
			if (s_column >= total_column){
				$('#hr').addClass('disabled');
			}
			if (s_column <= 3){
				$('#hl').addClass('disabled');
			}
		}

		function h_scroll(){
			var list = $('div.catalog div.list');
			st_active();

			list.animate({
				'width': s_column * 100
			},t_nav);

			var lf = ((s_column - 3) * 275) / (total_column - 3);
			$('div.catalog div.scroll span').animate({'left': lf}, t_nav); 

			return false;
		}

		//	st nav hover

		$('#hr').hover(function(){
			if (!$(this).hasClass('disabled')){
				$('#strl').css('background-position','-21px 0');
			}
		},function(){
			$('#strl').css('background-position','');
		});
		
		$('#hl').hover(function(){
			if (!$(this).hasClass('disabled')) {
				$('#strl').css('background-position', '-42px 0');
			}
		},function(){
			$('#strl').css('background-position','');
		});

	
		if (total_column > 3){

		//	st nav click
		
		$('#hr').click(function(){
			s_column ++;
			if (s_column >= total_column){
				s_column = total_column;
			}
			return h_scroll();
		});

		$('#hl').click(function(){
			s_column --;
			if (s_column <= 3){
				s_column = 3;
			}
			return h_scroll();
		});
		
		//	scroll


			var max_sw = total_column * 100;
			var min_sw = 300;

			$('div.catalog div.scroll').show();
			$('div.catalog div.scroll span').draggable({
				containment: 'parent',
				axis: 'x',
				drag: function(event, ui) {
					var w = (max_sw - min_sw) * ui.position.left / 275 + min_sw;
					$('div.catalog div.list').css({
						'width': w
					});
					s_column = parseInt(w / 100);
				},
				stop: st_active
			});
		}
	}
	
	/*
	 *	end catalog
	 *	
	 *	win
	 */
	
	$('div.win a.tg-feedback').live('click',function(){
		$(this).parents('div.form').find('form.feedback').slideToggle(500);
		return false;
	});

	$('div.win a.hide-form').live('click',function(){
		$(this).parents('div.form').find('form.feedback').slideUp(500);
		return false;
	});

	$('div.win .close').live('click',function(){
		$(this).parents('div.win').hide();
		if ($(this).parents('div.win').hasClass('rrrrr-win')) $(this).parents('div.win').remove();
		$('div.win .feedback').hide();
		return false;
	});
	
	
	/*
	 * gallery
	 */
	
	if ($('a[rel=gallery]').length){
		var gallery_total = $('a[rel=gallery]').length;
		$('a[rel=gallery]').click(function(){
			var ii = $('a[rel=gallery]').index(this);
			var gallery_container = $('\
			<div class="win rrrrr-win">\
				<div class="bg"> </div>\
				<div class="form form-gallery">\
					<div class="form-bg"> </div>\
					<div class="form-wrapper">\
						<span class="close"> </span>\
						<div class="form-container">\
							<div class="gnav"></div>\
						</div>\
					</div>\
				</div>\
			</div>');
			gallery_container.appendTo('body');
			gallery_container.show();
			$('div.win').show();
			
			
			var fg = gallery_container.find('div.form-gallery');
			var fc = fg.find('div.form-container');
			
			var gallery_time = 300;
			
			$('a[rel=gallery]').each(function(){
				$('<a href="'+this.href+'"></a>').click(function(){
					fg.addClass('loading');
					
					fg.find('div.gnav a').removeClass('active');
					$(this).addClass('active');
					var im = $('<img />');
					im.load(function(){
						fg.removeClass('loading');
						var w = this.width;
						var h = this.height;
						var st = $('body').scrollTop()>0 ? $('body').scrollTop() : $('html').scrollTop();
						var dh = document.documentElement.clientHeight;
						fc.find('img').remove();
		
						fg.animate({
							width: w+30,
							marginLeft: -(w/2+15),
							top: st+(dh/2)-h/2-30
						},gallery_time);
						fc.animate({
							height: h
						},gallery_time,'',function(){
							fc.find('img').remove();
							im.appendTo(fc);
						});
					});
					im.attr('src',this.href);
					return false;
				}).appendTo(fg.find('div.gnav'));
			});
			
			fg.find('div.gnav a:eq('+ii+')').click();
			return false;
		});
	}
	
	//	map
	if (typeof(YMaps) != "undefined") {
		var s = new YMaps.Style();
		s.iconStyle = new YMaps.IconStyle(new YMaps.Template("<div class=\"g-point-name\"></div>"));
		s.iconStyle.offset = new YMaps.Point(-37, 0);
		s.iconStyle.size = new YMaps.Point(37, 40);
		YMaps.Styles.add("g#point", s);
	}
	
	$('.hidemenucat li ul li.active').parent().parent().parent().show();
	$('.hidemenucat li.active').each(function(){
		if ($(this).hasClass('slevelcat')){
			$(this).parent().show();
		}
	});

	/*$('.first-main-level').click(function(){
		$('.hidemenucat').slideUp();
		$(this).next().stop().slideDown();
	});*/

	/* 
	 $('.i-list-rec li a').click(function(){
		if (!$(this).parent().parent().hasClass('active')){
			$('.i-list-rec li.active').each(function(){
				
				$(this).animate({width: '30%',paddingTop: '30%'}).removeClass('active');
				
				var imgIn = $('img',this).attr('src');
				$('img',this).unbind('load').attr('src',$('a',this).attr('title'));
				$('a',this).attr('title',imgIn);
			});
			
			var img = $('img',this).attr('src');
			$(this).parent().addClass('i-loading');
			$('img',this)
				.hide()
				.load(function(){
					$(this).show().parent().parent().parent().parent()
						.animate({width: '60%',paddingTop: '60%'},600).addClass('active');
					$(this).parents('ul.i-list-rec li').removeClass('i-loading');
				})
				.attr('src',$(this).attr('title')).parent().parent()
				.attr('title',img).find('img')
			
		}
		return false;
	});
	*/
	
	$('div.content').resize(function(){
		$('div.footer').get(0).innerHTML += '';
	});
	
});

$(document).ready(function(){
	$('a').click(function(){
		
		var str = $(this).attr('href');
		if(str.indexOf('http') + 1){
			
			if((str.indexOf('newformstil.ru') == -1) && (str.indexOf('www.newformstil.ru') == -1)){
				//alert(str.indexOf('newformstil.ru'));
				window.open(str);
				return false;
			}
		}
	});
});

