$(window).load(function(){
	sliders();
	tableList();
});
$(window).resize(function(){
	sliders();
	tableList();
	hello(1);
});
$(document).ready(function(){
	$('.xhover').mouseover(function(){$(this).addClass('hover');});
	$('.xhover').mouseout(function(){$(this).removeClass('hover');});
	$('#cart').click(function(){window.location='/cart';});
	tableList();
	hello();
	if ($('#xflood').size())
	{
		var big=document.createElement('div');
		big.id='flood-big';
		big.innerHTML='<a href=""></a>';
		document.body.appendChild(big);
		
		$('#xflood a').mouseover(function(){
			var position=$(this).css('backgroundPosition');
			position=parseInt(position.split(' ')[1])*2;
			
			$('#flood-big').css('left', $(this).offset().left-37+'px');
			$('#flood-big').css('top', $(this).offset().top-37+'px');
			$('#flood-big a').css('background-position', '0 '+position+'px');
			$('#flood-big').css('display', 'block');
			$('#flood-big a').attr('href', $(this).attr('href'));
		
			$('#flood-big').mouseout(function(e){
				$('#flood-big').css('display', 'none');
			});
		});
	}
	form_comment();
});
function confirmEmail(element){
	element.html('<img src="/img/load.gif" alt="load" /> обработка…');
	element.load("/ajax/confirmemail.php");
}
function ajaxForm(form){
	var data=new Object;
	var callback;
	$('input, select, textarea', $(form)).each(function(){
		if($(this).attr('name')){
			if($(this).attr('name')=='callback'){
				callback=$(this).val();
			}else{
				data[$(this).attr('name')]=$(this).val();
			}
		}
	});
	$.ajax({
		'type': 			form.method,
		'url': 				form.action,
		'dataType': 	'json',
		'data': 			data,
		'success': 		auctionRate
	});
	return false;
}
function openClose(object)
{
	if (object.hasClass('hidden'))
	{
		object.removeClass('hidden');
	}
	else
	{
		object.addClass('hidden');
	}
}
function popUp(url, name, width, height, top, left){
	if(!top){top=($(document).height()/2-Math.ceil(485/2));}
	if(!left){left=($(document).width()/2-Math.ceil(730/2));}
	if(!width){width=730;}
	if(!height){height=485;}
	window.open(url, name, 'top='+top+', left='+left+', width='+width+', height='+height);
}
function hidden(object)
{
	object.animate({'opacity':0,'height':0}, 500, function(){object.addClass('hidden');});
}
function sliders ()
{
	$('.slider').each(function(){
		width=$('.center', this).width();
		count=Math.floor(width/152);
		left=Math.floor(((width/count)-152)/2);
		right=Math.ceil(((width/count)-152)/2);
		$('.item', this).css('padding-left', left+'px');
		$('.item', this).css('padding-right', right+'px');
		$('.item', this).css('display', 'block');
		$('.overflow', this).css('width', (152+left+right)*$('.item', this).size()+'px');
		$(this).removeClass('load');
	});
}
function sliderMove(type, object)
{
	var overflow=$('.overflow', $(object).parent().parent());
	var left=parseInt(overflow.css('left'));
	var width=parseInt($('.item', overflow).width()-(-parseInt($('.item', overflow).css('padding-left')))-(-parseInt($('.item', overflow).css('padding-right'))));
	var widthCenter=parseInt(overflow.parent().width());
	switch (type)
	{
		case 'left':
		{
			if (left<0)
			{
				end=left+width;
				if (end>0)
				{
					end=0;
				}
				overflow.animate({'left':end+'px'}, 'fast');
			}
			break;
		}
		case 'right':
		{
			if (left>-(width*($('.item', overflow).size())-widthCenter))
			{
				var end=left-width;
				if (end<-(width*($('.item', overflow).size())-widthCenter))
				{
					end=-(width*($('.item', overflow).size())-widthCenter);
				}
				overflow.animate({'left':end+'px'}, 'fast');
			}		
			break;
		}
	}
}
function loadImg(object, left, top)
{
	if (!left)
	{
		left=6;
	}
	if (!top)
	{
		top=3;
	}
	var id=Math.ceil(Math.random()*1000)
	var load=document.createElement('img');
	load.src='/img/load.gif';
	load.id='load_'+id;
	$(load).css('position', 'absolute');
	$(load).css('left', $(object).offset().left+$(object).width()-(-parseInt($(object).css('padding-left')))-(-parseInt($(object).css('padding-right')))+left+'px');
	$(load).css('top', $(object).offset().top+top+'px');
	$('body').append(load);
	return 'load_'+id;
}
function addCart(model, mod, add, button)
{
	var load=loadImg(button, 10, 4);
	data='&model[]='+model+'&mod[]='+mod+'&add[]='+add;
	$('input[name^=rec]:checked').each(function(){
		xmodel=this.name.substring(this.name.indexOf('[')+1, this.name.indexOf(']'));
		xmod=this.value;
		xadd=0;
		data=data+'&model[]='+xmodel+'&mod[]='+xmod+'&add[]='+xadd;
	});
	
	$('#cart .data').load('/ajax/cart.php?type=add'+data, function(){$('#'+load).remove();flashCart();});
}
function flashCart()
{
	$('#cart').css('background-color', '#FFBF00');
	$('#cart').animate({'backgroundColor': 'gray'}, 500, function(){$('#cart').css('background-color', '');});
}
function flashElement(element)
{
	var back=element.css('background-color');
	element.css('background-color', '#FFBF00');
	element.animate({'backgroundColor': 'white'}, 500, function(){element.css('background-color', back);});
}
function filterOffer (object)
{
	if ($(object).next().hasClass('hidden'))
	{
		$(object).next().removeClass('hidden');
		$(object).html('убрать фильтр');
		$.cookie('filter', '1', {path:'/', expires:'30'});
	}
	else
	{
		$(object).next().addClass('hidden');
		$(object).html('показать фильтр');
		$.cookie('filter', '0', {path:'/', expires:'30'});
	}
}
function filterWizard (man, pref)
{
	if ($.browser.msie)
	{
		$('#selection-table').load('/ajax/selection.php?man='+man+'&pref='+pref);
	}
	else
	{
		$('#selection-table table').animate({'opacity':0}, 500, function(){
			$('#selection-table').css('visibility', 'hidden');
			$('#selection').css('background-image', 'url(/img/load.gif)');
			$('#selection').css('background-position', '50% 50%');
			$('#selection').css('background-repeat', 'no-repeat');
			$('#selection-table').load('/ajax/selection.php?man='+man+'&pref='+pref, function(){
				$('#selection').css('background-image', '');
				$('#selection-table').css('visibility', 'visible');
				$('#selection-table').animate({'opacity':1}, 500);
				if (man=='small')
				{
					$('#selection').animate({'backgroundColor':'softpink'}, 500);
				}
				
			});
		});
	}
}
function switchFilter (type)
{
	$('#selection-simple').addClass('hidden');
	$('#selection-table').addClass('hidden');
	$('#selection .switch a').removeClass('active');
	$('#selection .switch .'+type).addClass('active');
	$('#selection-'+type).removeClass('hidden');
	$.cookie('filter-type', type, {path:'/', expires:30});
}
function tableList()
{
	if($('table.list').size())
	{
		$('table.list .model-mini').each(function(){
			$('h3', $(this)).css('left', $('img', $(this)).offset().left);
			$('h4', $(this)).css('left', $('img', $(this)).offset().left);
		});
	}
}
function commentForm (id)
{
	if ($('#comment-form').size())
	{
		$('#comment-form').appendTo('#comment-form-'+id);
		if ($('#comment-form textarea').size())
		{
			$('#comment-form textarea').get(0).focus();
			$('#comment-form input[name=parent]').val(id);
		}
		$('#comment-add-title').addClass('button');
		if (!id)
		{
			$('#comment-add-title').removeClass('button');
		}
	}
}

