$(document).ready(function(){
	
	openimage_init();
	
	// Изменение картинки
	$('a.set_image').click(function(){

		$('.small_img div').removeClass('select');
		$(this).parent().addClass('select');
		
		
		var main_image = $('#main_image');
		//main_image.addClass('load');
		
		url = $(this).attr('href');

		$.ajax({
		    url: url,
		    dataType: "html",
		    type: 'POST',
		    success: function (data) 
		    {
				//$('img:first',data).load(function(){
					main_image.html(data);
					openimage_init();
				//});
				
			}
		});		
		
		return false;
	});
	
});


// Увеличенные миниатюрки
function openimage_init()
{
	$('a.openimage').click(function(){
	
		// Создадим картинку
		image_data = $('<img>');
		image_data.attr('src',$(this).attr('href'));
		image_data.css('cursor','pointer');
		image_data.click(function(){$.modal.close();});
	
		image_data.load(function(){
	
			// Ресайз 
			cwidth = $(window).width();
			cheight = $(window).height();
			
			imgWidth = image_data.attr('width');
			imgHeight = image_data.attr('height');
		
			widthPrecent=0;
			if (imgWidth>cwidth) 
			{
				rzc=(imgWidth-cwidth)+50;
				prc=imgWidth/100;
				widthPrecent=Math.ceil(rzc/prc);
			}
		
			heightPrecent=0;
			if (imgHeight>cheight) 
			{
				rzc=(imgHeight-cheight)+50;
				prc=imgHeight/100;
				heightPrecent=Math.ceil(rzc/prc);
			}
		
			if (widthPrecent>0 || heightPrecent>0)
			{
				if (widthPrecent>heightPrecent)
					prec=widthPrecent;
				else
					prec=heightPrecent;
				
				imgWidth=Math.ceil(imgWidth-((imgWidth/100)*prec));
				imgHeight=Math.ceil(imgHeight-((imgHeight/100)*prec));
			}
			
			image_data.attr('width',imgWidth);
			image_data.attr('height',imgHeight);
			
			$.modal(image_data,{
				overlayClose:true,				
				escClose:true,
				opacity:50,
				overlayCss: {backgroundColor:"#000"},
				autoResize:true,
				
				onOpen: function (dialog) {
					dialog.overlay.fadeIn(300, function () {
						dialog.data.hide();
						dialog.container.fadeIn(300, function () {
							dialog.data.fadeIn(300);
						});
					});
				},
				onClose: function (dialog) {
					dialog.data.fadeOut(300, function () {
						dialog.container.fadeOut(300, function () {
							dialog.overlay.fadeOut(300, function () {
								$.modal.close();
							});
						});
					});
				}
			});
		
		});
		
		return false;
	});

}

$(document).ready(function(){	
	$("input.input").addClass("idle");	
	$("textarea.input").addClass("idle1");
	$("input.submit").addClass("idle2");
	
});
	

function setPImage(img,txt,id,num) 
{
	var boldp = document.getElementById('boldp');
	if (boldp){
		boldp.innerHTML = '<img src="'+img+'" alt="'+txt+'" onclick="doLoad(\'\',\'/ajax/catalog/'+id+'/'+num+'/\');">'+(txt?'<p>'+txt+'</p>':'')+'';
	}
}

jQuery(function ($) {
	$('.modal_fb').click(function (e) {
		$('#basic-modal-content').modal();

		return false;
	});

});

