$(function() {

	$('.container').click(function() {

		$('#popup').css('display', 'none');

	});

	

	$('#popup_title a').click(function() {

		$('#popup').css('display', 'none');

		return false;

	});

	$('body table:first').click(function(){
	  $('#popup').css('display', 'none');
	})

	$("#dialog").dialog({

		bgiframe: true,

		height: 140,

		modal: true,

		autoOpen: false

	});
	$(document).click(function(){
	  $('#dialog').dialog('close');
	})
	$('#popup_prev, #popup_next, .thumbnail').click(function() {

		

		$('#popup').css('display', 'block');

		$('#popup_content').css('display', 'none');

		$('#popup_loader').css('display', 'block');

		

		$.getJSON($(this).attr("href"), function(data) {

			var img = new Image;
			img.onload = function() {
				$('.toremove').remove();
				if (this.width < 620) {
					 $.ajax({
					    //url: "/bg/popup_2.html",
					    url: "popups.php?type=2",
					    async: false,
					    success: function(html){
					      $('#append_after').after(html);
					    }
					  });

				} else {

				     $.ajax({
					//url: "/bg/popup_1.html",
					url: "popups.php?type=1",
					async: false,
					success: function(html){
					  $('#append_after').after(html);
					}
				      });
				}
				$('#popup_image img').attr('src', (this.width<620?
				  this.src.toString().replace('/files/', '/phpThumb/phpThumb.php/w=400;h=400;zc=1;/files/'):
				  this.src.toString().replace('/files/', '/phpThumb/phpThumb.php/w=400;h=400;zc=1;/files/')
				));

				$('.addtocartbut').click(function(){

				    $.get('ecart.php?page=addprod&id='+$('#id').val()+'&count='+$('#qtny').val()+'&comment='+$('#commentorder').val(), {}, function(data){
				      $('#cart_gross_items').text(data); 
				      //$('#popup').hide();
				      $("#dialog").dialog('open');
				    })

				    return false;
				});

				$('#popup_title h1').text(data.title);

				$('#dialog_title').text(data.title);

				$('#a_size').text(data.size);

				$('#a_qty').text(data.qty);

				$('#a_materials').text(data.materials);

				$('#a_info').text(data.info);

				$('#popup_price').text(data.price.toFixed(2));

				

				$('#popup_next strong').text(data.prev.title);

				$('#popup_prev strong').text(data.next.title);

// 				console.log( data.prev.id );

				if(data.prev.id > 0 ) {

					$('#popup_next').removeClass('hidden');

// 					$('#popup_prev').attr('href', 'ajax/product' + data.prev.id + '.js');

					$('#popup_next').attr('href', data.prev.href);

				} else {

					$('#popup_next').addClass('hidden');

				}

// 				console.log( data.next.id );

				if (data.next.id > 0) {
					$('#popup_prev').removeClass('hidden');
// 					$('#popup_next').attr('href', 'ajax/product' + data.next.id + '.js');
					$('#popup_prev').attr('href', data.next.href);
				} else {
					$('#popup_prev').addClass('hidden');
				}

				$('#id').attr('value', data.id);

				$('#popup_content').css('display', 'block');

				$('#popup_loader').css('display', 'none');

				this.onload=function(){};
				$('.addtocartbut').get(0).focus();
			}

			

			img.src = data.image;

		});

		return false;

	});

});