function hello (nomove)
{
	if ($('#hello').size())
	{
		var left=$('#hello').offset().left;
		var width=$('#hello').width();
		
		$('#hello div').css('margin-left', -(Math.ceil((1500-width)/2))+'px');
		if (!nomove)
		{
			
			$('#hello').mousemove(function(e)
			{
				step=Math.ceil((left+Math.ceil($('#hello').width()/2)-e.clientX)*(1500-$('#hello').width())/($('#hello').width()/2)/2)-Math.ceil((1500-$('#hello').width())/2);
				if(step>0){step=0;}
				if(step<-(1500-$('#hello').width())){step=-(1500-$('#hello').width());}
				$('#hello div').css('margin-left', step+'px');
			});
		}
	}
}
function form_comment ()
{
	$('#form-rait .star').mouseover(function(){
		var id=this.id.substring(5);
		for (var i=1;i<=id;i++)
		{
			$('#star-'+i).addClass('hover');
		}
		$(this).mouseout(function(){
			for (var i=1;i<=id;i++)
			{
				$('#star-'+i).removeClass('hover');
			}
		});
	});
	$('#form-rait .star').click(function(){
		$('#form-rait .star').removeClass('active');
		var id=this.id.substring(5);
		if (!$(this).hasClass('active'))
		{
			for (var i=1;i<=id;i++)
			{
				$('#star-'+i).addClass('active');
			}
		}
		$('#form-rait input[name=rait]').val(id);
	});
	$('#comment-form .rec label').click(function()
	{
		$('#comment-form .rec label').removeClass('active');
		$('#comment-form .rec input:checked').parent().addClass('active');
	});
	$('#comment-form .rec input:checked').parent().addClass('active');
}
function otzivRait(el, id, rait)
{
	$('span.title', $(el).parent()).html('<img src="/img/load.gif" />');
	$.post('/', {'otziv-rait':1, 'otziv':id, 'rait':rait}, function(data){
		$('span.title', $(el).parent()).html(data['text']);
		if (data['plus'])
		{
			$('span.plus', $(el).parent()).html(data['plus']);
		}
		if (data['minus'])
		{
			$('span.minus', $(el).parent()).html(data['minus']);
		}
	}, 'json');
}


