$(document).ready(function() {

    ////////////////////////////////////
    ////////////////////////////////////
    // Settings
    ////////////////////////////////////

    // width of columns inside mega dropdown. Does NOT set the width. This is used to calculate the appropriate width for the dropdown containers.
    //var mega_dropdown_col_width = 200;
    // defines the element, such as table or div that is the outer edge of the template. Replace #wrapper with id or class of element.
    var mega_template_container = $("#mainmenu");
    // enter a comma separated list of category type foreign ids corresponding to the category types whose mega menu should be aligned left with no auto center.
    var no_auto_center = [19, 10];
    // enter a comma separated list of category type foreign ids corresponding to the category types whose mega menu should be aligned right.
    var align_right = [7, 2]
    // get the whole site width
    var site_container = $(".content");
    ////////////////////////////////////
    ////////////////////////////////////
    // End Settings
    ////////////////////////////////////

    var mega_template_container_width = $(site_container).width();  //$(mega_template_container).width();

    // add class names to top level li's so that later statements can use class names to properly align dropdowns
    // no_auto_center
    $(".mega").each(function() {
        var curr_id = $(this).attr("id");
        curr_id = parseInt(curr_id);
        // $.inArray(4,data)
        var no_auto_center_test = $.inArray(curr_id, no_auto_center);
        if (no_auto_center_test != -1) {
            $(this).addClass("no_auto_center");
        }
        var align_right_test = $.inArray(curr_id, align_right);
        if (align_right_test != -1) {
            $(this).addClass("mega_right_align");
        }
    });


    // set width of .mega_content dropdowns in horizontal nav
    $("#mega_menu .mega_content").each(function() {
        $this = $(this);
        //var curr_number_of_columns = $this.find("div").length - 1;
        var curr_number_of_columns = $this.find("div").length;

        var curr_mega_content_width = 860;  //(curr_number_of_columns * mega_dropdown_col_width);
        //handle less than 5 columns situation
        var padding = 10;
        if (curr_number_of_columns < 5) {
            var each_columns_width = (860 - 100 - (padding * 4)) / 4;
            curr_mega_content_width = (curr_number_of_columns - 1) * each_columns_width;
            if (curr_number_of_columns == 2)
                curr_mega_content_width += 115;
            if (curr_number_of_columns == 3)
                curr_mega_content_width += 120;
            if (curr_number_of_columns == 4)
                curr_mega_content_width += 125;
        }
        //if(curr_number_of_columns == 3){curr_mega_content_width = curr_mega_content_width - 50;}
        //if (curr_number_of_columns == 6) { curr_mega_content_width = curr_mega_content_width - 100; }

        curr_mega_content_width = curr_mega_content_width + "px";
        $this.css("width", curr_mega_content_width);
        // give left border to first column
        //$this.find("div:first").css("border-left", "1px solid #13578e");
        $this.find("div:first").css("padding-left", "10px");
    });

    existing_template_position = $(mega_template_container).position();
    existing_template_left_margin = existing_template_position.left;
    
    $(".mega").each(function() {
        $this = $(this);
        // if browser is ie6, add iframe beneath dropdown so that it appears over select boxes

        /*	if($.browser.msie && $.browser.version=="6.0") {
        var ie6_iframe = "<iframe class='mega_cover_select' />";
        $(this).prepend(ie6_iframe);
        var mega_height_to_match = $(this).height;
        $(".mega_cover_select").css("height",mega_height_to_match);
        }
        */
        var curr_mega_content_width = $this.find(".mega_content").width();
        /* Hieutt*/
        //if (curr_mega_content_width < (mega_template_container_width)) {
        //var desired_left_value = (mega_template_container_width - curr_mega_content_width) / 2;
        var existing_position = $this.position();
        var existing_left_position = existing_position.left;
        //var existing_left_inside_template_position = existing_left_position - existing_template_left_margin;
        //var left_position_adjustment = ((existing_left_inside_template_position - desired_left_value) * -1) + "px";

        //if the dropdown box fallout of the right edge of the website
        //position it back with the right edge equal to the website right edge
        if ((curr_mega_content_width + existing_left_position) > mega_template_container_width) {
            var desired_adjustable_width = 17;
            var total_width = curr_mega_content_width + existing_left_position;
            var diff = total_width - mega_template_container_width + desired_adjustable_width;
            //adjust padding of 10px
            if (curr_mega_content_width < 860) {
                diff = diff - 10;
            }
            var left_position_adjustment = (diff * -1) + "px";
            // dont' center the dropdown if it's too close to the edge
            if ($this.hasClass("mega_right_align") == false && $this.hasClass("no_auto_center") == false) {
                $this.find(".mega_content").css("left", left_position_adjustment);
            }
        }

        //            if ((curr_mega_content_width + existing_left_position) > (mega_template_container_width)) {
        //                if ((curr_mega_content_width > (mega_template_container_width - existing_left_position)) &&
        //			    (curr_mega_content_width < existing_left_position)) 
        //			    {
        //                    $this.addClass('right');
        //                    var desired_left_value = (mega_template_container_width - curr_mega_content_width) / 2;
        //                    var existing_position = $this.offset();
        //                    var existing_left_position = existing_position.left;
        //                    var existing_left_inside_template_position = existing_left_position - existing_template_left_margin;
        //                    var left_position_adjustment = ((existing_left_inside_template_position - desired_left_value) * -1) + "px";
        ////                    // dont' center the dropdown if it's too close to the edge
        //                    if ($this.hasClass("mega_right_align") == false && $this.hasClass("no_auto_center") == false) {
        //                        $this.find(".mega_content").css("left", left_position_adjustment);
        //                    }
        //                }
        //                else if ((curr_mega_content_width > (mega_template_container_width - existing_left_position)) 
        //                      && (curr_mega_content_width > existing_left_position)) {
        //                    $this.addClass('mid');
        //                    $this.find(".mega_content").css("left", left_position_adjustment);
        //                }
        //            } else {

        //            }
    }
    /*End hieutt */

    // if dropdown is greater than half of screen width, then place in center
    //if (curr_mega_content_width >= mega_template_container_width / 2) {
    //If the right dropdown box position greater than the right of the website in pixel
    //the readjust it in central
    //        if (curr_mega_content_width >= mega_template_container_width / 2) {
    //            var desired_left_value = (mega_template_container_width - curr_mega_content_width) / 2;
    //            var existing_position = $this.offset();
    //            var existing_left_position = existing_position.left;
    //            var existing_left_inside_template_position = existing_left_position - existing_template_left_margin;
    //            var left_position_adjustment = ((existing_left_inside_template_position - desired_left_value) * -1) + "px";
    //            // dont' center the dropdown if it's too close to the edge
    //            if ($this.hasClass("mega_right_align") == false && $this.hasClass("no_auto_center") == false) {
    //                $this.find(".mega_content").css("left", left_position_adjustment);
    //            }
    //            //desired_left_value > existing_left_inside_template_position
    //        }

    // assign right position to dropdown so that it stays on screen
    //        if ($this.hasClass("mega_right_align")) {
    //            // get width of mega_content
    //            var curr_mega_width = $this.width();
    //            if (curr_mega_content_width < 498 || existing_left_inside_template_position > $this.width()) {
    //                var curr_left_value = -1 * (curr_mega_content_width - curr_mega_width + 2);
    //                // assign CSS left property to mega content
    //                $this.find(".mega_content").css("left", curr_left_value);
    //            } //if
    //        } // if

    //} //function
); // each

    // change class on hover to cause dropdown
    $(".mega").hover(
		function() {
		    $(this).addClass("hovering");
		    var curr_background_color = $(this).find("a:first").css("background-color");
		    $(this).css("background-color", curr_background_color);
		},
		function() {
		    $(this).removeClass("hovering");
		    $(this).css("background-color", "transparent");
		}
);
    //if($(".accsub").lenght)
    $(".accsub").each(function() {
        $this = $(this);
        $(this).click(
		function() {
		    $(".accsub").removeClass("active");
		    $("ul", this).show(100);
		    $(this).addClass("active");
		    $("ul", this).hide();
		    return false;
		}
	);

});

});                // ready

