HEARST.ff.profile = {
	
	bookId: 0,	
	
	init: function() {	
		if( HEARST.tempUtils.readCookie("ff_action") == "edit_fragrance" ) {
			HEARST.tempUtils.eatCookie("ff_action");
		}

		if(mag_user.user_name) {
		
			$('#username').text(mag_user.user_name + "'s ");
		
			$('#saved_fragrances').empty();
			$('#my_reviews').empty();
			
			$('#profile_content').show();
			
			$.ajax({
				type: 'GET',
				url: '/api/js/FragranceBook?cachebust=' + HEARST.ff._cacheBust,
				dataType: 'json',
				error: function(x){
					// If no Fragrance book is found, create one.
					if( x.status == 404 ) {				
						$.ajax({
							 type: 'POST',   
							 url: '/api/js/FragranceCategory',
							 data: 'title=My Fragrance File',
							 dataType: 'json',
							 error: function(x){     
								if( x.status == 200 ) HEARST.ff.profile.init();	
							 },
							 success: function(data){   
								HEARST.ff.profile.init();					
							 }
						});					
					}
					
				},
				success: function(data){		
				
					$('#fragrances_tab span').append(" (" + data[0].fragrances.length + ")");
				
					if( data[0].fragrances.length == 0 ) {
						
						$('#saved_fragrances').html("<p class='no_saved' style='padding-top:10px;'><strong>You have no saved fragrances in your Fragrance File.</strong></p><p class='no_saved'>To look up Fragrances by name, go to the <a href='/fragrance-finder/index'>Fragrance Index Page</a></p>");
						
					} else {
					
						HEARST.ff.profile.bookId = data[0].id;
						
						var _id_list = new Array();
						
						$.each (data[0].fragrances, function(i, val) {
							
							_id_list.push( val.fragrance_id );
							
						});
						
						$('#saved_fragrances').append("<div id='print_option'><a href='print' class='print' target='_blank'></a><p class='print_message'>Print this page and bring it to the store</p><div class='clear'></div></div>");
						
						$.each (_id_list, function(i, val) {
												   
							$.ajax({
								type: 'GET',
								url: '/api/js/Fragrance/' + val,
								dataType: 'json',
								error: function(e){
									//
								},
								success: function( frag ){	
									
									var _frag_string = "<div class='result _" + i + "'>";
									_frag_string += "<a href='" + frag.url_name + "'><img src='" + frag.main_image.small_url + "' alt='" + frag.title + "' class='thumb'></a>";
									_frag_string += "<div class='delete_container _" + i + "' style='position:relative;'><p class='delete'><a href='javascript:HEARST.ff.profile.deleteConfirmation(" + val + ", " + i +")'>Delete &#187;</a></p></div>";
									_frag_string += "<h2><a href='" + frag.url_name + "'>" + frag.title + "</a> <span>|</span> " + frag.fragrance.house + "</h2>";
									_frag_string += "<p class='notes'>" + frag.fragrance.family.anon + " | " + frag.fragrance.salesbite + "</p>";
									_frag_string += "<p>" + frag.fragrance.storytext + "</p>";
									_frag_string += "<div class='clear'></div></div>";		
									
									$('#saved_fragrances').append( _frag_string );
								
								}
								
							});							
							
						});
						
					}
					
					$.ajax({
						type: 'GET',
						url: '/api/js/FragranceComment?ur_id=' + mag_user.ur_id + '&cachebust=' + HEARST.ff._cacheBust,
						dataType: 'json',
						error: function(x){
							if( x.status == 404 ) {
								$('#my_reviews').html("<p class='no_saved' style='padding-top:10px;'><strong>You have left no Fragrance reviews.</strong></p><p class='no_saved'>To look up Fragrances by name, go to the <a href='/fragrance-finder/index'>Fragrance Index Page</a></p>");
								$('a#reviews_tab span').empty();
								$('a#reviews_tab span').append("REVIEWS (0)");
							}
						},
						success: function(data){	
						
							// Update tab
							$('a#reviews_tab span').empty();
							$('a#reviews_tab span').append("REVIEWS (" + data.length + ")");
						
							$.each (data, function(i, val) {
												   
								// *KLUDGE* comment_title is being used as the rating field
								var _rating = val.comment_title;
												   
								var _review_string = "<div class='review _" + i + "'>";
								_review_string += "<h2><a href='javascript:HEARST.ff.profile.getFragranceLink(" + val.fragrance_id + ")' class='fragrance'>" + val.fragrance_title + "</a></h2>";
								_review_string += "<ul class='rating'><li class='off'><li class='off'><li class='off'><li class='off'><li class='off'></li></ul>";
								_review_string += "<p>" + val.comment_text + "</p>";
								_review_string += "<div class='clear'></div>";
								_review_string += "</div>";
							
								$('#my_reviews').append( _review_string );
							
								// Set ratings
								if( (_rating == 0) || (_rating == null) || (_rating == "undefined") ) {
									$('.review._' + i + ' ul').remove();
									$('.review._' + i + ' p.by').css('padding-top','2px');
									_rating = 0;
								} else {
									for( j = 0; j < _rating; j++ ) {
										$('.review._' + i + ' ul li:eq(' + j + ')').attr('class','on');			
									}
								}								
							
							});						
						
						
						}
						
					});
					
				}
				
			});
		
			$('a#reviews_tab').bind('click', function(){ 
				$('#saved_fragrances').hide();
				$('#my_reviews').show();
			});
			
			$('a#fragrances_tab').bind('click', function(){ 
				$('#my_reviews').hide();
				$('#saved_fragrances').show();
			});
			
		} else {
		
			$('#profile_content').html("<div id='message'><p>You must <a href='/login/'>Log In</a> to view your Fragrance File.</div></p>");
			$('#profile_content').show();
		
		}
	
	},
	
	deleteConfirmation: function( id, num ) {
	
		var _confirm_string = "<div class='confirm_box' style='width:280px;'><p style='width:280px;'>Are you sure you want to delete this fragrance?</p><p style='text-align:center;margin-top:8px;width:280px;'><a href='javascript:HEARST.ff.profile.deleteFragrance(" + id + "," + num + ")'>Yes</a> &nbsp;&nbsp;&nbsp;<a href='javascript:HEARST.ff.profile.deleteCancel(" + num + ")'>No</a></p></div>";

		$('.delete_container._' + num).append( _confirm_string );		
		
	},
	
	deleteFragrance: function( id, num ) {
		
		$.ajax({
			type: 'POST',
			url: '/api/js/FragranceBook/' + HEARST.ff.profile.bookId + '?cachebust=' + HEARST.ff._cacheBust,
			dataType: 'json',
			data: 'delete_fragrance_id=' + id + '&_method=PUT',
			error: function(x){
				//alert(x);
				if( x.status == 200 ) {
					
					$('.result._' + num).fadeOut("fast", function () {
					   $('.result._' + num).remove();		
					   if($('.result').length == 0) {
						   $('#fragrances_tab span').html("FRAGRANCES");
						   HEARST.ff.profile.init();
					   } else {
						   $('#fragrances_tab span').html("FRAGRANCES (" + $('.result').length + ")");
					   }						  
					});	
					
				}
			},
			success: function(data){	
				
				$('.result._' + num).fadeOut("fast", function () {
				   $('.result._' + num).remove();		
				   if($('.result').length == 0) {
					   $('#fragrances_tab span').html("FRAGRANCES");
					   HEARST.ff.profile.init();
				   } else {
					   $('#fragrances_tab span').html("FRAGRANCES (" + $('.result').length + ")");
				   }						  
				});
				
			}
		});
		
	},
	
	deleteCancel: function( num ) {
	
		$('.result._' + num + ' .confirm_box').fadeOut('fast');
	
	},
	
	getFragranceLink: function( id ) {
		
		$.ajax({
			type: 'GET',			
			url: '/api/js/Fragrance/' + id,
			dataType: 'json',				
			error: function( x ){
				// Error message
			},
			success: function( data ){
			
				window.location = "/fragrance-finder/" + data.url_name;
				
			}
			
		});
		
	}
	
}