function closeWindow ()
{
	$('.window-back').remove();
	$('.window-content').remove();
}
function openWindow (url, width, height, background)
{
	if(!background){background='#ffffff';}
	if(!width){width=550;}
	if(!height){height=200;}
	var back=document.createElement('div');
	$(back).click(function(){closeWindow();});
	$(back).addClass('window-back');
	$(back).height($(document).height());
	$(back).appendTo($('body'));
	
	var content=document.createElement('div');
	$(content).addClass('window-content');
	$(content).css('left', $(window).width()/2-(width/2));
	$(content).css('top', $(window).height()/2-(height/2));
	$(content).html('<div id="window-content"></div><div class="clear"></div>');
	$(content).appendTo($('body'));
	
	$("#window-content").css('background', background);
	$('#window-content', $(content)).load(url);
	$(document).keyup(function(e)
	{
		if (e.keyCode==27)
		{
			closeWindow();
		}
	});
}
function recall (phone, name, hour, min)
{
	if (phone || name || hour || min)
	{
		if (!phone)
		{
			alert('Укажите свой телефон');
			return false;
		}
		$.post('/recall', {'phone':phone, 'name':name, 'date_hour':hour, 'date_min':min}, function(data){if(data){alert('Ваша заявка принята');closeWindow();}else{alert('Ваша заявка уже была принята, отправлять заявки можно раз в 10 минут');}}, 'json');
	}
	else
	{
		openWindow('/recall');
	}
}

