$(document).ready(function(){
	if($('#body').width()<900)
	{
		$('#body').css('width', '900px');
	}
	/*$("li.lock a").click(function(){
		var link = $(this);
		$.ajax({
			type:"POST",
			url:"/ajax/validateAccess.php",
			data:"intAlbumID="+link.attr("id"),
			success:function(xml){
				if(xml == 0){
					self.location.href = link.attr("href");
				}else{
					popPrompt(link);
				}
			}
		});
		return false;
	});*/
	if($('#enter').html() != undefined)
	{
		$('#enter').ajaxForm({
			beforeSubmit: showRequest,
			dataType: 'json',
			success: showResponse
		});
	}
	$('#logout').click(function(){
		$.post('/ajax/logout.php', function(){
			self.location.href = '/';
		});
		return false;
	});
	$('#checkout_continue').click(function(){
		checkout($(this).attr('name'));
		return false;
	});
	$('#checkout_view_details').click(function(){
		getDetails($(this));
		return false;
	});
});
function showRequest(formData, jqForm, options) {
	$(':input', jqForm).attr('disabled', 'disabled');
}
function showResponse(res, status){
	if(res.status ==0)
	{
		self.location.href = res.url;
	}
	else
	{
		var msg = '';
		for(var i=0; i<res.message.length;i++)
		{
			msg += res.message[i].error + '<br />';
		}
		$('#error').html(msg).fadeIn(500);
	}
	$(':input').removeAttr('disabled');
}
/*function popPrompt(link){
	var txt = 'Enter your access code to view the album:<br />';
	txt += '<input type="hidden" id="href" value="'+link.attr("href")+'" /><input type="password" id="alertName" name="'+link.attr("id")+'" size="20" value="" style="width:200px;" /><br /><span id="error" style="color:red;">&nbsp;</span>';
	$.prompt(txt,{
		submit: mysubmitfunc,
		buttons: { Ok:true, Cancel:false }
	});
}
 function mysubmitfunc(v,m){
	if(v){
		an = m.children('#alertName');
		var pass = false;
      	if(an.val() == ""){
            pass = false;
            m.children('#error').html("Incorrect!");
      	}else{
      		m.children("#error").html('<img src="images/loading.gif" />');
      		$.ajax({
      			type:"POST",
      			url:"/ajax/validateCode.php",
      			data:"intAlbumID="+an.attr("name")+"&code="+an.val(),
      			success:function(xml){
      				if(xml == 0){
      					pass = true;
      					m.children('#error').html("Redirecting! Please wait...").css("color", "green");
      					self.location.href=m.children("#href").val();
      				}else{
      					pass = false;
      					m.children('#error').html("Incorrect!");
      				}
      			}
      		});
      	}
	}else{
		return true;
	}
}
*/
function show_more_prints(obj){
	var prints_box_top = parseInt($('#lightbox-container-prints-box').css('top')) - 70;
	$('#lightbox-container-prints-box').animate(
									{height:260, top:prints_box_top},
									200,
									function(){
										$('#prints-list div.hide').show();
										obj.parent().hide();
									});
}
function checkout(step){
	switch(step)
	{
		case '1':
			step1();
		break;
		case '2':
			step2();
		break;
		case '3':
			step3();
		break;
		case '4':
			step4();
		break;
	}
}
function step1()
{
	addStep(1);
}
function step2()
{
	$('#delivery form').ajaxSubmit({
		dataType:'json',
		success:function(res)
		{
			if(res.status == 1)
			{
				var msg = '';
				for(var i=0; i<res.message.length;i++)
				{
					//$('#'+res.message[i].id).next().html(res.message[i].error);
					msg += res.message[i].error + '<br />';
				}
				$('#error').html(msg).fadeIn(500);
			}
			else
			{
				$('#error').fadeOut(200, function(){
					addStep(2);	
				});
			}
		}
	});
}
function step3()
{
	$('#delivery form').ajaxSubmit({
		dataType:'json',
		success:function(res)
		{
			if(res.status == 1)
			{
				var msg = '';
				for(var i=0; i<res.message.length;i++)
				{
					//$('#'+res.message[i].id).next().html(res.message[i].error);
					msg += res.message[i].error + '<br />';
				}
				$('#error').html(msg).fadeIn(500);
			}
			else
			{
				$('#error').fadeOut(200, function(){
					addStep(3);
				});
			}
		}
	});
}
function step4()
{
	$.ajax({
		type:"POST",
		url:"/ajax/getPaymentURL.php",
		data:'',
		success:function(url){
			self.location.href = url;			
		}
	});
	
}
function addStep(step)
{
	$.ajax({
		type:"POST",
		url:"/ajax/addToSteps.php",
		data:'step='+step,
		success:function(){
			self.location.href = '/checkout/'+(step+1) + '/';
		}
	});
}
function getDetails(lnk)
{
	if($('#cart_full_details').html() == undefined)
	{
		$.ajax({
			type:'POST',
			url:'/ajax/getCartByPrints.php/',
			data:'',
			success:function(html){
				lnk.parent().parent().parent().parent().after(html);
			}
		});
	}
}
function updateCart(photo, prints, obj)
{
	var qty = parseInt(obj.val());
	qty = (qty<0)?0:qty;
	$.ajax({
		type:"POST",
		url:"/ajax/updateCart.php",
		data:"photo_id="+photo+'&prints['+prints+']='+qty,
		success:function(html){
			if(html==0)
			{
				$('#cart-details').load('/ajax/getCartInfo.php', function(){
					$('#checkout_continue').click(function(){
						checkout($(this).attr('name'));
						return false;
					});
					$('#checkout_view_details').click(function(){
						getDetails($(this));
						return false;
					});	
				});
				
			}
		}
	});
}
function emptyCart()
{
	$.ajax({
		type:'POST',
		url:'/ajax/emptyCart.php',
		data:'',
		success:function(res){
			self.location.href = res;
		}
	});
}