function popup_init() {
	popup_init.popPars = { arrowKey:false, rel:'nofollow' };
	$(document).bind('cbox_complete', function(){
		$('#cboxLoadedContent a[rel=popup]').colorbox(popup_init.popPars);
		$('#cboxLoadedContent form').smoothForm({
			success: function(html){
				var par = {};
				for(var i in popup_init.popPars)
					par[i] = popup_init.popPars[i];
				par.html = html;
				$.colorbox(par);
			}
		});
		$('#cboxLoadedContent input:first').focus();
	});
	$('a[rel=popup]').colorbox(popup_init.popPars);

	$('a[rel=ipopup]').click(function(){
		var par = {};
		for(var i in popup_init.popPars)
			par[i] = popup_init.popPars[i];
		par.html = '<div style="width:800px; height:500px"><br><iframe src="' + this.href + '" frameborder="0" border="0" style="width:100%; height:100%"></iframe></div>';
		$.colorbox(par);
		return false;
	});
}

function gallery_init() {
	gallery_init.cb = function(c){
		if(!c) c = this;
		var t = $('._th', c);
		if(t.scrollLeft() > 0)
			$('._l', c).removeClass('_off');
		else
			$('._l', c).addClass('_off');
		if(t.scrollLeft() < $('table', c).outerWidth() - t.width())
			$('._r', c).removeClass('_off');
		else
			$('._r', c).addClass('_off');
	};
	$('._csld ._l').click(function(){
		var c = $(this).parent();
		var t = $('._th', c);
		var w = $('td', c).outerWidth();
		var s = (Math.round(t.scrollLeft() / w) - 3) * w;
		t.stop(true, true).animate({ scrollLeft:s }, 'normal', function(){ gallery_init.cb(c); });
	});
	$('._csld ._r').click(function(){
		var c = $(this).parent();
		var t = $('._th', c);
		var w = $('td', c).outerWidth();
		var s = (Math.round(t.scrollLeft() / w) + 3) * w;
		t.stop(true, true).animate({ scrollLeft:s }, 'normal', function(){ gallery_init.cb(c); });
	});
	$('._csld').each(function(){
		var r = $(this).closest('tr');
		r.parent().prepend(r);
	});
	$('._csld td > div').mouseover(function(){
		$('._ov', this).fadeTo('normal', 0.9);
	}).mouseleave(function(){
		$('._ov', this).fadeOut();
	});

	$('._csld').each(gallery_init.cb);

	var box = $('._csli');
	if(box.length) {
		var load = box.html();
		box.empty();
		$('._csld td > div').click(function(){
			var w = box.width();
			var h = box.height();
			var img = new Image();
			var url = $('img', this).attr('src').replace(/_thumb/, '');
			var ovr = $(load).hide().appendTo(box).fadeIn('normal', function(){
				$(img).load(function(){
					var r = Math.min(img.width / w, img.height / h)
					img.width /= r;
					img.height /= r;
					$(img).hide().appendTo(box).css({
						position: 'absolute',
						left: parseInt((w - img.width) / 2) + 'px',
						top: parseInt((h - img.height) / 2) + 'px'
					});
					ovr.fadeOut('normal', function(){ $(this).remove(); });
					$(img).fadeIn('normal', function(){
						$('img:not(:last)', box).remove();
					});
				});
				img.src = url;
			});
		}).eq(0).click();
	}
}

function catalog_init() {
	$('.catalog ._over').mouseover(function(e){
		var tr = $(this).closest('tr');
		var p = $('.catalog_popup', tr);
		var rp = $(tr).position();
		var cp = $(this).position();
		p.css({ top: rp.top - 76 + $(this).height() / 2 + 'px', left: cp.left - 178 + 'px' }).stop(true, true).fadeIn();
		$(this).data('p', p);
	}).closest('td').mouseleave(function(){
		$('.catalog_popup', $(this).closest('tr')).stop(true, true).fadeOut();
	});

	$('.csl_toggle').click(function(){
		var c = $('._csld', $(this).closest('table'));
		c.slideToggle();
		$(window).resize();
		setTimeout(function(){ gallery_init.cb(c); }, 100);
		return false;
	});

	gallery_init();
}

