$(document).ready(function() {
    
    //DD_belatedPNG.fix('div#question-bar, div#comments h4, div#quiz-notifier div, a#close-panel, div.checkbox-field, a.submit-form-btn, div.comment-btn div, a.comment-btn, div#main-menu-container div, a.gallery-categories, div.link-columns h4, div.contact-right h4, div#logo a, img');
    
    //gallery thumbnails slider jQuery plugin
    $(".gallery-slideshow").slider({
        controls : {left : ".left-arrow", right : ".right-arrow"}
    });
    /*
    $(".gallery-slideshow-large").slider({
        controls : {left : ".left-arrow-large", right : ".right-arrow-large"},
        container : ".thumbnail-holder-large",
        visibleContainer : ".overflow-holder-large",
        xStep : 640
    });
    */
    //gallery thumbnails slider jQuery plugin
    $(".event-slideshow-large").slider({
        controls : {top : ".top-arrow-large", bottom : ".bottom-arrow-large"},
        container : ".thumbnail-holder-large",
        visibleContainer : ".overflow-holder-large",
        xStep : 490,
        yStep : 490
    });
    
    $(".event-slideshow-small").slider({
        controls : {top : ".top-arrow-small", bottom : ".bottom-arrow-small"},
        container : ".thumbnail-holder-small",
        visibleContainer : ".overflow-holder-small",
        xStep : 60,
        yStep : 377
    });
    
    $(".event-slideshow-horizontal-small").slider({
        controls : {left : ".top-arrow-horizontal-small", right : ".bottom-arrow-horizontal-small"},
        container : ".thumbnail-holder-horizontal-small",
        visibleContainer : ".overflow-holder-horizontal-small",
        xStep : 189,
        yStep : 60
    });
    
    $(document).keydown(function (e) {
        if( e.which == 37 || e.which == 38 || e.which == 39 || e.which == 40 ) {
            switch (e.which) {
                case 37 :
                    var className = ".top-arrow-horizontal-small";
                    break;
                case 38 :
                    var className = ".top-arrow-large";
                    var classNameSmall = ".top-arrow-small";
                    break;
                case 39 :
                    var className = ".bottom-arrow-horizontal-small";
                    break;
                case 40 :
                    var className = ".bottom-arrow-large";
                    var classNameSmall = ".bottom-arrow-small";
                    break;
            }
            $(className).trigger("click");
            $(classNameSmall).trigger("click");
            return false;
        }
    });
    
    $(".img_gallery_item").click(function() {
        var imgID = $(this).attr("id").split("-")[1];
        var imgPos = $("#largeimggallery-" + imgID).position().left ;
        //imgPos = -imgPos+703;
        var elmIndex = checkElmIndex( "largeimggallery-" + imgID );
        $(".thumbnail-holder-large").animate({left: (-elmIndex*703)+"px"});
        
        return false;
    })
    
    function checkElmIndex(elmID) {
        var idx = 0;
        var result = 0;
        $(".thumbnail-holder-large").find("div").each(function() {
            var id = $(this).find(".img_gallery_item").attr("id");
            if(id == elmID) {
                result = idx;
            }
            idx++;
        })
        return result;
    } 
});

/**
 *
 *  jQuery Slider Plugin
 *
 */