/****************************************************************************************************************/
/* Mega_menu delaying function - delaying interval can be alter the dropdown speed of the menu.
/****************************************************************************************************************/

var timer;
var current;

var showMenu = function(menu) {

    if (current == menu) {
        return;
    } else {
        current = menu;
        var callback = function(o) {
            return function() {
                o.addClass("show");
            }

        } (menu);

        if (timer != undefined) {
            window.clearTimeout(timer);
        }
        timer = window.setTimeout(callback, 200); //you can change the 500 to you wanted.
    }
};


var hideMenu = function(menu) {
    if (timer != undefined) {
        window.clearTimeout(timer);
    }
    menu.removeClass("show");
    current = null;
};

$(function() {
    $(".mega").mouseenter(function() {
        showMenu($(this));
    })

    $(".mega").mouseleave(function() {
        hideMenu($(this));
    });
});
//**************************************************
//var timer;
//var current;

//var showMenu = function(menu) {

//    if (current == menu) {
//        return;
//    } else {
//        current = menu;
//        var callback = function(o) {
//            return function() {
//                o.addClass("show");
//            }

//        } (menu);

//        if (timer != undefined) {
//            window.clearTimeout(timer);
//        }
//        timer = window.setTimeout(callback, 500); //you can change the 500 to you wanted.
//    }
//};


//var hideMenu = function(menu) {
//    if (timer != undefined) {
//        window.clearTimeout(timer);
//    }
//    menu.removeClass("show");
//    current = null;
//};

//$(function() {

//    $(".mega > a").hover(function() {
//        showMenu($(this).parent());
//    })

//    $(".mega").mouseenter(function() {
//        showMenu($(this));
//    })

//    $(".mega").mouseleave(function() {
//        hideMenu($(this));
//    });

//});