function catalog_tooltip_init() {
	var ttf = function(){
		var tt = $(this).data('tt');
		if(tt) return;

		var p = $(this).position();
		tt = $('<div>'+$(this).data('text')+'</div>');
		$(this).data('tt', tt);
		tt.css({
			position: 'absolute',
			left: p.left - 2 + 'px',
			top: p.top - 1 + 'px',
			zIndex: '1',
			background: '#fff',
			border: '1px solid #444',
			padding: '2px 4px',
			minWidth: $(this).width() + 'px',
			minHeight: $(this).height() + 'px',
			lineHeight: $(this).height() + 'px',
			fontSize: '12px'
		}).hide().appendTo($(this));
		window.setTimeout(function(){ tt.fadeIn(); }, 100);
	};
	$('.catalog td._l').each(function(){
		if($(this).text().length > 8) {
			var el = $('._over', this).length > 0 ? $('._over', this) : $(this);
			$(this).data('text', el.text());
			el.html(el.text().substring(0, 6) + '&hellip;');
			$(this).mouseover(ttf);
		}
	}).mouseleave(function(){
		var tt = $(this).data('tt');
		$(this).data('tt', null);
		if(tt)
			tt.remove();//stop(true, true).fadeOut('normal', function(){ tt.remove(); });
	});

	$('table.catalog').show();
	$(window).resize();
}

function dealers_init() {
	var com = $('._dealers ._combo');
	com.hide();
	com.eq(0).show('slide', {direction:'right'}, 1000);
	$('select', com.eq(0)).change(function(){
		if($(this).val() == 223)
			com.eq(1).filter(':hidden').show('slide', {direction:'right'}, 1000);
		else
			com.eq(1).filter(':visible').hide('slide', {direction:'right'}, 1000);
	}).change();
	$('select', com).change(function(){
		$(this.form).submit();
	});
}

$(function(){
	$('.pngfix').ifixpng();
	$('.pngfix_str').ifixpng();
	
	$('#menu a').mouseover(function(){
		var i = $('#menu a').index(this);
		clearTimeout($(this).data('hh'));
		var me = $('#header .menuinfo:eq('+i+')');
		var a = this;
		var t = setTimeout(function(){
			me.stop(true, true).fadeIn();
			var o = $(a).offset().left - me.offset().left + ($(a).width() - 10 - $('._arrow', me).width()) / 2;
			$('._arrow', me).css('margin-left', o+'px');
		}, 250);
		$(this).data('t', t);
	}).mouseout(function(){
		var i = $('#menu a').index(this);
		var ln = this;
		var me = $('#header .menuinfo:eq('+i+')');
		clearTimeout($(this).data('t'));
		var hh = setTimeout(function(){ $(me).stop(true, true).fadeOut(); }, 100);
		$(ln).data('hh', hh);
	});
	
	$('#header .menuinfo').mouseover(function(){
		var i = $('#header .menuinfo').index(this);
		var ln = $('#menu a:eq('+i+')');
		clearTimeout($(ln).data('hh'));
	}).mouseout(function(){
		var i = $('#header .menuinfo').index(this);
		var ln = $('#menu a:eq('+i+')');
		var me = this;
		var hh = setTimeout(function(){ $(me).stop(true, true).fadeOut(); }, 100);
		$(ln).data('hh', hh);
	});
	
	$(window).resize(function(){
		$('.column_main .column_left').css('height', 'auto');
		$('.column_main').css('height', 'auto').each(function(){
			var h1 = $(this).height();
			var h2 = $('#container').height() - $('#header').height() - $('#footer').height();
			if(h2 > h1)
				$(this).height(h2);
		});
		$('.column_main .column_left').each(function(){
			$(this).height($(this).parent().height());
		});
	}).resize();

	$('.news_box').click(function(){
		window.location = $('a', this).attr('href');
		return false;
	});

	$('.box').each(function(){
		var box = this;
		$('._btn a', box).click(function(){
			var all = $('._btn a', box);
			var cur = Math.max(0, all.index(all.filter('._sel')));
			var i = all.index(this);
			all.filter('._sel').removeClass('_sel');
			$(this).addClass('_sel');
			$('._tab:eq('+cur+')', box).stop(true, true).fadeOut('normal', function(){
				$('._tab:eq('+i+')', box).stop(true, true).fadeIn();
			});
			return false;
		}).eq(0).click();
	});

	$('#footer form[name=newsletter]').submit(function(){
		var em = $(':input[name=newsletters_email]').val();
		var par = popup_init.popPars;
		par.href = $(this).attr('action');
		par.onComplete = function(){
			$('#colorbox input[name=newsletters_email]').val(em);
		};
		$.colorbox(par);
		return false;
	});

	popup_init();
	
	dealers_init();
});

