var current = 1;
var special_items = 0;
var list_width = 0;

function stop_timer() {
	if(slides){
		clearInterval(slides)
	}
	$('#special #timer').addClass('play')
}

function get_next() {
	var w = 0;
	$('#special dl dd a').removeClass('active')
	current++
	if (current > special_items) {
		current = 1
		$('#special ul').animate({marginLeft: '0'},400)
	}
	$("#special ul li:lt("+ (current-1) +")").each(function() {
		w += $(this).width() + 17
	})
	$('#special ul').animate({marginLeft: -w},400)	
	$('#special dl dd a:eq(' + (current-1) + ')').addClass('active')
}

function blam(el) {
	$(el).animate({opacity: 0.25},350).animate({opacity: 1},350).animate({opacity: 0.25},350).animate({opacity: 1},350);
}

function slide(el,step) {
	list_width = 0
	$(el).find('li').each(function() {
		list_width += $(this).outerWidth()
	})
	if (list_width >= 662) {
		var m = $(el).css('margin-left')
		pos = new Number(m.substr(0,m.length-2))
		if ((pos + step) > 0) {
			$(el).animate({marginLeft: 0},300)
		} else if ((pos + step - 737) < -(list_width+50)) {
			$(el).animate({marginLeft: (687-list_width-50)},300)
		} else {
			$(el).animate({marginLeft: '+=' + step},300)
		}
	}
}

function add_to_cart(id, cost, numbers) {
	var subtotal_before = new Number($('#subtotal').text())
	var count = new Number($('#count').text())
	cost = new Number(cost)
	$('#cart #subtotal').html(subtotal_before+cost)
	$('#cart #count').html(count + numbers)
	blam('#cart a')
	
	$.post('/ajax.php', {action: 'add-to-cart', count: numbers*1, prodid: id},	function(data) {																						 
		if('ok' == data.status){
			/*
				var subtotal_before = new Number($('#subtotal').text())
				var count = new Number($('#count').text())
				cost = new Number(cost)
				$('#cart #subtotal').html(subtotal_before+cost)
				$('#cart #count').html(count + numbers)
			*/
			
			/*
				$('#cart #subtotal').html(data.sum_price)
				$('#cart #count').html(data.sum_num)
				 blam('#cart a')
				 */
		}
		
	}, "json"
	);

}

function calc() {
	var summary = 0;
	var summary_count = 0;
	
	var ids = "";
	var first_tr = true;
	
	$("#ordered tr:not(.no_selected)").each(function(){
		if(first_tr){
			first_tr = false;
			return;
		}
		
		summary += new Number($(this).find('.cost').html())
		var count = new Number($(this).find('.count').attr('value'))
		summary_count += count
		
		prod_id = $(this).find('.count').attr('id')
		prod_id = prod_id.substring(4)

		ids += prod_id + "=" + count + ";"	
	})
	$('#finaly').html(summary)
	$('#subtotal').html(summary)
	$('#count').html(summary_count)
	
	$.post('/ajax.php', {action: 'save-cart', list: ids},	function(data) {																						 
		if('ok' == data.status){
			//
		}
		
	}, "json"
	);
}



