﻿$(function() {
    _recipe = new Recipe();
    _recipe.RecipeDetailPageRetrieve();
    
    function Recipe () {
        this.RecipeDetailPageRetrieve = RecipeDetailPageRetrieve;
        this.RecipeReviewsRetrieve = RecipeReviewsRetrieve;
        this.RecipeReviewCreate = RecipeReviewCreate;
        this.SetRating = SetRating;
        var RECIPEDETAIL = "#RecipeDetail";
        var RECIPEIMG = "#RecipeImg";
        var CUTINFO = "#CutInfo";
        var RATINGS = "#Ratings";
        var REVIEWS = "#Reviews";
        
        function RecipeDetailPageRetrieve()
        {
            Pork.PorkRecipe.RecipeDetailPageRetrieve(RecipeID(), 'RecipeDetailPage', RecipeDetailPageRetrieve_callback);
        }
        function RecipeDetailPageRetrieve_callback(res)
        {
            if (res.error)
                alert(res.error.Message);
            else
            {
            
                var results = res.value;
                var x = $(results).find('RecipeDetail');
                $(RECIPEDETAIL).html(x.text());
                x = $(results).find('RecipeImg');
                $(RECIPEIMG).html(x.text());
                x = $(results).find('Info');
                $(CUTINFO).html(x.text());
                x = $(results).find('Ratings');
                $(RATINGS).html(x.text());
                x = $(results).find('Analytics');
                $('body').append(x.text());
                x = $(results).find('RecipeName');
                
				var recipeDetailStr = 'Recipe Detail - ' + x.text();
				document.title = recipeDetailStr;
                $(sifrSwap);
            }
        }

        function RecipeReviewsRetrieve()
        {
            Pork.PorkRecipe.RecipeReviewsRetrieve(RecipeID(), true, RecipeReviewsRetrieve_callback);
        }
        function RecipeReviewsRetrieve_callback(res)
        {
            if (res.error)
                alert(res.error.Message);
            else
            {
                var results = res.value;
                var x = $(results).find('Ratings')
                $(RATINGS).html(x.text());
            }
        }
        
        function RecipeReviewCreate()
        {
            var Username = $('#txtName').val();
            var Title = $('#txtReviewTitle').val();
            var Comment = $('#txtComment').val();;
            
            if (Username.length > 0 && Title.length > 0 && Comment.length > 0)
            {
                var rating = 0;
                
                for (var i = 4; i >= 1; i--)
                {
                    var r = '#rating' + i;
                    if ($(r).attr('src') == '/resources/images/starOn.jpg')
                    {
                        rating = i;
                        break;
                    }
                }
                Pork.PorkRecipe.RecipeReviewCreate(RecipeID(), Username, Title, Comment, rating, RecipeReviewCreate_callback)
            }
            else
            {
                alert("Missing Fields");
            }
        }
        function RecipeReviewCreate_callback(res)
        {
            if (res.error)
                alert(res.error.Message);
            else
            {
                var results = res.value;
                                
                $('#TB_ajaxContent').html($('#divRatingConfirmation').html());
                //$('#TB_ajaxContent').html('lllllll');
                
                var x = $(results).find('Ratings')
                $(RATINGS).html(x.text());
                $("#rateButtonDiv").replaceWith("<span>Thank you</span>");
            }        
        }
        
        function SetRating(o)
        {
            var oName = $(o).attr("id");
            var starID = oName.charAt(6);
            
            //set on stars
            for (var i = 1; i <= starID; i++)
            {
                var r = '#rating' + i;
                $(r).attr('src', '/resources/images/starOn.jpg');
            }       
            
            //set off stars
            i = parseInt(starID) + 1;
            for (i; i <= 4; i++)
            {
               var r = '#rating' + i;
                $(r).attr('src', '/resources/images/starOff.jpg');
            }     
        }
                
        function RecipeID()
        {
			var retVal = -1;
            var rid = GetQueryVariable("RID");
            if (rid == '')
            {
				var windowLocation = window.location;
				var urlParts = (windowLocation+"").split("/");
				if(urlParts.length > 2)
				{
					retVal = parseInt(urlParts[urlParts.length -2]);
				}
			}
            else
            {
                retVal = parseInt(rid);
            }
            return retVal;
        }
    };
});