// JavaScript Document

function autocomplete() {
    // Pass db value into both below using array. Take out string and replace with var.
    if ($(".keyword").length > 0) {

        // autocomplete
        $(".keyword").autocomplete("/Ajax/Tips/Default.aspx", {
            width: 210, matchContains: true, scroll: true, minChars: 1, selectFirst: false
            //max: 4, highlight: false, multiple: true, multipleSeparator: " ", scrollHeight: 300, minChars: 2
        });
        // autocomplete short
        $(".keyword_short").autocomplete("/Ajax/Tips/Default.aspx", {
            width: 138, matchContains: true, scroll: true, minChars: 1, selectFirst: false
            //max: 4, highlight: false, multiple: true, multipleSeparator: " ", scrollHeight: 300, minChars: 2
        });

        // clear init value
        $(".keyword_input").click(function() {
            init_val = "e.g. store leftovers";
            current_val = $(this).val();
            if (current_val == init_val) {
                $(this).val("");
            }
        });
    }

    // clear init value - for best kept only
    $(".keyword_input_bestkept").click(function() {
        init_val = "e.g. potatoes";
        current_val = $(this).val();
        if (current_val == init_val) {
            $(this).val("");
        }
    });
}

function best_kept_ajax_tabswitcher() {
    if ($("div.search_small ul li").length > 0) {
        // ajax load of switcher [best_kept_product page]
        $("div.search_small ul li").click(function() {
            class_identifier_b4load = $(this).attr("class");
            $.get("refine_module.html", function(data) {
                //alert("Data Loaded: " + data);
                $(".search_small ul").remove();
                $(".search_small form").remove();
                $(".search_small").attr("id", "advice_refine").removeClass("search_small").addClass("ajax");
                // append and set switcher
                $("div#advice_refine").append(data);
                switch_tab(class_identifier_b4load);
                $("ul.gtlw_tabs li").click(function() {
                    class_identifier = $(this).attr("class");
                    switch_tab(class_identifier);
                })
                $("ul.categories li li").hover(
				function() {
				    $(this).addClass("hover")
				}, function() {
				    $(this).removeClass("hover")
				})
                autocomplete();
            });
        });
    }
}

function switch_tab(state){
	if (state == "fresh")
	{
		$("#cooked_food_tc").hide();
		$("#fresh_food_tc").show();
		$("ul.gtlw_tabs").removeClass("d_green").addClass("l_green");
	}else{
		$("#cooked_food_tc").show();
		$("#fresh_food_tc").hide();	
		$("ul.gtlw_tabs").removeClass("l_green").addClass("d_green");
	}
	
}

function best_kept_tabswitcher(){
	// tab switcher [best kept landing]
	$("#categories.hidden").hide();
	$("ul.gtlw_tabs").show();
	if ($("ul.gtlw_tabs").hasClass("d_green") == true){
		// d_green defaults		
	}else{
		$("ul.gtlw_tabs").addClass("active").addClass("l_green");	
	}
	$("#advice_refine ul.headers").addClass("clear").addClass("js");
	$("#advice_refine ul.categories").addClass("margin_clear");
	$("#cooked_food_tc").hide();
	$("ul.categories h4").hide();
	$("ul.gtlw_tabs li").click(function(){
		$("#categories").show();
		$("#categories").removeClass("hidden");
		class_identifier = $(this).attr("class");
		switch_tab(class_identifier);
	})
	$("ul.categories li li").hover(
		function(){	
			$(this).addClass("hover")							
		},function(){
			$(this).removeClass("hover")	
		}
	)
	drop_down_ul_select();
}

function lightboxes() {
	
	// append query string to links
	lb_links_array = $(".lb_link");
	//alert(lb_links_array.length);
	for( var i=0, count=lb_links_array.length; i<count; ++i){
		url = $(lb_links_array[i]).attr("href");
		new_url = url+"?lb=true";
		$(lb_links_array[i]).attr("href",new_url);
    }

    if ($(".lightbox_init1").length > 0) {
	    // first - login/register
        $(".lightbox_init1").colorbox({ iframe: true, innerWidth: 590, innerHeight: 805, opacity: 0.4 });
    }
}

function drop_down_ul_select() {
    $("ul.secondary li").hide();
    $("ul.categories li.primary").removeClass("non_js");
    $("ul.secondary").addClass("js");

    $("ul.primary li").hover(
        function() {
            $(this).addClass("hover");
        },
        function() {
            $(this).removeClass("hover");
        }
    );

        $("ul.secondary").hover(
        function() {
            parent_id = $(this).parent().attr("id");
            $("#" + parent_id ).addClass("hover");
            sub_cat_count = $("#" + parent_id + " ul.secondary").children("li").length;
            //alert(sub_cat_count);
            height_adjustment = (sub_cat_count * 19) + 2;
            $("#" + parent_id + " span.trigger").css("height", height_adjustment + "px");
            $("#" + parent_id + " ul.secondary li").show("");
            
        },
        function() {
            $("#" + parent_id ).removeClass("hover");
            $("#" + parent_id + " ul.secondary li").hide("");
            $("#" + parent_id + " span.trigger").css("height", "0px");
        }
    );
}

function init() {
    if ($("div.gtlw").length > 0) {
        autocomplete();
        best_kept_ajax_tabswitcher();
        best_kept_tabswitcher();
    }
	lightboxes();
}

$(document).ready(function() {
    //initialise js	
    init();
});
