$(document).ready(function(){
	/*
	 *	submenu
	 */

	$('.submenu>li').hover(function(){
    	$(this).addClass('hover');
    },
    function(){
    	$(this).removeClass('hover');
    });

    $('dl.sitemap').hover(function(){
    	$(this).addClass('hover');
    },
    function(){
    	$(this).removeClass('hover');
    });

	/*
	 *	search
	 */

	$('form.search input.text').focus(function(){
		if (this.value=='Найти') this.value = '';
	});
	$('form.search input.text').blur(function(){
		if (this.value=='') this.value = 'Найти';
	});

	/*
	 *	divan
	 */

	if ($('div.divan').length){
		var dposition = 0;
		var step;
		var ram = $('div.dv-mini div.ram');
		var divan = $('div.divan');
		var scrolldivan = function(){
			var sw = document.getElementById('divan').scrollWidth-$(document.body).width();
			var ramwidth = ($(document.body).width()/document.getElementById('divan').scrollWidth*200)-2;
			ram.css({'width':ramwidth,'display':'block'});
			sw = document.getElementById('divan').scrollWidth-$(document.body).width();
			divan.animate({scrollLeft: parseInt(ram.css('left'))*sw/(200-ramwidth)}, 750,'linear',function(){
				step = parseInt((198-ramwidth)/2);
				var rleft = parseInt(ram.css('left'));
				$('div.title').css('display','none');
				if ((rleft>=0) && (rleft<step) || !rleft){
					$('div.tclassic').css('display','block');
					dposition = 0;
					$('#f2t1, #f3t2').hide();
					$('#f1t2, #f2t3').show();
				}else{
					if ((rleft>=step) && (rleft<(step*2))){
						$('div.tartdeco').css('display','block');
						dposition = 1;
						$('#f1t2, #f3t2').hide();
						$('#f2t1, #f2t3').show();
					}else{
						$('div.tmodern').css('display','block');
						dposition = 2;
						$('#f1t2, #f2t3').hide();
						$('#f2t1, #f3t2').show();
					}
				}
			});

		}
		scrolldivan();
		$(document).resize(scrolldivan);
		ram.draggable({
			'axis':'x',
			'containment':'parent',
			'stop': scrolldivan
		});

		$(document).keypress(function(e){
			if (e.ctrlKey && e.which==39){
				if (dposition<2 && dposition>=0){
					ram.css('left',parseInt((dposition+1)*step));
					scrolldivan();
				}
				return false;
			}
			if (e.ctrlKey && e.which==37){
				if (dposition<=2 && dposition>0){
					ram.css('left',parseInt((dposition-1)*step));
					scrolldivan();
				}
				return false;
			}
		});
		$('#f2t1, #f3t2').hide();
		$('#f1t2, #f2t3').show();
		$('div.bg .c1').click(function(){
			ram.css('left',0);
			scrolldivan();
			$('#f2t1, #f3t2').hide();
			$('#f1t2, #f2t3').show();
		});
		$('div.bg .c2').click(function(){
			ram.css('left',parseInt(step));
			scrolldivan();
			$('#f1t2, #f3t2').hide();
			$('#f2t1, #f2t3').show();
		});
		$('div.bg .c3').click(function(){
			ram.css('left',parseInt(2*step));
			scrolldivan();
			$('#f1t2, #f2t3').hide();
			$('#f2t1, #f3t2').show();
		});
		
		$('#f2t1 a').click(function(){
			ram.css('left',0);
			scrolldivan();
			$('#f2t1, #f3t2').hide();
			$('#f1t2, #f2t3').show();
			return false;
		});
		$('#f1t2 a, #f3t2 a').click(function(){
			ram.css('left',parseInt(step));
			scrolldivan();
			$('#f1t2, #f3t2').hide();
			$('#f2t1, #f2t3').show();
			return false;
		});
		$('#f2t3 a').click(function(){
			ram.css('left',parseInt(2*step));
			scrolldivan();
			$('#f1t2, #f2t3').hide();
			$('#f2t1, #f3t2').show();
			return false;
		});

		$(document).keydown(function(e){
			if (e.ctrlKey && e.which==39){
				if (dposition<2 && dposition>=0){
					ram.css('left',parseInt((dposition+1)*step));
					scrolldivan();
				}
				return false;
			}
			if (e.ctrlKey && e.which==37){
				if (dposition<=2 && dposition>0){
					ram.css('left',parseInt((dposition-1)*step));
					scrolldivan();
				}
				return false;
			}
		});
	}

	/*
	 *	cookie
	 */
	
	$('input.cookie').click(function(event){
		var id = this.value;
		if(this.checked == true) document.cookie = "id["+id+"] = 1; path=/"; else document.cookie = "id["+id+"] = 0; path=/";
	});

	/*
	 *	producers
	 */

	if ($('form.producers').length){
		var pr_option = $('#producers').get(0).options;
		var s = '<div class="wrap"><div class="corners-top"></div><div class="corners-bottom"></div><span>'+($('form.producers label').html())+'</span>';
		for (var i=1; i<=pr_option.length-1; i++){
			s+='<a href="'+pr_option[i].value+'">'+pr_option[i].innerHTML+'</a>';
		}
		s+='</div>';
		$('form.producers').html(s);

		$('form.producers div.wrap').click(function(event){
			$(this).addClass('active');
			$(document.body).click(function(){
				$('form.producers div.wrap').removeClass('active');
			});
			event.stopPropagation();
		});
	}

	/*
	 *	gallery
	 */

	$('div.gallery-image dl.images a').click(function(event){
		var gwrap = $('div.gallery-image div.wrap');
		gwrap.empty().addClass('loading');
		$('div.gallery-image dl.images a').removeClass('active');
		$(this).addClass('active');
		var gimg = $('<img src="'+this.href+'" alt="'+this.title+'" />');
		gimg.css({'opacity':0,'visibility':'hidden'});
		gwrap.append(gimg);
		gimg.load(function(){
			gwrap.removeClass('loading');
			gimg.css('visibility','visible');
			gimg.animate({'opacity': 1},500);
		});
		return false;
	});

	if ($('.toright a').length || $('.toleft a').length){
		$(document).keypress(function(e){
			if (e.ctrlKey && e.which==39 && $('.toright a').length){
				document.location = $('.toright a').get(0).href;
				return false;
			}
			if (e.ctrlKey && e.which==37 && $('.toleft a').length){
				document.location = $('.toleft a').get(0).href;
				return false;
			}
		});
		$(document).keydown(function(e){
			if (e.ctrlKey && e.which==39 && $('.toright a').length){
				document.location = $('.toleft a').get(0).href;
				return false;
			}
			if (e.ctrlKey && e.which==37 && $('.toleft a').length){
				document.location = $('.toleft a').get(0).href;
				return false;
			}
		});
	}
});

