﻿$(document).ready(function () {
//console.log("recipes.js loaded");
    $('.selectall_checkbox').hide();

    //print button display binds

    $('.printlink a').live('click', function () {
        $('.print_wrapper').fadeIn(500);

        $('.full_link').attr('href', '/print_recipe.aspx?id=' + activerecipe);
        $('.three_link').attr('href', '/print_recipethreecard.aspx?id=' + activerecipe);
        $('.four_link').attr('href', '/print_recipefourcard.aspx?id=' + activerecipe);


        return false;

    });

    $('.close_print').live('click', function () {
        $('.print_wrapper').fadeOut(500);

    })

    //comment displays event binds
    $('.commentlink').live("click", function () {

        var hascomments = $('.comments').is("div");
        if (!hascomments) {

            var parentEls = $(this).closest("#drink");

            parentEls.after("<div class='comments first'>Loading...</div>");

            pageTracker._trackEvent('Participation', 'viewedComments', pageTrackingTitle + '/RecipeAddComment', null);

            var comelem = parentEls.next(".comments");

            comelem.css('display', 'block');


            comelem.css('display', 'block');

            $.get("dynamicContent/GetComments.aspx", { id: activerecipe, t: commenttype }, function (comments) {

                comelem.html(comments);

                comelem.find('textarea').one("click", function () { $(this).val(''); });
                buildtransparency();
                comelem.hide().show();
            });


        }

        else {
            //alert('already has comments');
        }
        return false;

    });

    var array_form_fridge_history;
    var fridge_form = document.getElementById("recipe_filter");

    $('#recipe_filter input').bind("click",
	function () {
	    var clicker = 0;



	    $('#search_text').val('');
	    rsrhval = $('#search_text').val();
	    ingredstring = '';

	    array_form_fridge_history = new Array(fridge_form.elements.length);
	    $('.comments').slideUp(500, function () { $(this).remove(); })
	    for (i = 0; i < fridge_form.elements.length; i++) {
	        if (fridge_form.elements[i].type == 'checkbox') {
	            if (fridge_form.elements[i].checked == true) {
	                clicker = clicker + 1;

	                array_form_fridge_history[i] = true;
	                ingredstring = ingredstring + fridge_form.elements[i].value + ',';
	            }
	            else if (fridge_form.elements[i].checked == false) {
	                array_form_fridge_history[i] = false;
	            }
	        }
	    }

	    if (clicker > 0) {
	        $('.reset_checkbox').show();
	    }
	    else if (clicker == 0) {
	        $('.reset_checkbox').hide();
	        $('.selectall_checkbox').show();

	        for (var ii = 0; ii < fridge_form.elements.length; ii++) {
	            array_form_fridge_history[ii] = false;
	            if (fridge_form.elements[ii].type == 'checkbox') {
	                fridge_form.elements[ii].checked = false;
	            }
	        }

	        ingredstring = '';
	    }

	    /*
	    if (ingredstring == '') 
	    {									
	    $('.ingredient_list input').attr('checked',true);
	    }
	    */

	    ingredstring = ingredstring.substring(0, (ingredstring.lastIndexOf(',')));

	    $.get("dynamicContent/recipleList.aspx", { s: $('#s_value').val(), i: ingredstring },
		function (response) {
		    $('#dynamic_drinks').html(response);

		    for (i = 0; i < array_form_fridge_history.length; i++) {
		        if (array_form_fridge_history[i] == true) {
		            fridge_form.elements[i].checked = true;
		            pageTracker._trackEvent('Participation', 'checkedbxRecipe', pageTrackingTitle + '/FridgeCheckbox', null);
		        }
		        else {
		            fridge_form.elements[i].checked = false;
		            pageTracker._trackEvent('Participation', 'checkedbxRecipe', pageTrackingTitle + '/FridgeUnCheckbox', null);
		        }
		    }

		    rebuilddrinktrans();
		    buildtransparency();
		    return false;
		});

	    $.post("DynamicContent/UserEngagement.aspx", { categoryscore: '2', activity: 'Search' });

	    if (jQuery.browser.msie) {
	        return false;
	    }

	});


    $('.drink_item').live("click", function () {
        $('.transparent_class_drink').css('visibility', 'hidden');
        $(this).siblings().css('visibility', 'visible');

        activerecipe = $(this).attr('id').replace('drink', '');
        $('.comments').slideUp(500, function () { $(this).remove(); })

        pageTracker._trackEvent('Participation', 'viewedmoreRecipe', pageTrackingTitle + '/Thumbnail/' + activerecipe, null);

        $('#drink').fadeOut(1000, function () {
            $.get("dynamicContent/recipeDetails.aspx", { id: activerecipe },
				function (response) {
				    response = $(response).find('#drink').html();
				    $('#drink').html(response).fadeIn(1000);
				    buildtransparency();
				    reinitrating();
				});
        });
    });

    $('.drink_item a').live("click", function () {
        $('.transparent_class_drink').css('visibility', 'hidden');
        $(this).closest('.drink_item').siblings().css('visibility', 'visible');
        pageTracker._trackEvent('Participation', 'viewedmoreRecipe', pageTrackingTitle + '/Thumbnail', null);
        activerecipe = $(this).closest('.drink_item').attr('id').replace('drink', '');
        $('.comments').slideUp(500, function () { $(this).remove(); })
        $('#drink').fadeOut(1000);
        $('#drink').fadeOut(1000, function () {
            $.get("dynamicContent/recipeDetails.aspx", { id: activerecipe },
				function (response) {
				    response = $(response).find('#drink').html();
				    $('#drink').html(response).fadeIn(1000);
				    buildtransparency();
				    reinitrating();
				});
        });




        return false;


    });
    var loc = document.location.href;
    x = loc.indexOf('p=');
    if (loc == -1) var page = 1;
    else {
        var page = loc.substring(x + 2);
    }


    $('.drink_page a').live("click", function () {
        page = $(this).attr('href');
        pageTracker._trackEvent('Participation', 'viewedmoreRecipe', pageTrackingTitle + '/NextPage', null);
        x = page.indexOf('p=');
        page = page.substring(x + 2);
        $('#dynamic_drinks').html('<div class="loader"></div>');

        $.get("dynamicContent/recipleList.aspx", { s: $('#s_value').val(), i: ingredstring, p: page, rsrh: rsrhval },
		function (response) {

		    $('#dynamic_drinks').html(response);


		    rebuilddrinktrans();
		    buildtransparency();
		    return false;
		});


        return false;
    });

    $('.popdrink_row').live("click", function () {

        activerecipe = $(this).attr('id').replace('popdrink', '');

        $('.comments').slideUp(500, function () { $(this).remove(); })


        $('#drink').fadeOut(1000, function () {
            $.get("dynamicContent/recipeDetails.aspx", { id: activerecipe },
				function (response) {
				    response = $(response).find('#drink').html();
				    $('#drink').html(response).fadeIn(1000);
				    buildtransparency();
				    reinitrating();
				});


        });



        return false;
    });

    $('.featured_recipe_hdr').bind("click", function () {


        activerecipe = $(this).attr('id').replace('featuredDrink', '');

        $('.comments').slideUp(500, function () { $(this).remove(); })


        $('#drink').fadeOut(1000, function () {
            $.get("dynamicContent/recipeDetails.aspx", { id: activerecipe },
				function (response) {
				    response = $(response).find('#drink').html();
				    $('#drink').html(response).fadeIn(1000);
				    buildtransparency();
				    reinitrating();
				});


        });



        return false;
    });



    $('#search_text').one("click", function () {
        $(this).val('')
    });
    var rsrhval = ''
    $('.sorter a').live('click', function () {

        if (!$(this).hasClass('active')) {
            $('.sorter a').removeClass('active');
            $(this).addClass('active');

            $('#s_value').val($(this).attr('id'));
            //alert($('#s_value').val())

            switch (id) {
                case 0:

                    $('#celeb_nav').attr('src', 'images/celebrations/nav_landing_cal_on.gif');

                    break;
                case 1:
                    $('#celeb_nav').attr('src', 'images/celebrations/nav_landing_cheer_on.gif');

                    break;
                case 2:
                    $('#celeb_nav').attr('src', 'images/celebrations/nav_landing_view_on.gif');

                    break;
            }

            switch ($(this).attr('id')) {
                case 'm':

                    pageTracker._trackEvent('Participation', 'sortedRecipe', pageTrackingTitle + '/MostRecentSort', null);

                    break;
                case 'r':

                    pageTracker._trackEvent('Participation', 'sortedRecipe', pageTrackingTitle + '/RatingsSort', null);

                    break;

                case 'a':

                    pageTracker._trackEvent('Participation', 'sortedRecipe', pageTrackingTitle + '/A-Zsort', null);

                    break;
            }


            $.get("dynamicContent/recipleList.aspx", { p: $('#p_value').val(), s: $('#s_value').val(), i: ingredstring, rsrh: rsrhval },
			function (response) {

			    $('#dynamic_drinks').html(response);
			    rebuilddrinktrans();
			    buildtransparency();

			});

            return false;
        }

        else {
            return false;
        }



    });


    $('#search_recipe').submit(function () {

        rsrhval = $('#search_text').val();
        pageTracker._trackEvent('Participation', 'searchedRecipe', pageTrackingTitle + '/Search', null);
        $.get("dynamicContent/recipleList.aspx", { p: '1', rsrh: rsrhval, s: $('#s_value').val() },
			function (response) {
			    if (response.length > 25) {
			        $('#dynamic_drinks').html(response);
			        rebuilddrinktrans();
			        buildtransparency();
			    }

			    else {
			        $('#dynamic_drinks').html('No results found');
			        buildtransparency();
			    }



			});
        $.post("DynamicContent/UserEngagement.aspx", { categoryscore: '2', activity: 'Search' });
        return false;



    });

    $('.btn_go').bind("click", function () {
        $('#search_recipe').submit();
        return false;
    });

    $('.selectall_checkbox').bind("click", function () {
        pageTracker._trackEvent('Participation', 'filteredRecipe', pageTrackingTitle + '/SelectAll', null);
        var fridge_form = document.getElementById("recipe_filter");
        for (var ii = 0; ii < fridge_form.elements.length; ii++) {
            if (fridge_form.elements[ii].type == 'checkbox') {
                fridge_form.elements[ii].checked = true;
            }
        }

        $('.selectall_checkbox').hide();
        $.get("dynamicContent/recipleList.aspx", { p: $('#p_value').val(), s: $('#s_value').val(), i: '' },

				function (response) {
				    $('#dynamic_drinks').html(response);
				    rebuilddrinktrans();
				    buildtransparency();
				});
    });

    //filters
    $('.reset_checkbox').bind("click", function () {
        $('.selectall_checkbox').show();
        pageTracker._trackEvent('Participation', 'filteredRecipe', pageTrackingTitle + '/DeSelectAll', null);
        // $('#recipe_filter')[0].reset();

        $('.comments').slideUp(500, function () { $(this).remove(); })
        ingredstring = '';
        $('#search_text').val('');
        rsrhval = $('#search_text').val();
        $.get("dynamicContent/recipleList.aspx", { p: $('#p_value').val(), s: $('#s_value').val(), i: ingredstring },

				function (response) {
				    $('#dynamic_drinks').html(response);
				    rebuilddrinktrans();
				    buildtransparency();
				});

        return false;

    });

    /*
    $('#recipe_filter input').bind("click", function() 
    {
    alert("debugM filter input");
    });
    */

    $('.flag_comment').live("click", function () { //binds flag functionality to all flag links

        var commentid = $(this).attr('id');
        commentid = commentid.replace('commentid', '');
        var el = $(this);
        //alert(commentid + ' is reported')
        $.post("dynamicContent/ReportAbuse.aspx", { id: commentid, t: commenttype },
  		function (data) {

  		    el.html(data)
  		});
        return false;
    });


    $('.drink_stars img').live('click', function () {
        //alert('rating is: '+$(this).attr('class'));
        $(this).parent().children().fadeOut().remove();

        pageTracker._trackEvent('co-creation', 'ratedRecipe', pageTrackingTitle + '/Rating', null);

        $.post("dynamicContent/recipeVote.aspx", { id: activerecipe, r: $(this).attr('class') },
					function (data) {
					    data = data + '<span>Thanks for voting</span>';
					    $('.drink_stars').html(data).hide().fadeIn(1000);
					    //$('.nowplaying').append(data);
					    //$('.nowplaying .rating').addClass('rating2').removeClass('rating').append('thanks').hide().fadeIn(1500);
					    $('.drink_stars').addClass('drink_stars2').removeClass('drink_stars');

					});
    });



    function reinitrating() {

        $('.drink_stars img').bind('mouseenter', function () {
            $(this).attr("src", "images/recipe/star_on.gif");
            $(this).prevAll().attr("src", "images/recipe/star_on.gif");
            $(this).nextAll().attr("src", "images/recipe/star_off.gif");
        });
    }

    reinitrating();
    rebuilddrinktrans();

    function rebuilddrinktrans() {
        $(".transparent_class_drink").remove();
        $(".unit_drink").each(function (i) {


            $(this).append("<div class='transparent_class_drink'></div>");
            var test = new Array();
            test[i] = $(this).height();
            var test2 = new Array();
            test2[i] = $(this).width();
            $('.transparent_class_drink').each(function (i) {
                $(this).css('height', test[i], 'width', test2[i]);
            });
            $(document).pngFix();
        });
    }


});

$(window).load(
    function () {

        /**/

        if (jQuery.browser.msie) {

            $('.sharp').fadeIn(1000, function () {
                $('.crown_content').css('display', 'block');
                buildtransparency();
                buildrecipetrans();

            });
        }

        else {

            $('.sharp').fadeIn(1000, function () {

                $('.crown_content').fadeIn(1000,
					function () {
					    buildtransparency();
					});
                buildtransparency();
                buildrecipetrans();
            });
        }

    }
);



function buildrecipetrans() {

    $(".transparent_class_drink").remove();
    $(".unit_drink").each(function (i) {


        $(this).append("<div class='transparent_class_drink'></div>");
        var test = new Array();
        test[i] = $(this).height();
        var test2 = new Array();
        test2[i] = $(this).width();
        $('.transparent_class_drink').each(function (i) {
            $(this).css('height', test[i], 'width', test2[i]);

        });
    });
}