$(document).ready( function() {
	
	$('#topbar').wrapInner('<div id="topbar_data"/>').find('#cart').wrapInner('<div />');
	//$('#login_form').wrap('<div id="login_form_container"/>').wrap('<div id="login_form_data"/>');
	$('.bordered').wrapInner('<div class="after_bordered"/>').wrapInner('<div class="before_bordered"/>').find('.after_bordered').append('<div class="clear" />')
	$('#step dt').nextUntil('.current_action').addClass('visited')
	
	$('#sidebar li:has(dl)').each(function() {
		$(this).find('a:first').append('&nbsp;<strong>&nbsp;&nbsp;</strong>')
		$(this).find('a:first').toggle(function() {
			$(this).parent().find('dl').slideDown("slow");
			$(this).addClass('active')
			return false;
		}, function(){
			$(this).parent().find('dl').slideUp("slow");
			$(this).removeClass('active')
			return false;
		})
	})
	
	$('#topbar .sign_in').click(function() {
		$('#login_form').toggle()
		return false;
	})	
	
	$(document.body).click(function(e){
	   if(!$(e.target).parents().is("#login_form")){
		$('#login_form').hide()
	   }
	})   
	
	
	$('.items_list').each(function() {
		if ($(this).is('.slider') == false) {
			$(this).append('<li class="clear" />')
		}
		$(this).find('img').wrap('<span class="picture"/>')
		$(this).find('li').hover(
			function() {$(this).addClass('hover')},
			function() {$(this).removeClass('hover')}
		)
	})
	
	// IE fix
	if ($.browser.msie) {
		$('.items_list').find('a:not(.add_to_cart)').click(function(){
			window.location.href=$(this).attr('href')
		})	
	}
	
	$('.special_list').each(function(){
		$(this).append('<li class="clear" />').find('img').wrap('<span class="picture"/>')
		$(this).find('li').has('.picture').addClass('big')
	})
	
	$('#search input.text').bind({
		blur:	function() {if (this.value=='') this.value='поиск товара';},
		focus:	function() {if (this.value=='поиск товара') this.value='';}
	});
	
	$('.add_to_cart').click(function(){
		var product_cost = $(this).parent().find('.cost').text()
		var product_id = $(this).attr('id').substring(4);
		if ($(this).parent().parent().is('.product_info')) {
			var numbers = new Number($(this).parent().find('.count').attr('value'))
			add_to_cart(product_id, product_cost, numbers)
		} else {
			add_to_cart(product_id, product_cost, 1)			
		}		
		return false;
	})

	
	$('#special').each(function() {
		$(this).find('.after_bordered').wrapInner('<div class="special_bordered"/>')
		$(this).find('li img').wrap('<span class="picture"/>').parent().parent().parent().addClass('current')
		$('#special dl dd a:first').addClass('active')
		special_items = $(this).find('li').size();
		slides = 0;
		if(special_items > 2){
			slides = setInterval("get_next()", 2500)
		}
		$(this).find('dl dd a').click(function(){
			if ($(this).is('.play#timer')) {
				if(special_items > 2){
					slides = setInterval("get_next()", 2500)
				}
				$(this).removeClass('play')
			} else if ($(this).is('#timer')) {
				stop_timer()
			} else {
				var number =new Number($(this).text())
				current = number-1
				get_next()
				stop_timer()
			}
			return false;
		})
	})
	
	$('.producers').each(function() {
		$(this).wrap('<div class="producers_container" />').after('<div class="producers_nav"><a class="producers_next" href="#"></a><a class="producers_prev" href="#" /></a></div>')
		$(this).parent().find('.producers_nav a').click(function() {
			if ($(this).is('.producers_next')) {
				slide('.producers',-200)
			} else {
				slide('.producers', 200)
			}
			return false;
		})
	})
	
	$('.product_info').each(function() {
		$(this).append('<dd class="clear"/>')
	})
	
	$('input.count').each(function() {
		$(this).after('<span class="count_controller"><a class="plus" href="#"></a><a class="minus" href="#"></a></span>').wrap('<div class="count_wrap"/>')
		var input = $(this)	
		var cost = new Number($(this).parent().parent().parent().find('.cost_per').text())	
		if (input.parent().parent().is('.sum')) {
			var result = input.parent().parent().find('.cost')			
		} else {
			var result = input.parent().parent().parent().find('.cost')
			input.parent().parent().addClass('count_cell')
		}
		result.html(cost*input.attr('value'))
		$(this).parent().parent().find('.count_controller a').click(function() {
			var n = new Number(input.attr('value'))
			if ($(this).is('.plus')) {n++} 
			else if (n>=2) {n--}
			input.attr('value',n)
			result.html(cost*(n))
			if (input.parent().parent().is('td')) {calc()}
			return false;
		})
		$(this).change(function() {
			var n = new Number(input.attr('value'))
			if (n<1) {
				input.attr('value',1)
				n=1
			}
			result.html(cost*n)
			if (input.parent().parent().is('td')) {calc()}
		})
	})
	
	$('.slider').each(function() {
		$(this).wrap('<div class="slider_container" />').after('<div class="slider_nav"><a class="slider_prev" href="#" /></a><a class="slider_next" href="#"></a></div>')
		$(this).parent().find('.slider_nav a').click(function() {
			if ($(this).is('.slider_next')) {slide('.slider',-159)} else {slide('.slider', 159)}
			return false;
		})
	})
	
	$('#ordered tr').each(function() {
		var tr = $(this)
		tr.hover(function(){
			tr.addClass('hover')
		}, function(){
			tr.removeClass('hover')
		})
		var box = $(this).find('input:checkbox')
		box.after('<div class="checked checkbox"/>').addClass('hide')
		var div = $(this).find('div.checkbox')
		if (box.attr("checked") == false) {
			div.removeClass('checked')
			tr.addClass('no_selected')
			calc()
		}
		div.toggle(function(){
			box.attr("checked",false)
			div.removeClass('checked')
			tr.addClass('no_selected')
			calc()
		}, function(){
			box.attr("checked",true)
			div.addClass('checked')
			tr.removeClass('no_selected')
			calc()
		})		
	})
	
	$('#subtotal_in_cart').each(function() {
		$(this).parent().css('padding-bottom',1)
	})
	
})
