/* -------------------------------------------------- *
 * Project scripts
 * -------------------------------------------------- *
 * Author: Morozov Igor
 * URL: http://www.morozoff.info/
 * Copyright: 2010 Morozov Igor
** -------------------------------------------------- */

$(document).ready(function() {
	if($('#applescroll').length > 0) {
		if($('div.b-head-i i').length > 1)
		{
			$('.b-applescroll').loopedCarousel({
				container: '.b-head',
				slides: '.b-head-i',
				items: 1,
				padding:0,
				next: '.l-ar'
			})
			$('.b-applescroll').loopedCarousel({
				container: '#applescroll',
				slides: '#applescroll ul',
				items: 1,
				padding:0,
				next: '.r-ar'
			})
		}
		else
		{
			$('.b-applescroll').loopedCarousel({
				container: '#applescroll',
				slides: '#applescroll ul',
				items: 1,
				padding:0,
				next: '.r-ar',
				prev: '.l-ar'
			})
		}
		
		$('.b-applescroll .l-ar, .b-applescroll .r-ar').click(function() {
			return false;
		});
	}
	
	if($('.b-similar-goods').length > 0) {
		$('.b-similar-goods .pane').show();
		$('.b-similar-goods .b-sit-i').scrollable({
			size:4
		}).circular();
		$('.b-similar-goods .l-ar, .b-similar-goods .r-ar').click(function() {
			return false;
		});
		$(".b-similar-goods ul.b-tabs").tabs(".b-similar-goods .b-panes > .pane");
	};
	
	$('.selrep').sSelect();

	if($('.b-cart-chk').length > 0) {
		$('.b-cart-chk').addClass('b-cart-chk-js');
		$('.b-cart-chk .hd input').click(function() {
			var $t = $(this);
			var $contain = $t.parents('ul');
			$('li',$contain).removeClass('selected');
			if($t[0].checked) {
				$t.parents('li').addClass('selected');
			}
		});
	}
	
	if($('.ips .chk-b').length > 0) {
		$('.ips .chk-b input').click(function() {
			$(this).parents('.b-forms').find('input:password').parents('.ips').toggle();
		});
	}
	
	if($('.b-three-cats a').size() == 0)
	{
		$('.h-topbn').remove();
	}

	if($('.b-items-shop').length > 0) {
		$('.b-items-shop li a').hover(function() {
			$(this).parents('li').addClass('li-hover')
		}, function() {
			$(this).parents('li').removeClass('li-hover')
		});
	}
	
	$('.btn-style').hover(function() {
		$(this).addClass('btn-hover')
	}, function() {
		$(this).removeClass('btn-hover')
	});
	
	var zoom = $('.big-ph-i li a').jqzoom({
		zoomWidth: 278,
		zoomHeight: 278,
		xOffset: 10,
		yOffset: 0,
		position: "right"
	});

	$('dl.b-size').hide();
		
	$('dl.b-choose-color dd a').click(function() {
		$('dl.b-choose-color dd[class=selected]').removeClass('selected');
		$(this).parent('dd').addClass('selected');
		
		$('ul.b-thumbs:visible').hide();
		$images = $('#'+$(this).attr('id')+'_images');
		if($images.find('li a').length == 0)
		{
			$images = $('#color0_images');
		}
		$images.show().find('li a').first().click();

		$('dl.b-size:visible').hide();
		$('small.no-item:visible').hide();
		lid = $('#'+$(this).attr('id')+'_sizes').show().find('select').val();
		if(lid != null)
		{
			$('p.big-cost:visible').hide();
			$('#lid'+lid+'_big_cost').show();
			$('div.b-cost-tot:visible').hide();
			$('small.no-item:visible').hide();
			$('#lid'+lid+'_cost').show();
			if($('div.b-cost-tot:visible').length > 0 || lid == 0)
			{
				$('div.btn').show();
			}
			else
			{
				$('div.btn').hide();
			}
			$cost = $('div.b-cost-tot:visible');
			$cost.find('input[name=count]').val(1);
			$cost.find('span.cost-summ').html(($cost.find('input[name=count]').val()*$cost.find('input[name=price]').val())+' P');
		}
		else
		{
			$('p.big-cost:visible').hide();
			$('div.b-cost-tot:visible').hide();
			$('div.btn').hide();
		}
		return false;
	});

	$('dl.b-size select').change(function(){
		$('p.big-cost:visible').hide();
		$('#lid'+$(this).val()+'_big_cost').show();
		$('div.b-cost-tot:visible').hide();
		$('small.no-item:visible').hide();
		$('#lid'+$(this).val()+'_cost').show();
		if($('div.b-cost-tot:visible').length > 0 || $(this).val() == 0)
		{
			$('div.btn').show();
		}
		else
		{
			$('div.btn').hide();
		}
		$cost = $('div.b-cost-tot:visible');
		$cost.find('input[name=count]').val(1);
		$cost.find('span.cost-summ').html(($cost.find('input[name=count]').val()*$cost.find('input[name=price]').val())+' P');
	});

	$('ul.b-thumbs li a').click(function() {
		$(this).parent().parent().find('li a[class=current]').removeClass('current');
		$(this).addClass('current');
		
		$('div.big-ph-i ul li a').attr('href', $(this).attr('rel'))
		$('div.big-ph-i ul li a img').attr('src', $(this).attr('href'))
	
		return false;
	});
	
	$('div.b-cost-tot input[name=count]').keypress(function (e)
	{
		if(e.which!=8 && e.which!=0 && (e.which<48 || e.which>57))
		{
			return false;
		}
	});
		
	$('div.b-cost-tot input[name=count]').keyup(function() {
		if($(this).val() == '' || $(this).val() == 0)
		{
			$(this).val(1);
		}

		$cost = $('div.b-cost-tot:visible');
		$cost.find('span.cost-summ').html(($cost.find('input[name=count]').val()*$cost.find('input[name=price]').val())+' P');
	});

	
	$('div.b-card-item div.btn input[name=submit_form]').click(function() {
		
		var id_color = $('dl.b-choose-color dd[class=selected] a').attr('id');
			
		if($('dl.b-size:visible').length != 1)
		{
			var offset = $('div.b-card-item div.btn input[name=submit_form]').offset();
			$('#basket_message_error_color').show().css({left:offset.left+'px',top:offset.top+'px',marginLeft:'-'+$('div.b-card-item div.btn input[name=submit_form]').width()/2+'px'})
			$('#basket_message_error_color .close').click(function() {
				$(this).parents('.pp-ftip').hide();
				return false;
			});
		}
		else if($('div.b-cost-tot:visible').length != 1)
		{
			var offset = $('div.b-card-item div.btn input[name=submit_form]').offset();
			$('#basket_message_error_size').show().css({left:offset.left+'px',top:offset.top+'px',marginLeft:'-'+$('div.b-card-item div.btn input[name=submit_form]').width()/2+'px'})
			$('#basket_message_error_size .close').click(function() {
				$(this).parents('.pp-ftip').hide();
				return false;
			});
		}
		else
		{
			$('div.cart').html('<img src="/webroot/delivery/img/ajax-loader.gif" width="32" height="18" alt="" /><br /><i class="ico"></i>');
			$cost = $('div.b-cost-tot:visible');
			var order_data = {
				lid: $cost.find('input[name=lid]').val(),
				art: $cost.find('input[name=art]').val(),
				id_color: $cost.find('input[name=id_color]').val(),
				id_size: $cost.find('input[name=id_size]').val(),
				id_product: $cost.find('input[name=id_product]').val(),
				price: $cost.find('input[name=price]').val(),
				discount: $cost.find('input[name=discount]').val(),
				count: $cost.find('input[name=count]').val()
			}
			
			$('div.cart').load('/plugins/ajax/basket.php', order_data, function(){
				var offset = $('div.b-card-item div.btn input[name=submit_form]').offset();
				$('#basket_message').show().css({left:offset.left+'px',top:offset.top+'px',marginLeft:'-'+$('div.b-card-item div.btn input[name=submit_form]').width()/2+'px'})
				$('#basket_message .close').click(function() {
					$(this).parents('.pp-ftip').hide();
					return false;
				});
			});
		}
		
		return false;
	});

	$('#twn-list').bind('change', function(event) {
		var $t = $(this);
		var indexsel = $t[0].selectedIndex;
		var valuesel = $t[0].options[indexsel].value;
	});
	
	if($('#scroll-towns').length > 0) {
		$('#scroll-towns').jScrollPane({
			scrollbarWidth:21,
			showArrows:true,
			arrowSize:20,
			dragMinHeight:1
		});
	}
});
