function tabsChange(){

	$('.tabs li').each(function(i,el){
		$(this).click(function(){
			$('.tabs li.active').removeClass('active');
			$(this).addClass('active');

			if (i == 0){
				$('.contents .content, .drawings').removeClass('show');
				$('.contents .content:first-child').addClass('show');
			}else if(i == 1){
				$('.contents .content:first-child').removeClass('show');
				$('.contents .content:last-child, .drawings').addClass('show');
			}
			return false;
		});
	});

}

function selectForCompare(){
	var items = [];

	function createItems(params){
		if (checkexist(params.id)) {
			var element = [
				'<li>',
				'	<div class="transfered">',
				'		<a href="javascript:;" rel="' + params.id + '" class="close">Close</a>',
				'		<a href="javascript:;">',
				'			<img src="' + params.img + '" alt="' + params.title + '" />',
				'		</a>',
				'		<div>',
				'			<h4>',
				'				<a href="javascript:;">' + params.title + '</a>',
				'			</h4>',
				'			<p>' + params.type + '</p>',
				'		</div>',
				'	</div>',
				'</li>'
			].join('');

			//var element = '<li><div class="transfered"><a rel="' + params.id + '" href="javascript:;" class="close">Close</a><a href="javascript:;"><img src="' + params.img + '" alt="" /></a><div><h4><a href="javascript:;">' + params.title + '</a></h4><p>' + params.type + '</p></div></div></li>'

			$(element).appendTo('ul.compare').mouseenter(function(){
				$(this).addClass('hover');
			}).mouseleave(function(){
				$(this).removeClass('hover');
			});

			$('div.compare input[id="' + params.id + '"]')
				.parents('li').effect('highlight', {color : "#eeeeee"}, 500)
				.effect("transfer",
					{to: $('a[rel="' + params.id + '"]').parents('li')},
					1000,
					function(){
						$('a[rel="' + params.id + '"]').parents('div').fadeIn();
					}
				);

			$('a[rel="' + params.id + '"]').click(function(){
				removeItems(this.rel);
				$('div.compare input[id="' + params.id + '"]').attr('checked', false);
				return false;
			});

			items.push(params.id);


			listNull(true);
		}
	}

	function removeItems(id){
		$('ul.compare li a[rel="'+id+'"]')
			.parents('li')
			.effect('highlight',
				{color : '#6ed4ff'},
				500)
			.fadeOut(500,
				function(){
					$(this).remove();
					for (var i=0; i<items.length; i++){
						if (id == items[i]){
							items.splice(i,1);
							if (items.length < 1) {
								listNull(false);
							}
						}
					}
				});
	}

	function checkexist(id){
		for (var i=0; i<items.length; i++){
			if (id == items[i]){
				return false;
			}
		}
		return true;
	}

	$('div.compare input').each(function(i,o){
		if(o.checked){
			createItems({
				id: o.id,
				img: $(o).parents('li').find('img').attr('src'),
				title: $(o).parents('li').find('h4').text(),
				type: $($(o).parents('.w34').find('h2').get(0)).text(),
				url: $(o).parents('li').find('a')[0].href
			});
		}
		if ($('ul.compare li').length < 1){
			listNull(false);
		}else{
			listNull(true);
		}
		$(o).click(function(){
			if(this.checked){

			if(items.length == 3) {
				removeItems(items[0]);
				$('div.compare input[id="' + items[0] + '"]').attr('checked', false);
			}

				createItems({
					id: this.id,
					img: $(this).parents('li').find('img').attr('src'),
					title: $(this).parents('li').find('h4').text(),
					type: $($(this).parents('.w34').find('h2').get(0)).text()
				});
			}else{
				removeItems(this.id);
			}
		});
	});

	function listNull(flag){
		if (!flag) {
			$('.tbuttons').hide();
			$('ul.compare').parent().find('p').show();
		}else {
			$('.tbuttons').show();
			$('ul.compare').parent().find('> p').hide();
		}
	}

	$(function(){

		$('.tbuttons .link').click(function(){
			$('ul.compare li a.close').each(function(i, o){
				removeItems($(o).attr('rel'));
				$('div.compare input[id="' + $(o).attr('rel') + '"]').attr('checked', false);
			});
			return false;
		});
	});
}

