$(document).ready(function(){

	// Fancybox
	$("a.fancybox").fancybox();


	// Show/Hide Comments
	$('a#show-comments').live('click', function() {
		$('.all-comments').slideToggle();
		return false;
	});
	
	// New Comment
	$('#new-comment').live('click', function() {
		var site_url		= $('#site_url').val();
		var type 			= $(this).attr('alt');
		var status			= $(this).attr('loggedin');
		var entry_id		= $('#entry_id').val();
		var user_id			= $('#user_id').val();
		var comment_content = $('#comment_content').val()
		if (status != '1') {
			alert('You must be logged in to leave a comment.');
			return false;
		}
		var dataString = 'type=' + type;
		dataString += '&post_id=' + entry_id;
		dataString += '&user_id=' + user_id;
		dataString += '&comment_content=' + comment_content;
		$('#error-message').hide();
		$('#success-message').hide();
		$('#loading-small').fadeIn('fast', function() {
			$('#loading-small').animate({opacity: 1.0}, 1000, function() {
				$.ajax({
				    url: site_url + 'comments/new_comment/',
					type: "POST",
					data: dataString,
					cache: false,
					success: function(ret){
						if (ret == '1')
						{
							$('#loading-small').fadeOut(500);
							$('#error-message').fadeIn(500);
						} else {
							$('#comment_content').val('');
							$('#loading-small').fadeOut(500);
							$('#success-message').fadeIn(500);
						}
					}
				});
			});
		});
		return false;
	});
	
	// Comment Pagination
	$('a.pagination').live('click', function(){
		var height 			= $('#comment-window').height();
		$('#comment-window').css('height', height);
		
		var site_url		= $('#site_url').val();
		var offset			= $(this).attr('alt');
		var entry_id		= $('#entry_id').val();
		var type 			= $('#comment_type').val();
		var limit			= $('#limit').val();
		var comment_order	= $('#comment_order').val();
		if (type == '') return false;
		var dataString = 'type=' + type;
		dataString += '&post_id=' + entry_id;
		dataString += '&offset=' + offset;
		dataString += '&limit=' + limit;
		dataString += '&comment_order=' + comment_order;
		var whereTo = $('.all-comments');
		$.scrollTo( whereTo, 500 );
		$('#comment-replace').fadeOut('fast', function() {
			$('#comment-window').css('backgroundImage', 'url("'+site_url+'assets/images/loading-large.gif")');
			$.ajax({
			    url: site_url + 'comments/get_comments_ajax/',
				type: "POST",
				data: dataString,
				cache: false,
				success: function(ret){
					$('#comment-replace').html(ret);
					var new_height = $('#comment-replace').height();
					$('#comment-window').animate({height: new_height}, 500, function() {
						$('#comment-replace').fadeIn('fast');
						$('#comment-window').css('backgroundImage', 'none');
					});
				}
			});
		});
		return false;	
	});
	
	// Star Ratings
	$('a.star').live('click', function(){
		var rating		= $(this).attr('rating');
		var cur_rating  = $('a.star-selected').attr('rating');
		if (rating == cur_rating) return false;
		
		var site_url	= $('#site_url').val();
		var rating_int	= rating * 1;
		var user_id		= $(this).parent().parent().attr('alt');
		var recipe_id	= $(this).parent().parent().attr('id').replace('recipe-', '');
		var dataString  = 'rating=' + rating;
		dataString     += '&user_id=' + user_id;
		dataString     += '&recipe_id=' + recipe_id;
		
		$('#content .star-rating li#current-rating-' + recipe_id).css('background', 'none');
		$('#content .star-rating li#current-rating-user-' + recipe_id).css('width', rating_int * 14);
		$('#rating_text_' + recipe_id).html('Your Rating');
		$.ajax({
			url: site_url + 'ratings/update_rating/',
			type: 'POST',
			data: dataString
		});
		return false;
	});
	
	// Mark as Favorite
	$('a.mark-favorite').live('click', function(){
		var post_id 	= $(this).attr('id').replace('favorite-', '');
		var user_id		= $(this).attr('lang');
		var type 		= $(this).attr('alt');
		var site_url	= $('#site_url').val();
		var dataString  = 'post_id=' + post_id;
		dataString     += '&user_id=' + user_id;
		dataString     += '&type=' + type;
		if (user_id == '')
		{
			alert('You must be logged in to mark a favorite.');
			return false;
		}
		$('#favorite-' + post_id).fadeOut('fast', function() {
			$.ajax({
				url: site_url + 'myweber/update_favorite/',
				type: 'POST',
				data: dataString,
				success: function(ret){
					if (ret == 'a')
					{
						$('#favorite-' + post_id).addClass('favorite');
						$('#favorite-' + post_id).html('<strong>Marked as favorite</strong>');
					} else {
						$('#favorite-' + post_id).removeClass('favorite');
						$('#favorite-' + post_id).html('Mark as favorite');	
					}
					$('#favorite-' + post_id).fadeIn('fast');
				}
			});
		});
		return false;
	});
	
	// Email Functions
	$("a#email-recipe, a#email-shopping-list, a.share-email").live('click', function() {
		
		// for favorites page individual share links
		var message = $(this).attr('alt');
		if (message) $('#email_share_text').val(message);
		
		var href = $(this).attr('href');
		$.fancybox({
			'padding'		: 15,
			'autoScale'		: false,
			'transitionIn'	: 'none',
			'transitionOut'	: 'none',
			'width'			: 592,
			'height'		: 400,
			'type'			: 'iframe',
			'href'			: href
		});
		return false;
	});

	$('a#close-email').live('click', function() {
		parent.$.fancybox.close();
	});
	
	// Facebook Sharer
	$('a#share_on_facebook').live('click', function() {
		var href = $(this).attr('href');
		window.open(href, 'facebook', 'width=550,height=400,scrollbars=1');
		return false;
	});
	
	// Grill Compare
	$(".compare-button").live('click',function() {
		var grill = $(this).attr('title');
		var n = 1;
		var url = '/explore/compare/' + grill + '/';
		$.each($("input[name='compare_model[]']:checked"), function() { 
			if ($(this).val() != grill) 
			{
				url += $(this).val() + '/';
				n++;
			}
		});
		if (n > 2) {
			alert("You may only compare 2 models at a time.");
			return false;
		}
		window.location = url;
		return false;
	});

});

// Populate Search Store Results
function populate_store_search(all) {
	var site_url = $('#site_url').val();
	// Get Search results from API
	$.ajax({
    	url: site_url + 'search/store/1',
		type: "POST",
		success: function(ret){
			obj = $.parseJSON(ret);
			if (obj !== null)
			{
				var results = '';
				var i = 0;
				$.each(obj.Products, function(k, v) {
					if (all == 'all') {
						results += '<li><a href="'+v.product_url+'">' + v.Product_title + '</a></li>';
					} else {
						if (i < 5) {
							results += '<li><a href="'+v.product_url+'">' + v.Product_title + '</a></li>';
						}
					}
					i += 1;
				});
				$('ul#store-results').html(results);
				$('#store-total').html(i);
				if (i < 5 || all == 'all') {
					$('#store-count').html(i);
				} else {
					$('#view-all-store').show();
				}
			} else {
				var results = '<li>No Results.  Please try again.</li>';
				$('ul#store-results').html(results);
				$('#store-count').html('0');
				$('#store-total').html('0');
			}
		}
	});
}
