var slideshow = {};

slideshow.init = function() {
	n = 0;
	function change() {
		setTimeout(function(){
			n++;
			if (n >= slideshow.pics.length) n = 0;
			$('#slideshow_pic img')
				.before('<img src="' + slideshow.pics[n] + '" alt="" />')
				.css('opacity', 1)
				.fadeTo(1000, 0, function(){
					$(this).remove();
					$('#slideshow_link').attr('href', slideshow.links[n]);
					change();
				});
		},3000);
	}
	change();
};

var carousel = {};

carousel.init = function() {

	function createCarousel() {
		n = Math.floor(block.width() / li.outerWidth(true));
		li.width((block.width() / n) - parseInt(li.css('padding-right')));

		block.jCarouselLite({
			btnNext: '.models_carousel .next',
			btnPrev: '.models_carousel .prev',
			speed: 300,
			visible: n,
			scroll: 1
		});
	}

	block = $('#models_thumbs');
	li = block.find('li');
	createCarousel();

};

$(function(){

	// Для баннера
	
	/*
	if($('#banner_right ul li').length > 1) {

		var start = $.cookie('banner');

		if (parseInt(start) < $('#banner_right ul li').length - 1) {
			start++;
		} else {
			start = 0;
		}

		$.cookie('banner', start);

		$('#banner_right').jCarouselLite({
			btnNext: '.banner_right .next',
			btnPrev: '.banner_right .prev',
			speed: 700,
			visible: 1,
			scroll: 1,
			start: start
		});

	} else {

		$('#banner_right').jCarouselLite({
			btnNext: '.banner_right .next',
			btnPrev: '.banner_right .prev',
			speed: 700,
			visible: 1,
			scroll: 1
		});

	}
*/

(function($){
 
    $.fn.shuffle = function() {
 
        var allElems = this.get(),
            getRandom = function(max) {
                return Math.floor(Math.random() * max);
            },
            shuffled = $.map(allElems, function(){
                var random = getRandom(allElems.length),
                    randEl = $(allElems[random]).clone(true)[0];
                allElems.splice(random, 1);
                return randEl;
           });
 
        this.each(function(i){
            $(this).replaceWith($(shuffled[i]));
        });
 
        return $(shuffled);
		
	
 
    };	
	
})(jQuery);
// Рандомно банера выводятся
$(function() {

    $("#for_carous li").shuffle();
	$("#for_carous2 li").shuffle();
		

first_rand = $('#for_carous  li object param').filter(':first').attr('value');		
second_rand = $('#for_carous2  li object param').filter(':first').attr('value');		
				
 if(first_rand == second_rand) {
 
 	$('#for_carous  li').hide().filter(':first').show();
	$('#for_carous2  li').hide().filter(':last').show();
	//Банера одинаковые и мы меняем их
}		
else
		{
	$('#for_carous  li').hide().filter(':first').show();
	$('#for_carous2  li').hide().filter(':first').show();
		//Банера разные, все хорошо
}
		

});

	// Для обзора и дизайна
	flag = true;
//	$('div.review dl, div.design dl')
//		.hover(
//			function(){
//				if (flag == true) {
//					flag = false;
//					$(this).children('dd').slideDown('fast', function(){
//						flag = true;
//					});
//				}
//			},
//			function(){
//				if (flag == true) {
//					flag = false;
//					$(this).children('dd').slideUp('fast', function(){
//						flag = true;
//					});
//				}
//			}
//		)
//		.mousemove(function(){
//			t = $(this);
//			if (flag == true) {
//				flag = false;
//				t.parent().find('dd').not(t.children('dd')).slideUp('fast', function(){
//					flag = true;
//				});
//				t.children('dd').slideDown('fast', function(){
//					flag = true;
//				});
//			}
//		});

	// Для дизайна
	$('div.design').find('dt a, a.more').click(function(){
		t = $(this);
		short = $('div.design div.short div:first');
		full = $('div.design div.full');

		flag = false;
		short.animate({left: -short.width()}, 'fast', function(){
			full
				.children('div').html(t.closest('dl').find('div.forfull').html())
				.end().css({display: 'block', bottom: -full.height()})
				.animate({bottom: 0}, 'fast', function(){
					full.children('a.close').click(closeFull);
					flag = true;
				});
		});
		return false;
	});

$('a.more').click(
function(){
$('#animate_top').animate({marginTop:"-150px"}, 200);
});
	
$('a.close').click(
function(){
$('#animate_top').animate({marginTop:"0px"}, 200);
});
	
});

function colorSelect(handler) {
	handler = $(handler);

	handler
		.addClass('act')
		.siblings().removeClass('act');

	title = handler.attr('title');
	num = handler.parent().find('li').index(handler);

	div = handler.closest('div.color_select');

	div.find('div.color span').html(title);
	div.find('div.img img').eq(num)
		.addClass('act')
		.siblings().removeClass('act');
}

function closeFull() {
	flag = false;
	short.find('dd').hide();
	$(this).parent().animate({bottom: -full.height()}, 'fast', function(){
		short.animate({left: 0}, 'fast', function(){
			flag = true;
		});
	});
	return false;
}

var popupGallery = {};

popupGallery.open = function() {
	shadow = $('#popup_shadow');
	gallery = $('#popup_gallery');

	shadow.show();
	gallery.show();

	var item = gallery.find('li.el_1 a');
	popupGallery.select(0, item);
	
	return false;
}

popupGallery.close = function() {
	shadow.hide();
	gallery.hide();
	return false;
}

popupGallery.select = function(item,handler){

	handler = $(handler);

	handler.parent().parent()
		.find('a').show()
		.end().find('span').remove();

	handler
		.hide()
		.after('<span>'+handler.html()+'</span>');

	if(typeof gallery != 'undefined') {
		gallery.find('div.item')
			.hide()
			.eq(item).show();
	} else {
		$('#popup_gallery').find('div.item')
			.hide()
			.eq(item).show();
	}
	

	return false;
}


$(
function(){
var table_height = $('#animate_top').css('height');
	var new_height = parseInt(table_height);
	
//	$('.for_equal_height').css('height', new_height);




});