function carousel(params){
	var index = 0,
		next = params.btnNext,
		prev = params.btnPrev,
		slide = params.Carousel,
		element = params.carouselItems,
		visible = params.visibleItems ? params.visibleItems : 1;
		width = element[0].offsetWidth;

	if (slide.find('li').length-visible < 1){
		next.css({visibility: 'hidden'});
		prev.css({visibility: 'hidden'});
	}

	next.bind('click', function(e){
		if (index < slide.find('li').length-visible){
			index++;
			move();
		}
		return false;
	});

	prev.bind('click', function(e){
		if (index > 0){
			index--;
			move();
		}
		return false;
	});

	function move(){
		//new stop().effect.Move( slide , { x: -(width*index), y: 0, mode: 'absolute' });
		slide.animate({left: -(width*index)+'px'}, 300);
	}
}

function gallery(){
	var images = $('.carousel_wrapper ul li a'),
		view = $('.text img');

	images.click(function(){
		var link = $(this).attr('href');
		$(this).find('img').stop().effect("transfer",
					{to: view},
					700,
					function(){
						view.attr('src', link);
					});
		return false;
	});
}

function inputHideValue(sName){
	$(sName).each(function(i, el){
		var v = el.value;
		$(el).bind('focus', function(){
			this.value = (this.value == v) ? '' : this.value;
		});
		$(el).bind('blur', function(){
			this.value = (this.value == '') ? v : this.value;
		});

	});
}

function faqToggle() {
	$('.faq li a[class!="close"]').each(function() {
		$(this).click(function() {
			if ($(this).parent().hasClass('close')) {
				$(this).next().stop(true, true).slideDown(300, function() {
					if ($.browser.msie) {
						this.style.overflow = 'hidden';
					}
				});
				$(this).parent().removeClass('close').addClass('open');

			} else {
				$(this).next().stop(true,true).slideUp(300);
				$(this).parent().removeClass('open').addClass('close');
			}

			return false;
		});
	});

	$('.faq li a.close').click(function() {
		var element = this;

		$(element).parent().fadeOut(300, function() {
			$(element).parents('li:first').removeClass('open').addClass('close');
		});

		return false;
	});
}

var lang_changed = false;
$(function(){

	// Cufon
	Cufon.replace('.navigation > ul > li > a, .prise_holder p, .article_preview .administration a, h2', {fontFamily: 'CorpidCdOTB',hover:true});
	Cufon.replace('h3, .corpid_light', {fontFamily: 'CorpidCdOTB Light'});
	Cufon.replace('.files h3, .gallery h3, .drawings h3,.products_menu h4, .corpid', {fontFamily: 'CorpidCdOTB', hover:'true'});
	Cufon.replace('.price, .dincyr', {fontFamily: 'DINCyr-Black', textShadow: '#a52840 0 -1px 0'});
	// end Cufon

	Cufon.now();	// ouch!

	$('.compare li').mouseenter(function(){
		$(this).addClass('hover');
	});

	$('.compare li').mouseleave(function(){
		$(this).removeClass('hover');
	});


	$('.navigation > ul > li').bind('mouseenter mouseleave', function(e){
		lang_changed = lang_changed ? true : false ;

		if(!lang_changed)
		{
			handleMenu(this, e);
		}
	});

	tabsChange();
	selectForCompare();
	inputHideValue('.hideValue');

	// table odd/even classes
	$('table').each(function() {
		if ($('tr', this).length > 1) {
			if (!$('tr.odd', this).length) {
				$('tr:even', this).addClass('odd');	// jquery sucks!
				$(this).wrap('<div class="general_info p0i">');
			}
		}
	});

	// stupid stuff
	$('.products_category .w14 h2.with_line:first').height($('.products_category .w34 h2.with_line:first').height());
});


function handleMenu(item, event) {
	if (event.type == 'mouseleave') {
		$(item).find('a').removeClass('active');
		Cufon.replace('.navigation > ul > li > a', {fontFamily: 'CorpidCdOTB', hover: 'true'});
	}

	$('div.products_menu:visible').hide();

	if (event.type == 'mouseenter') {
		$(item).find('a').addClass('active');

		if ($.browser.msie) {
			var left_bg = $(item).find('.product_menu_inner_left').css('background-image', 'none');
			var right_bg = $(item).find('.product_menu_inner_right').css('background-image', 'none');

			//$(item).find('div.products_menu').slideDown(
			$(item).find('div.products_menu').fadeIn(400, function() {
				left_bg.removeAttr('style');
				right_bg.removeAttr('style');
			});
		} else {
			//$(item).find('div.products_menu').slideDown();
			$(item).find('div.products_menu').fadeIn(400);
		}
	}
}

