﻿$(document).ready(function() {
    //Scroller
    //DivBlockUI("main");
    //Main menu
    initMainMenu();	

    initMenuFunction();
    //DivUnBlockUI("main");

    $('#js-news').ticker({titleText:''});


});

function clearPop() {
    $('#mainDiv').remove();
}

function initMenuFunction()
{
//    $("#sub_prod").hide();
//	$("#home").hover(function(){
//		$("#sub_prod").hide();
//		$("#sub_news").show();
//		
//	});
//	$("#nav_products").hover(function(){
//		$("#sub_news").hide();
//		$("#sub_prod").show();
//	});
//	$("#nav_products").mouseout(function(){
//		$("#sub_news").show();
//		$("#sub_prod").hide();
//	});
}
function initMainMenu() {
    var style = 'easeOutElastic';

    //Retrieve the selected item position and width
    var default_left = Math.round($('#lava li.selected').offset().left - $('#lava').offset().left);
    var default_width = $('#lava li.selected').width();

    //Set the floating bar position and width
    $('#box').css({ left: default_left });
    $('#box .head').css({ width: default_width });

    //if mouseover the menu item
    $('#lava li').hover(function() {

        //Get the position and width of the menu item
        left = Math.round($(this).offset().left - $('#lava').offset().left);
        width = $(this).width();

        //Set the floating bar position, width and transition
        $('#box').stop(false, true).animate({ left: left }, { duration: 1000, easing: style });
        $('#box .head').stop(false, true).animate({ width: width }, { duration: 1000, easing: style });

        //if user click on the menu
    }).click(function() {

        //reset the selected item
        $('#lava li').removeClass('selected');

        //select the current item
        $(this).addClass('selected');

    });

    //If the mouse leave the menu, reset the floating bar to the selected item
    $('#lava').mouseleave(function() {

        //Retrieve the selected item position and width
        default_left = Math.round($('#lava li.selected').offset().left - $('#lava').offset().left);
        default_width = $('#lava li.selected').width();

        //Set the floating bar position, width and transition
        $('#box').stop(false, true).animate({ left: default_left }, { duration: 2000, easing: style });
        $('#box .head').stop(false, true).animate({ width: default_width }, { duration: 2000, easing: style });

    });
}

function initScroller ()
{
    $customScrollBox = $("#customScrollBox");
    $customScrollBox_container = $("#customScrollBox .container");
    $customScrollBox_content = $("#customScrollBox .content");
    $dragger_container = $("#dragger_container");
    $dragger = $("#dragger");
    visibleHeight = $customScrollBox.height();
    if ($customScrollBox_container.height() > visibleHeight) { //enable scrollbar if content is long
        totalContent = $customScrollBox_content.height();
        minDraggerHeight = $dragger.height();
        draggerContainerHeight = $dragger_container.height();
        adjDraggerHeight = totalContent - ((totalContent - visibleHeight) * 1.3); //adjust dragger height analogous to content
        if (adjDraggerHeight > minDraggerHeight) { //minimum dragger height
            $dragger.css("height", adjDraggerHeight + "px").css("line-height", adjDraggerHeight + "px");
        } else {
            $dragger.css("height", minDraggerHeight + "px").css("line-height", minDraggerHeight + "px");
        }
        if (adjDraggerHeight < draggerContainerHeight) { //maximum dragger height
            $dragger.css("height", adjDraggerHeight + "px").css("line-height", adjDraggerHeight + "px");
        } else {
            $dragger.css("height", draggerContainerHeight - 10 + "px").css("line-height", draggerContainerHeight - 10 + "px");
        }
        animSpeed = 600; //animation speed
        easeType = "easeOutCirc"; //easing type
        bottomSpace = 1.05; //bottom scrolling space
        targY = 0;
        draggerHeight = $dragger.height();
        $dragger.draggable({
            axis: "y",
            containment: "parent",
            drag: function(event, ui) {
                Scroll();
            },
            stop: function(event, ui) {
                DraggerOut();
            }
        });

        //scrollbar click
        $dragger_container.click(function(e) {
            $this = $(this);

            var mouseCoord = (e.pageY - $this.offset().top);
            var targetPos = mouseCoord + $dragger.height();
            if (targetPos < $dragger_container.height()) {
                $dragger.css("top", mouseCoord);
                Scroll();
            } else {
                $dragger.css("top", $dragger_container.height() - $dragger.height());
                Scroll();
            }
        });

        //mousewheel
        $(function($) {
            $customScrollBox.bind("mousewheel", function(event, delta) {
                vel = Math.abs(delta * 10);
                $dragger.css("top", $dragger.position().top - (delta * vel));
                Scroll();
                if ($dragger.position().top < 0) {
                    $dragger.css("top", 0);
                    $customScrollBox_container.stop();
                    Scroll();
                }
                if ($dragger.position().top > $dragger_container.height() - $dragger.height()) {
                    $dragger.css("top", $dragger_container.height() - $dragger.height());
                    $customScrollBox_container.stop();
                    Scroll();
                }
                return false;
            });
        });

        //scroll
        function Scroll() {
            var scrollAmount = (totalContent - (visibleHeight / bottomSpace)) / (draggerContainerHeight - draggerHeight);
            var draggerY = $dragger.position().top;
            targY = -draggerY * scrollAmount;
            var thePos = $customScrollBox_container.position().top - targY;
            $customScrollBox_container.stop().animate({ top: "-=" + thePos }, animSpeed, easeType); //with easing
            //$customScrollBox_container.css("top",$customScrollBox_container.position().top-thePos+"px"); //no easing
        }

        $dragger.mouseup(function() {
            DraggerOut();
        }).mousedown(function() {
            DraggerOver();
        });

        function DraggerOver() {
            $dragger.css("background", "url(App_Themes/images/custom_scrollbar_bg_over.png) no-repeat 0 10px");
        }

        function DraggerOut() {
            $dragger.css("background", "url(App_Themes/images/custom_scrollbar_bg.png) no-repeat 0 10px");
        }
    } else { //disable scrollbar if content is short
        $dragger.css("display", "none");
        $dragger_container.css("display", "none");
    }
}
