﻿$bg = $("#bg"); 0
$preloader = $("#preloader");
$outer_container = $("#outer_container");
$outer_container_a = $("#outer_container a.thumb_link");

$(window).load(function () {
    //cache vars
    $customScrollBox = $("#customScrollBox");
    $customScrollBox_container = $("#customScrollBox .container");
    $customScrollBox_content = $("#customScrollBox .content");
    $dragger_container = $("#dragger_container");
    $dragger = $("#dragger");

    CustomScroller();

    function CustomScroller() {
        outerMargin = 0;
        innerMargin = 20;
        $customScrollBox.height($(window).height() - outerMargin);
        $dragger_container.height($(window).height() - innerMargin);
        visibleHeight = $(window).height() - outerMargin;
        if ($customScrollBox_container.height() > visibleHeight) { //custom scroll depends on content height
            $dragger_container, $dragger.css("display", "block");
            totalContent = $customScrollBox_content.height();
            draggerContainerHeight = $(window).height() - innerMargin;
            animSpeed = 400; //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) {
                var mouseCoord = (e.pageY - $(this).offset().top);
                var targetPos = mouseCoord + $dragger.height();
                if (targetPos < draggerContainerHeight) {
                    $dragger.css("top", mouseCoord);
                    Scroll();
                } else {
                    $dragger.css("top", draggerContainerHeight - $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 > draggerContainerHeight - $dragger.height()) {
                        $dragger.css("top", draggerContainerHeight - $dragger.height());
                        $customScrollBox_container.stop();
                        Scroll();
                    }
                    return false;
                });
            });
            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
            }
            //dragger hover
            $dragger.mouseup(function () {
                DraggerOut();
            }).mousedown(function () {
                DraggerOver();

            });
            function DraggerOver() {
                $dragger.css("background", "url(includes/imgs/round_custom_scrollbar_bg_over.png)");
            }
            function DraggerOut() {
                $dragger.css("background", "url(includes/imgs/round_custom_scrollbar_bg.png)");
            }
        } else { //hide custom scrollbar if content is short
            $dragger, $dragger_container.css("display", "none");
        }
    }
    //resize browser window functions
    $(window).resize(function () {
        $dragger.css("top", 0); //reset content scroll
        $customScrollBox_container.css("top", 0);
        $customScrollBox.unbind("mousewheel");
        CustomScroller();
    });
});

function showMenu() {
    $("#menu_arrow").fadeTo("fast", 0);
    $('.menu').stop().animate({ right: '0px' }, 900, 'easeInOutExpo');
}

function hideMenu() {
    $('.menu').stop().animate({ right: '-260px' }, 900, 'easeInOutExpo', function () { $("#menu_arrow").fadeTo("fast", 1); });

}

//$('.menu').hover(
//		function () { //mouse over
//		    showMenu();
//		},
//		function () { //mouse out
//		    hideMenu();
//		}
//	);


//slide in/out left pane
$outer_container.hover(
		function () { //mouse over
		    SlidePanels("open");
		},
		function () { //mouse out
		    SlidePanels("close");
		}
	);
$("#tempImage").load(function () {
    LargeImageLoad($(this));
});
function LargeImageLoad($this) {
    $preloader.fadeOut("fast"); //hide preloader
    SlidePanels("close");
}
//Clicking on thumbnail changes the background image
$outer_container_a.click(function (event) {
    event.preventDefault();
    var $this = this;
    $preloader.fadeIn("fast"); //show preloader
    //style clicked thumbnail
    $outer_container_a.each(function () {
        $(this).children(".selected").css("display", "none");
    });
    $(this).children(".selected").css("display", "block");
    $("#atifcss").attr("href", $(this).attr("rel"));
    $("#tempImage").attr("src", $(this).attr("href"));
    clearTimeout(timeoutHandle);
    timeoutHandle = window.setTimeout("loadtheme()", 90000);
});

//slide in/out left pane function
function SlidePanels(action) {
    var speed = 900;
    var easing = "easeInOutExpo";
    if (action == "open") {
        $("#arrow_indicator").fadeTo("fast", 0);
        $outer_container.stop().animate({ left: 0 }, speed, easing);
        $bg.stop().animate({ left: 350 }, speed, easing);
    } else {
        $outer_container.stop().animate({ left: -350 }, speed, easing);
        $bg.stop().animate({ left: 0 }, speed, easing, function () { $("#arrow_indicator").fadeTo("fast", 1); });
    }
}
//preload script images
var images = ["ajax-loader_dark.gif", "round_custom_scrollbar_bg_over.png"];
$.each(images, function (i) {
    images[i] = new Image();
    images[i].src = this;
});
