m.print = (function() {
	return {
		photo: {
			add: function(photoId) {
				m.print.uriString = 'http://'+m.vars.apiServer+'/photo_cart?pathcode='+photoId+'&_method=post&callback=?';
				$.getJSON(m.print.uriString, function(response) {
					m.print.cart.photos = response.photos;
					$("#toPrintObj").val(escape($.toJSON(m.print.cart)));
					if(response.stat!=='ok') {
						$("#"+photoId).attr('checked', '');
						alert("Sorry, we are unable to print this photo at this time.  We are gathering the necessary info to print this file now.  Please try again in about 1 minute.");
					} else {
						m.print.cart.photos = response.photos;
						$("#toPrintObj").val(escape($.toJSON(m.print.cart)));
						m.print.updateCart();
					}
				});
			},
			remove: function(photoId) {
				m.print.uriString = 'http://'+m.vars.apiServer+'/photo_cart?pathcode='+photoId+'&_method=delete&callback=?';
				$.getJSON(m.print.uriString, function(response) {
					m.print.cart.photos = response.photos;
					$("#toPrintObj").val(escape($.toJSON(m.print.cart)));
					m.print.updateCart();
				});
			},
			clear: function() {
				m.print.uriString = 'http://'+m.vars.apiServer+'/photo_cart?_method=clear&callback=?';
				$.getJSON(m.print.uriString, function(response) {
					m.print.cart.photos = response.photos;
					$("#toPrintObj").val(escape($.toJSON(m.print.cart)));
					m.print.updateCart();
				});
			},
			order: function() {
				$("#num_images").val(m.print.cart.photos.length);
				$("#return_url").val(location.href);
				$.each(m.print.cart.photos, function(i, item) {
					$("#printForm").append('<input type="hidden" name="image_'+i+'" value="'+item.info.image_url+'">');
					$("#printForm").append('<input type="hidden" name="image_height_'+i+'" value="'+item.info.image_height+'">');
					$("#printForm").append('<input type="hidden" name="image_width_'+i+'" value="'+item.info.image_width+'">');
					$("#printForm").append('<input type="hidden" name="thumb_'+i+'" value="'+item.info.thumb_url+'">');
					$("#printForm").append('<input type="hidden" name="thumb_height_'+i+'" value="'+item.info.thumb_height+'">');
					$("#printForm").append('<input type="hidden" name="thumb_width_'+i+'" value="'+item.info.thumb_width+'">');
				});
				m.print.photo.clear();
				$("#printForm").submit();
			},
			getPricelist: function() {
				m.print.uriString = 'http://'+m.vars.apiServer+'/photo_pricing?callback=?';
				$.getJSON(m.print.uriString, function(response) {
					if(response.stat==='ok') {
						m.vars.pricingCode = response.pricing_id;
						$("#event_id").val(response.pricing_id);
					}
				});
			}
		},
		cart: {},
		updateCart: function() {
			if(m.print.cart.photos) {
				if(m.print.cart.photos.length > 0) {
					$("#printCart").text('( '+m.print.cart.photos.length+' )');
				} else {
					$("#printCart").text('');
				}
			}
		},
		config: function() {
			if(location.host==='share.memeo.com') {
				m.vars.apiServer = 'share.memeo.com';
			} else if (location.host==='stage.memeo.com') {
				m.vars.apiServer = 'stage.memeo.com';
			} else if (location.host==='buzi.memeo.com'){
				m.vars.apiServer = 'buzi.memeo.com';
			} else {
				m.vars.apiServer = location.host;
			}
			$("input[type='checkbox']").click(function() {
				if($(this).hasClass('orderPrint')) {
					if($(this).is(":checked")) {
						m.print.photo.add($(this).attr('id'));
					} else {
						m.print.photo.remove($(this).attr('id'));
					}
				}
			});
			if($("#toPrintObj").val()!=='' || $("#toPrintObj").val()!=='null' || $("#toPrintObj").val()!=='%5B%5D') {
				m.print.cart.photos = $.parseJSON(unescape($("#toPrintObj").val()));
				if(m.print.cart.photos && m.print.cart.photos[0]) {
					$.each(m.print.cart.photos, function(i, item) {
						$("#"+item.pathcode).attr('checked', 'checked');
					});
				}
				m.print.updateCart();
			}
			$("a#printFormSubmit").click(function() {
				if(m.print.cart.photos.length > 0) {
					m.print.photo.order();
				}
				return false;
			});
			m.print.photo.getPricelist();
			$("input.orderPrint").show();
		},
		init: function() {
			$(document).ready(function() {
				m.print.config();
			});
		}
	}
})();
m.print.init();