$(document).ready(function(){
	var count_ak=$('.slider_list a').length;
	var current_ak=0;
	change_akciya();
	var timer = window.setInterval(change_akciya, 5000);
	var brend_hover=false;
	$('.fancybox').fancybox();
	
	
	$('.slider_list a').click(function(){
		clearInterval(timer);
		var id=$(this).attr('id');
		id=id.split('-');
		id=id[2];
		current_ak=eval(id)-1;
		change_akciya();
		timer = window.setInterval(change_akciya, 5000);
		return false;
	});

	function change_akciya()
	{
		if (current_ak==0)
			current_ak=count_ak;
		next_ak=current_ak+1;
		if (next_ak>count_ak)
			next_ak=1;
		$('.slider_list a').attr('class','');	
		$('#banner-title-'+current_ak).animate({'opacity':0},500,function(){$(this).hide();$('#banner-title-'+next_ak).show();$('#banner-title-'+next_ak).animate({'opacity':1},500);});
		$('#banner-image-'+current_ak).animate({'opacity':0},500,function(){$(this).hide();$('#banner-image-'+next_ak).show();$('#banner-image-'+next_ak).animate({'opacity':1},500);});
		$('#banner-nav-'+next_ak).attr('class','active');	
		current_ak=next_ak;
	}

	function isChild(e, p){
		while(e)
		{
			if (e == p)
				return true;
			else
				e = e.parentNode;
		}
		return false;
	}	
	
	$('.speciallist_left').click(function(){
		$('.speciallist_right').css('visibility','visible');
		var margin=parseInt($('.speciallist_frame_table').css('margin-left'));
		if (margin<0)
		{
			margin+=220;
			$('.speciallist_frame_table').animate({'marginLeft':margin+'px'},600);
		}
		if (margin>-210)
			$('.speciallist_left').css('visibility','hidden');
	});
	
	$('.speciallist_right').click(function(){
		$('.speciallist_left').css('visibility','visible');
		var width=parseInt($('.speciallist_frame_table').css('width'));
		var margin=parseInt($('.speciallist_frame_table').css('margin-left'));
		if (width+margin>670)
		{
			margin-=220;
			$('.speciallist_frame_table').animate({'marginLeft':margin+'px'},600);
		}
		if (width+margin<890)
			$('.speciallist_right').css('visibility','hidden');
	});	
	
	$('#brends').hover(
		function(e){
			$('.cpt_brend_box').show();
		},
		function(e){
			var e = window.event||e;
			var relTarg = e.relatedTarget || e.toElement;
			var ischild=isChild(relTarg,document.getElementById('brend_box'))
			if (relTarg.id!='brend_box' && ischild==false)
				$('.cpt_brend_box').hide();
		}
	);
	
	$('.cpt_brend_box').hover(
		function(){
		},
		function(e){
			var e = window.event||e;
			var relTarg = e.relatedTarget || e.toElement;
			if (relTarg.id!='brends' && relTarg.id!='brend_box')
				$('.cpt_brend_box').hide();
		}
	);	
	
	$('#product_small_images a').click(function(){
		$('#product_small_images img').removeClass('active');
		$(this).find('img').addClass('active');
		$('#big_pic img').attr('src',$(this).attr('mid_pic'));
		$('#big_pic').attr('href',$(this).attr('big_pic'));
		return false;
	});
	
	$('.input_select').live('change',function(){
		$(this).parent().find('span').text($(this).find('option:selected').text());
	});
	
	$('.search_price').click(function(){
		$('.search_price').removeAttr('checked');
		$(this).attr('checked','checked');
		var search_price=$(this).val();
		switch (search_price)
		{
			case 'econ':
				$('#search_price_from').val('0');
				$('#search_price_to').val('2000');
				break;
			case 'sred':
				$('#search_price_from').val('2000');
				$('#search_price_to').val('6000');
				break;
			case 'vip':
				$('#search_price_from').val('6000');
				$('#search_price_to').val('');
				break;
		}
	});
	$('.add2cart_handler_new').click(function(){
		$.fancybox({
			'href':'#msg',
			'showCloseButton':false
		});
		var prid=$(this).parent().parent().attr('rel');
		var cart_col=$('.cart_col').text();
		cart_col++;
		$.ajax({
			url: '?productID='+prid+'&action=add_product&view=noframe&ukey=cart',
			success: function(data) {
				$('#cart_img').attr('class','cart_active');
                var reg=/<span id="cart_total" class="total"><span>(.+)<\/span><\/span>/;  
                var arr=reg.exec(data); 
				$('.cart_col').text(cart_col);
				$('.cart_sum').text(arr[1]);
				$.fancybox.close();
			}
		});	
		return false;
	});
});
