
var lightbox_delete_url = null;

$(document).ready(function() {

	var price = 0;
	var regularPrice = $('#licenseRegular').html();
	var extendedPrice = $('#licenseExtended').html();
	var deposit = $('#deposit').html();

	function checkPrice(){

		if ($(".license:checked").val() == 'regular') { price=regularPrice;}
		else if ($(".license:checked").val() == 'extended') { price=extendedPrice; }
		

		
		var type = $('#fileType').val();
		
		if ($(".payment:checked").val() == 'buynow') { 

			if(type == 1){
				price=2.80;
			} else if(type == 2){
				price=4.40;
			} else if(type == 5){
				price=3.30;
			} else {
				price=price*6.00;
			}
			
			if ($(".license:checked").val() == 'extended'){
				price = price*6.00;
			}
		}
		
		if ($(".payment:checked").val() == 'deposit') { price=price*1.00;}
		
		
		
		$("#price").html('$'+price.toFixed(2));
		$("#hiddenPrice").val(price.toFixed(2));
		
		Cufon.replace('#price', { fontFamily: 'Breuer' });
		checkDeposit();
	}
	
	function checkDeposit(){
		if(price>deposit && $(".payment:checked").val() == 'deposit'){
			$("#checkout").attr('disabled','disabled');
			$('#purchaseInfo').html('Your balance: $'+deposit+'. Make <a href="/order/deposit">deposit</a> to buy this file.');
		} else {
			$("#checkout").removeAttr('disabled');
			$('#purchaseInfo').html('');
		}
	}
	
	function isLogged(){
		if($('#deposit').html()!=null){
			return true;
		} else {
			$("#checkout").attr('disabled','disabled');
			return false;
		}
	}
	
	$(".payment, .license").change(function(){
		checkPrice();
		isLogged();
	});
	
	checkPrice();
	isLogged();
	
	// confirmation
	$("#checkout").click(function(){
		if($(".payment:checked").val() == 'deposit'){
			$(".filePreview .confirmation").show();
			return false;
		} else { 
			$("#paymentForm").attr('action', '/order/pay');
			return true;
		}
	});
	
	$("#proceed").click(function(){
		$("#paymentForm").submit();
	})
	
	$("#hideConfirm").click(function(){
		$(".confirmation").hide(500);
	});
	
	//$("select, input:checkbox, input:radio, input:file").uniform();
	
	$('.sort').mouseover(function(){
		$('.sort li').show();
	}).mouseout(function(){
		$('.sort li').hide();
		$('.sort li:first-child').show();
	});
	
	$('.perpage').mouseover(function(){
		$('.perpage li').show();
	}).mouseout(function(){
		$('.perpage li').hide();
		$('.perpage li:first-child').show();
	});
	
	$('.type').mouseover(function(){
		$('.type li').show();
	}).mouseout(function(){
		$('.type li').hide();
		$('.type li:first-child').show();
	});
	
	// thumbs hover
	$("#loginPopup").click(function(){
		$('#loginWrapper').css('opacity', .8);
		$("#loginWrapper").show();	
		$('.loginForm').css('opacity', 1);
	});
	
	$(".thumbs li").mouseover(function(){
		$(".titleBox", this).show();
	}).mouseout(function(){
		$(".titleBox", this).hide();
	});
	
	$("div.getInfo").hide();
	$("#buyItem").click(function(){
		$("div.getInfo").toggle('slow');
		return false;
	});
	
	$(".changeSortType").change(function()
	{
		var category = $(".changeSortType").attr('name');
		var sort = $(".changeSortType").val();
		var type = $(".changeSortType").attr('id');
		window.location='/'+type+'/'+category+'/'+sort;
	});
	
	$(".changeTagSortType").change(function()
	{
		var id = $(".changeTagSortType").attr('name');
		var sort = $(".changeTagSortType").val();
		window.location='/tag/'+id+'/'+sort;
	});
	
	$(".changePhotosPerPage").change(function()
	{
		var category = $(".changePhotosPerPage").attr('name');
		var perPage = $(".changePhotosPerPage").val();
		var type = $(".changePhotosPerPage").attr('id');
		window.location='/'+type+'/'+category+'/0/'+perPage;
	});
	
	// lightbox

		function reloadSelect(){
			$.getJSON('/lightbox/list', function(data){
			
				$("#selectLightbox").removeOption(/./);
				
				for (var i = 0; i <= data.length; i++) {
					$("#selectLightbox").addOption(data[i].id, data[i].name);
				}
				
				if (data.length == 0) {
					$("#addImage2Lightbox").attr('disabled', true).css('cursor', 'regular');
				}
			});
		}		
		
		
		$(".lightboxControl").hide();
		$(".buttonLightbox").click(function(){
			reloadSelect();
			$(".lightboxControl").toggle(400);
			if (isLogged()) {
				return false;
			}
		});
		$(".lightboxControl #createForm").hide();
		
		$(".lightboxControl #showCreate").click(function(){
			$(".lightboxControl #createForm").toggle(400);
			return false; 
		});
		
		$("#addImage2Lightbox").click(function(){
			
			var lightboxID = $("#selectLightbox").val();
			var fileID = $("#fileID").val();
			
			$.ajax({
				   type: "POST",
				   url: "/lightbox/addImage",
				   data: "fileID="+fileID+"&lightboxID="+lightboxID,
				   success: function(msg){
				     	$(".lightboxControl p.info").html('Success.');
						$(".lightboxControl p.info").show();
				   },
				   error: function(msg){
				     	$(".lightboxControl p.error").html('Error.');
						$(".lightboxControl p.error").show();
				}
			});
		});

		$("#createLightbox").click(function(){
			
			var newLightboxName = $(".lightboxControl #lightboxName").val();
			
			if(newLightboxName.length == 0)
			{
				$(".lightboxControl p.error").prepend('Error.');
				$(".lightboxControl p.error").show();
				
				setTimeout(function() { 
					$('.lightboxControl p.error').fadeOut(); 
				}, 2000);

			}
			else {
				$.ajax({
				   type: "POST",
				   url: "/lightbox/create",
				   data: "name="+newLightboxName,
				   success: function(msg){
				     	$(".lightboxControl p.info").html('Lightbox created.');
						$(".lightboxControl p.info").show();
				   },
				   error: function(msg){
				     	$(".lightboxControl p.error").html('Error.');
						$(".lightboxControl p.error").show();
				   }
				 });
				 
				 reloadSelect();
				 
				setTimeout(function() { $('.lightboxControl p.error').fadeOut(); }, 3000);
				setTimeout(function() { $('.lightboxControl p.info').fadeOut(); }, 3000);
			}
		});	

		/**
	 * Generic qTip executed on mouse over 
	 **/
	$('a.jsQTip').click(function(){
		content_from_title_element = $(this).attr('help');							 
		$(this).qtip({
					content: content_from_title_element, 
			       position: {
				  		corner: {
						 tooltip: 'bottomLeft', // Use the corner...
						 target: 'topRight' // ...and opposite corner
				  		}
			       },
			       show: {
					  delay: 0,	   
					  when: false, // Don't specify a show event
					  ready: true // Show the tooltip when ready
			       },
				   hide: {
					  delay: 4000
				   },
			       style: {
					  border: {
						 width: 3,
						 radius: 7
						},
					  padding: 10,
					  textAlign: 'left',
					  tip: true, // Give it a speech bubble tip with automatic corner detection
					  name: 'light' // Style
			       }
			    });
	});
	
	
	
	/** setup confirmation dialog **/
	$( "#dialog-confirm" ).dialog({
			resizable: false,
			height:140,
			modal: true,
			autoOpen: false,
			buttons: {
				"Yes": function() {
					document.location.href = lightbox_delete_url;
				},
				"No": function() {
					$( this ).dialog( "close" );
				}
			}
		});		
		
	
	/**
	 * Lightbox delete 
	 */
	$('a.buttonDelete').click(function(e){
		e.preventDefault();
		e.stopPropagation();
		
		lightbox_delete_url = $(this).attr('href');
		$( "#dialog-confirm" ).dialog('open');
	});
	

});


