var cam;

function generateSRC(obj) {
	obj.src = obj.lowsrc+"&_="+Math.random().toString();
}


$(document).ready(function(){
		
		$("div.slide-text li.prev").click(function(){
			current=$("div.slide-text-body").index($("div.slide-text-body:visible")[0]);
			new_tip=current-1;
			if (!$('*').is("div.slide-text-body#tip"+(new_tip))) new_tip=$("div.slide-text-body").size()-1;
			
			$("div.slide-text-body#tip"+current).css('display','none');
			$("div.slide-text-body#tip"+new_tip).css('display','block');

			return false;
		});
		
        $("div.next a").click(function(){ 
            left=$("div.scroll-area").scrollLeft()+138;
           var MinLeft=($("div.scroll-area div.items div").size()-3)*138;  

           if (MinLeft< left) left=MinLeft;
            $("div.scroll-area").scrollLeft(left);
            return false;
        });

        $("div.prev a").click(function(){
            left=$("div.scroll-area").scrollLeft()-138;
           if (left<0) left=0; 
            $("div.scroll-area").scrollLeft(left);
            return false;
        });

        $('.camera-view').click(function() {		
	        var height=345;
        	var width=460;
		var top=0;
		var left=0;
 	  	if(width > screen.width-10 || height > screen.height-28) scroll = "yes";
	   	if(height < screen.height-28) top = Math.floor((screen.height - height)/2-14);
		if(width < screen.width-10) left = Math.floor((screen.width - width)/2-5);
		var options='width='+width+',height='+height+',top='+top+',left='+left;
		window.open(this.href,'CameraView',options);
        return false;
	});
		
	
	$("#DeleteAllFromBasket").click(function() {
		//alert('asasa');
		$("input:checkbox[name^=DELETE_]").attr('checked','true');
		$("#"+$(this).attr("form_id")).append("<input type='hidden' name='BasketRefresh' value='1'>");
		$("#"+$(this).attr("form_id")).submit();
	});
	
	$("#show_detail").click(function() {
		$("#preview_text").hide();
		$("#detail_text").show();
		return false;
	})
	$("#show_preview").click(function() {
		$("#preview_text").show();
		$("#detail_text").hide();
		return false;
		
	})	
/* </3D> */

	$("form a.dosubmit").click(function(){
/* <3D>*/
		if ($(this).attr('param')){
			$("#"+$(this).attr("form_id")).append("<input type='hidden' name='"+$(this).attr('param')+"' value='1'>");
		}
/* </3D>*/
		$("#"+$(this).attr("form_id")).submit();
		return false;
	})

	$("form[action*='ADD2BASKET']").submit(function(){
		$.fn.fancybox.showLoading();

		$.ajax({
			type: "GET",
			url: $(this).attr("action"),
			data: { quantity: $("input[name=quantity]", this).val() },
			
			success: function(msg){

				width = 200;
				height = 100;
				
				w = $.fn.fancybox.getViewport();


				var itemLeft	= (width + 36)	> w[0] ? w[2] : (w[2] + Math.round((w[0] - width - 36) / 2));
				var itemTop		= (height + 50)	> w[1] ? w[3] : (w[3] + Math.round((w[1] - height - 50) / 2));

				$("#block_basket").html(msg);
				$("#fancy_content").html("<div class=\"basket_fancy\" style=\"width: "+width+"px\" align=\"center\"><h3>Добавлено к заказу</h3></div>");

				$("#fancy_outer").css({width: width, height: height, left: itemLeft, top: itemTop}).fadeIn();
				
				setTimeout('$("#fancy_outer").fadeOut()', 1000);
				
				$("#fancy_loading").hide();
			}
		});

		return false;
	})

	$("#catalog_show_images_checkbox").click(function(){
		if (($(this).attr('value')!=$(this).is(":checked"))){
			$('#catalog_show_images').attr('value',$(this).is(":checked"));
			$(this).parent().submit();
		}
		//location.href = $(this).parent().attr("action")+"?"+$(this).attr("name")+"="+$(this).is(":checked");
	 })
	
	$("table.catalog-table > tbody > tr:even").addClass("even");

	$("table.catalog-table > tbody > tr")
		.hover(
			function(){
				$(this).addClass("hovered");
			},
			function(){
				$(this).removeClass("hovered");
			}
		);

	$("img.camera_img").load(function(){
		image=this;

		clearInterval(cam);
		cam = setInterval("image.src = image.lowsrc+'&_='+Math.random().toString();", 5000);
	})


	$("a.detail").fancybox({
		'overlayShow'			: true,
		'overlayOpacity'		: 0.6,
		'padding'				: '2',
		'zoomSpeedIn'			: 0, //1000
		'zoomSpeedOut'			: 0,//500,
	//	'easingIn'				: 'easeOutBack',
	//	'easingOut'				: 'easeInBack',
		'frameWidth'			: 558,
		'frameHeight'			: 670,
		'hideOnContentClick'	: false

	});

})
