$(document).ready(function(){

    reloadCaptcha();

    element = document.getElementById('mycarousel');    
    if (element){
        jQuery(document).ready(function() {
            jQuery('#mycarousel').jcarousel({
                auto: 5,
                wrap: 'circular'
            });
        });                
    } 
    
    $(".itemGet").colorbox({href: $(this).attr("href"), inline:true, innerWidth:700});
    
    $("a[rel='lytebox']").colorbox();
    
    
    $(".serial .item a").hover(
        function () {            
            $(this)
            .find("img").attr("src", $(this).find("img").attr("src").replace("m","ms")).addClass("abs");            
        }, 
        function () {
            $(this)
            .find("img").attr("src", $(this).find("img").attr("src").replace("ms","m")).removeClass("abs");            
        }
    )
    
    
             
});


function getVstav(type){
    if(type == 'с драгоценными вставками'){
        $("#vstav").show();
    }else{
        $("#vstav").hide();
    }
}

/* обновляем капчу */
function reloadCaptcha(){
	
	$("#captchaImg").attr("src","/captcha/"+Math.round(Math.random()*1000));
	$(".captchaText").attr("value","");
	
	return true;
}

/* разворачивающиеся блоки */
function toggleBlock(block){    
	if ($("#"+block).css("display") == 'block'){	
		//сворачиваем				
		$("#"+block).css("display","none");
	}else{										
		$("#"+block).css("display","block");		
	}	
}

/* заносим товар в корзину */
function putBasket(){
    if (arguments.length == 1){
        argID = arguments[0];        
    }else{
        argID = false;
    }  
    
    if(argID){
        params = $("form#basketForm"+argID).serialize();    
    }else{
        params = $("form#basketForm").serialize();
    }

	jQuery.ajax({
	  	data: params,
		type: "POST",
		url: '/back/addToBasket',
		timeout: 10000,	
        beforeSend: function(){
            if(argID){
                $("#goodResult"+argID).hide();
                $("img#loading"+argID).show();
            }else{
                $("#goodResult").hide();
                $("img.basket").show();
            }
    		                
        },			
		error: function(xhr, desc, e) {
			alert("Ошибка при добавлении: " + desc);
		},			
		success: function(data) {				
            $("#basket").html(data);	 	
            if(argID){
                $("#goodResult"+argID).show();
            }else{
                $("#goodResult").show();
            }
		},
        complete: function(){
            if(argID){
                $("img#loading"+argID).hide();
            }else{
                $("img.basket").hide();
            } 
        }	
	});	
    return false;
}


function photoVoteFunc(form){
    var params = jQuery(form).serialize();				 				            		  					  	 
	
    var num = form.num.value;
    var word = form.word.value;

    if (num != word){
        alert("Вы неверно ввели цифру. Нужно ввести «"+num+"».");
        return false; 
    }    
    
	jQuery(form).ajaxStart(function() {				
			jQuery(this).hide();	
		}
	).ajaxStop(function() {
			jQuery(this).animate({opacity: "show"}, 500);	
		}
	);
	
	jQuery.ajax({
	  	data: params,
		type: "POST",
		url: '/back/photoVote',
		timeout: 2000,			
		error: function(xhr, desc, e) {
			alert("Ошибка при добавлении: " + desc);
		},			
		success: function(data) {
            $("#pi"+num).addClass("voted");		  
			jQuery("#photo"+num ).html(data);	 	
			
		}	
	});			 
       
    return false;
}
