/*
Initialisation file for:
- jScrollPane
- wresize
- scroll buttons
...
*/
$(document).ready(function() {
    /*--------------------------------------------------------------------------------------------------------------
    OPTIONS
    --------------------------------------------------------------------------------------------------------------*/

    //jScrollPane options
    scrollTopOffset = 22;
    scrollBottomOffset = 0;
    scrollHeight = null; // set to null to use container height
    animationInterval = 5; //jScrollPane default: "The interval in milliseconds to update an animating scrollPane (default 100)"
    animationStep = 3; // jScrollPane default: "The amount to divide the remaining scroll distance by when animating (default 3)"
    scrollbarWidth = 13;
    tabScrollingOffset = 30;


    // container options
    containerMinHeight = 200;
    containerMaxHeight = 616; //$( '.hold' ).height();
    containerWindowDistance = 190;
    containerStartOffset = 166;


    // left pane options
    /*leftPaneMinHeight = 100;
    leftPaneMaxHeight =  $( '.LeftPane' ).height();
    leftPaneWindowDistance = containerWindowDistance+180;//350;*/


    /*--------------------------------------------------------------------------------------------------------------
    INIT
    --------------------------------------------------------------------------------------------------------------*/

    // content
    if (!$('.content'))
        return false;


    // get querystring	
    (function() {

        var after_type_cast = {};
        var before_type_cast = {};
        var cached = false;


        jQuery.query = function(cast) {
            if (!cached) {
                // remove leading ? and trailing & 
                var q = location.search.replace(/^\?/, '').replace(/\&$/, '').split('&');
                for (var i = q.length - 1; i >= 0; i--) {
                    var p = q[i].split('='), key = p[0], val = p[1];
                    before_type_cast[key] = val;
                    // convert floats 
                    if (/^[0-9.]+$/.test(val))
                        val = parseFloat(val);
                    // convert booleans 
                    if (/^(true|false)$/.test(val))
                        val = (val == 'true');
                    // ignore empty values 
                    if (val)
                        after_type_cast[key] = val;
                }
                cached = true;
            }
            return cast === false ? before_type_cast : after_type_cast;
        };
    })();


    $.query();
    adminModus = $.query().adminmod;  // Detect Admin Modus in query string, if true => don't initialise scrolling
    
    scrollInnerHeight = $('.content').innerHeight();
    initialOffset = $('.content').offset().top;


    /*--------------------------------------------------------------------------------------------------------------
       ADMIN
    --------------------------------------------------------------------------------------------------------------*/
     function SetAdminModus() {
        adminModus = 1;
        $('#ContentPaneHold').css({ height: 'auto', 'overflow': 'hidden' });

        $('#cover_container').empty();
        $('.button_fwd').css('display', 'none');
        $('.main_cover_bottom').css('display', 'none');
        $('.main_cover_top').css('display', 'none');
        $('.button_back').css('display', 'none');

        $('#ContentPaneFooter').css('display', 'none');
        $('.toggleAdmin').empty();
        $('html').css('overflow-y', 'auto');
        $('.hold').addClass("holdAdmin").removeClass("hold");
        $('.content').addClass("contentAdmin").removeClass("content");
     }
	 
      function SetToSmallModus() {
        adminModus = 0;
        $('#ContentPaneHold').css({ height: 'auto', 'overflow': 'hidden' });

        $('#cover_container').empty();
        $('.button_fwd').css('display', 'none');
        $('.main_cover_bottom').css('display', 'none');
        $('.main_cover_top').css('display', 'none');
        $('.button_back').css('display', 'none');

        $('#ContentPaneFooter').css('display', 'block');
        $('.toggleAdmin').empty();
        $('html').css('overflow-y', 'auto');
        $('.hold').addClass("holdAdmin").removeClass("hold");
        $('.content').addClass("contentAdmin").removeClass("content");
     }

      function MyWinHeight() {
      	var myWidth = 0, myHeight = 0;
      	if( typeof( window.innerWidth ) == 'number' ) {
      		myWidth = window.innerWidth; myHeight = window.innerHeight;
      	} else if( document.documentElement && ( document.documentElement.clientWidth ||document.documentElement.clientHeight ) ) {
      		myWidth = document.documentElement.clientWidth; myHeight = document.documentElement.clientHeight;
      	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
      		myWidth = document.body.clientWidth; myHeight = document.body.clientHeight;
      	}
      	//window.alert( 'Width = ' + myWidth + ' and height = ' + myHeight );
      	return myHeight;
      }

    if (adminModus == 1) 
    {
        SetAdminModus();
    }
    else 
    {
        /********************************************
        *			    FULL SCROLLING				*
        *					  +						*
        *			       RESIZING					*
        *			    FUNCTIONALITY				*
        */
        //*****************************************

        /*--------------------------------------------------------------------------------------------------------------
        RESIZE WINDOW ==> resize container div + scrollbar
        --------------------------------------------------------------------------------------------------------------*/
        
        // custom scrollbar
        function RemoveScrollPanes() {
            $('.content').jScrollPaneRemove();
        }
        function CreateScrollPanes() {
            $('.content').jScrollPane({
                animateTo: true,
                scrollbarTopOffset: scrollTopOffset,
                animateInterval: animationInterval,
                animateStep: animationStep,
                scrollbarBottomOffset: scrollBottomOffset,
                scrollbarHeight: scrollHeight,
                selectTextScrolling: false,
                tabScrollOffset: tabScrollingOffset
            });
        }
        function scrollToInternalLink(scrollposition) {
            var pos = $(scrollposition.attr('href')).offset().top - 185;
            $(scrollposition).blur();
            $('#ContentPaneHold')[0].focus();
            $('.content')[0].scrollTo(pos);
            //if ($(".IE6").length != 0)
            //    content_resize();
        }

        function scrollbutton_visible() {
            // check if scrollbar is at the top of content
            if ($('.content').offset().top < initialOffset && $('.content').innerHeight() > $('.hold').height()) {
                $('.main_cover_top').css('visibility', 'visible');
                $('#forward').css('visibility', 'visible');
                $('.button_back').css('visibility', 'visible');
				$('#top').css('visibility', 'visible');

				
            }
            else {
                $('.main_cover_top').css('visibility', 'hidden');
                $('#forward').css('visibility', 'hidden');
                $('.button_back').css('visibility', 'hidden');
				$('#top').css('visibility', 'hidden');
            }

            // check if scrollbar is at the bottom of content	
            if (switchvar == 0) {
                maxScrollOffsetSwitch = maxScrollOffset;
            }
            else {
                maxScrollOffsetSwitch = maxScrollOffset + initialOffset;
                switchvar = 0;
            }
            if ($('.content').offset().top <= -(maxScrollOffsetSwitch) || $('.content').innerHeight() <= $('.hold').height()) {
                $('.button_fwd').css('visibility', 'hidden');
                $('#back').css('visibility', 'hidden');
                $('.main_cover_bottom').css('visibility', 'hidden');
            }
            else {
                $('.button_fwd').css('visibility', 'visible');
                $('#back').css('visibility', 'visible');
                $('.main_cover_bottom').css('visibility', 'visible');
            }


            // if no scrollbar, resize contentpane to show the same amount of right margin
            if ($('.content').innerHeight() <= $('.hold').height()) {
                $('.content').css('width', '480px');
            }
        }
        //end function scrollbutton_visible() 


        // resize container	
        function content_resize() {
        
            if (MyWinHeight() <= 515)  { 
              RemoveScrollPanes();
              SetToSmallModus();
              return;
             //$('html').css('overflow-y', 'auto');
            }
            else
            { 
              //$('html').css('overflow-y', 'hidden');
            }

            currentScrollPosition = initialOffset + (-$('.content').offset().top);

            var w = $(window);
            var H = w.height();
            var W = w.width();

            RemoveScrollPanes();

            if (H - containerWindowDistance <= containerMaxHeight) {
                //normal
                if (H - containerWindowDistance + containerStartOffset - $('.hold').offset().top > containerMinHeight) {
                    $('.hold').css({ height: H - containerWindowDistance + containerStartOffset - hardRefreshOffset });  // original : H-containerWindowDistance
                    $('.content').css({ height: H - containerWindowDistance + containerStartOffset - hardRefreshOffset });


                    if ($('.IE6')[0] != undefined) {
                        $('.main_cover_bottom').css({ top: H - containerWindowDistance - 85 }); //104
                    }
                    else {
                        $('.main_cover_bottom').css({ top: H - containerWindowDistance - 62 });
                    }
                }
                else {
                    $('.hold').css({ height: containerMinHeight });
                    $('.content').css({ height: containerMinHeight });
                    if ($('.IE6')[0] != undefined) {
                        $('.main_cover_bottom').css({ top: containerMinHeight - 85 }); //104
                    }
                    else {
                        $('.main_cover_bottom').css({ top: containerMinHeight - 62 });
                    }
                }

                // scrollInnerHeight, calculated before scrollpane is added!!
                scrollInnerHeight = $('.content').innerHeight();

            }
            else {
                //alert("bigger than maxheight");
                $('.hold').css({ height: containerMaxHeight + containerStartOffset - hardRefreshOffset });
                $('.content').css({ height: containerMaxHeight + containerStartOffset - hardRefreshOffset });
                if ($('.IE6')[0] != undefined) {
                    $('.main_cover_bottom').css({ top: containerMaxHeight - 85 }); //104
                }
                else {
                    $('.main_cover_bottom').css({ top: containerMaxHeight - 62 });
                }

                // scrollInnerHeight, calculated before scrollpane is added!!				
                scrollInnerHeight = $('.content').innerHeight();
            }          
            
            /* 
            make the scrollbar instantly scroll to the previous position, afterwards, set the animate option to true again
            */
            CreateScrollPanes();

            // als content te klein is, niet scrollen					
            if ($('.content').innerHeight() > $('.hold').height()) {
                $('.content')[0].scrollTo(currentScrollPosition, true);
                $('.main_cover_bottom').css('visibility', 'hidden');
                $('.button_fwd').css('visibility', 'hidden');
            }

            maxScrollOffset = $('.content').height() - $('.hold').height() - initialOffset;
            switchvar = 1;
            scrollbutton_visible();

            if (currentScrollPosition == 0) {
                $('.main_cover_top').css('visibility', 'hidden');
                $('.button_back').css('visibility', 'hidden');
            }
        } // end function content_resize()


        CreateScrollPanes();
        $(window).wresize(content_resize);
        hardRefresh = 0;
        if (hardRefresh == 0) {
            hardRefreshOffset = $('.hold').offset().top;
            content_resize();
            hardRefresh = 1;
        }


        /*--------------------------------------------------------------------------------------------------------------
        SCROLLBUTTONS
        --------------------------------------------------------------------------------------------------------------*/

        initialOffset = $('.content').offset().top;
        maxScrollOffset = ($('.content').height() - $('.hold').height() - initialOffset);

        $('.button_fwd').click(function() {
            $('.content')[0].scrollBy($('.hold').height() - 45);
        });
        $('.button_back').click(function() {
              $('.content')[0].scrollBy(-$('.hold').height() + 45);
        });
		    $('#top').click(function() {
            $('.content')[0].scrollTo($('.content').offset().top);
        });

        //clicking top link will scroll the content
        $('a[href*=#]').click(function(event) {
            event.preventDefault();
            //$('.content')[0].scrollTo($(this).parent().attr('href'));
            scrollToInternalLink($(this));
            return false;
        });

        // scroll

        $('.content').scroll(function() {
            test = initialOffset + (-$('.content').offset().top);
            $('#testvar').text(" current scroll: " + test + " == initalOffset: " + initialOffset + " + content offset top -" + $('.content').offset().top);
            scrollbutton_visible();

        });


    } 	// end adminModus 'IF'
//Attach lightbox event to hyperlinks with lightbox css class
$('a.lightbox').lightBox({ 
  		imageLoading: '/DesktopModules/SimpleGallery/images/lightbox/lightbox-ico-loading.gif',
  		//imageBtnPrev: '/DesktopModules/SimpleGallery/images/lightbox/lightbox-btn-prev.gif',
  		//imageBtnNext: '/DesktopModules/SimpleGallery/images/lightbox/lightbox-btn-next.gif',
  		//imageBtnClose: '/DesktopModules/SimpleGallery/images/lightbox/lightbox-btn-close.gif',
  		imageBlank: '/DesktopModules/SimpleGallery/images/lightbox/lightbox-blank.gif',
  		txtImage: 'Afbeelding',
  		txtOf: 'van',
  		next: 'Volgende &raquo;',
  		previous: '&laquo; Vorige',
  		close: '[S]luiten',
  		txtPlay: 'Slideshow',
  		txtPause: 'Pauze',
  		keyToClose: 's',
  		keyToPrev: 'p',
  		keyToNext: 'v',
  		slideInterval: '5000',
  		hideTitle: false,
  		hideDescription: false,
  		hidePaging: false,
  		hideTags: false
  	}); // Select all links with lightbox class


	/*--------------------------------------------------------------------------------------------------------------
    LINE ABOVE LOGO OVERHEID WHEN MENU PRESENT
    --------------------------------------------------------------------------------------------------------------*/
	if($("#LeftPaneMenu:has(div)").length ==0) {
		$("#line_logo").removeClass("top_line");
		$("#line_logo").css("margin-top","-25px");
		}else{
		$("#line_logo").addClass("top_line");
	}
});