var blockTimeout = false;
(function($) {
    $.fn.slider = function(options) {
        var opt = $.extend({}, $.fn.slider.defaults, options);
        
        return this.each(function(){
            $this = $(this);
            
            if(options.update == 'update-width') {
                var totalWidth = 0;
                $(opt.container).find("div.single-slide-item").each(function() {
                    totalWidth += $(this).outerWidth(true);
                });
                $(opt.container).width(totalWidth);
                return false;
            }
            
            //if(options.container != ".thumbnail-holder-large") {
                var totalWidth = 0;
                $(opt.container).find("div.single-slide-item").each(function() {
                    totalWidth += $(this).outerWidth(true);
                });
                $(opt.container).width(totalWidth);
            //}
            var newPos = 0;
            var contDim = {width : $(opt.container).width(), height : $(opt.container).height()};
            var visibleContainerDim = {width : $(opt.visibleContainer).width(), height : $(opt.visibleContainer).height()};
            var curr_pos = {x : parseInt($(opt.container).css("left"), 10), y : parseInt($(opt.container).css("top"), 10)};
            for(var i in opt.controls) {
                /*
                $(opt.controls[i]).mouseover(function() {
                    var newIdx = i;
                    return function() {
                        switch( newIdx ) {
                            case 'overLeft' :
                                var pixelsPerSeconds = Math.round((-parseInt($(opt.container).position().left, 10)/170))*1000;
                                $(opt.container).animate({ left: 0 }, pixelsPerSeconds, "linear");
                                break;
                            case 'overRight' :
                                var pixelsPerSeconds = Math.round((contDim.width - visibleContainerDim.width + parseInt($(opt.container).position().left, 10))/170)*1000;
                                $(opt.container).animate({ left: -(contDim.width - visibleContainerDim.width) }, pixelsPerSeconds, "linear");
                                break;
                        }
                    }
                }());
                
                $(opt.controls[i]).mouseout(function() {
                    var newIdx = i;
                    return function() {
                        switch( newIdx ) {
                            case 'overLeft' :
                            case 'overRight' :
                                $(opt.container).stop();
                                break;
                        }
                    }
                }());
                */
                $(opt.controls[i]).unbind('click').click(function() {
                    var newIdx = i;
                    return function() {
                        if(blockTimeout) return false;
                        switch( newIdx ) {
                            case 'left' :
                                curr_pos = {x : parseInt($(opt.container).css("left"), 10), y : parseInt($(opt.container).css("top"), 10)};
                                newPos = curr_pos.x + opt.xStep;
                                if(curr_pos.x < 0) {
                                    blockTimeout = true;
                                    $(opt.container).animate({left : newPos}, "fast", function() { blockTimeout = false; });
                                    curr_pos.x += opt.xStep;
                                }
                                break;
                            case 'right' :
                                curr_pos = {x : parseInt($(opt.container).css("left"), 10), y : parseInt($(opt.container).css("top"), 10)};
                                newPos = curr_pos.x - opt.xStep;
                                if((contDim.width/* - visibleContainerDim.width*/ + newPos) > 0) {
                                    blockTimeout = true;
                                    $(opt.container).animate({left : newPos }, "fast", function() { blockTimeout = false; });
                                    curr_pos.x -= opt.xStep;
                                }
                                
                                break;
                            case 'top' :
                                newPos = curr_pos.y + opt.yStep;
                                if((curr_pos.y < 0)) {
                                    $(opt.container).animate({top : newPos });
                                    curr_pos.y += opt.yStep;
                                }
                                coordProperty = "top";
                                break;
                            case 'bottom' :
                                newPos = curr_pos.y - opt.yStep;
                                if( contDim.height + newPos > 0 ) {
                                    $(opt.container).animate({top : newPos });
                                    curr_pos.y -= opt.yStep;
                                }
                                break;
                            case 'top_left' :
                                newPos = curr_pos.x + opt.xStep;
                                var newYPos = curr_pos.y + opt.yStep;
                                if((curr_pos.x < 0) && (curr_pos.y < 0)) {
                                    $(opt.container).animate({left : newPos, top : newYPos});
                                    curr_pos.y += opt.yStep;
                                    curr_pos.x += opt.xStep;
                                }
                                break;
                            case 'top_right' :
                                newPos = curr_pos.x - opt.xStep;
                                var newYPos = curr_pos.y + opt.yStep;
                                if( ((contDim.width + newPos) > 0) && (curr_pos.y < 0)) {
                                    $(opt.container).animate({left : newPos, top : newYPos});
                                    curr_pos.y += opt.yStep;
                                    curr_pos.x -= opt.xStep;
                                }
                                break;
                            case 'bottom_left' :
                                newPos = curr_pos.x + opt.xStep;
                                var newYPos = curr_pos.y - opt.yStep;
                                if((curr_pos.x < 0) && (contDim.height + newYPos > 0)) {
                                    $(opt.container).animate({left : newPos, top : newYPos});
                                    curr_pos.y -= opt.yStep;
                                    curr_pos.x += opt.xStep;
                                }
                                break;
                            case 'bottom_right' :
                                newPos = curr_pos.x - opt.xStep;
                                var newYPos = curr_pos.y - opt.yStep;
                                if( ((contDim.width + newPos) > 0) && (contDim.height + newYPos > 0)) {
                                    $(opt.container).animate({left : newPos, top : newYPos});
                                    curr_pos.y -= opt.yStep;
                                    curr_pos.x -= opt.xStep;
                                }
                                break;
                            case 'reset' :
                                $(opt.container).animate({top : "0px", left : "0px"});
                                break;
                        }
                        return false;
                    }
                }());
                
            }
        });
    };
    
    $.fn.slider.defaults = {
        controls : {
            overLeft : ".left-over-btn",
            overRight : ".right-over-btn",
            left : ".left-btn",
            right : ".right-btn",
            top : ".top-btn",
            bottom : ".bottom-btn",
            top_left : ".top-left-btn",
            top_right : ".top-right-btn",
            bottom_left : ".bottom-left-btn",
            bottom_right : ".bottom-right-btn",
            reset: ".reset-btn"
        },
        container : ".thumbnail-holder",
        visibleContainer : ".overflow-holder",
        xStep : 528,
        yStep : 528
    };
})(jQuery);
