﻿var currentIndex = 1;
var currentImageId = '#bgimg1';
var lastIndex = 4;
var ratio = 900 / 1440;
var imgIdPrefix = 'bgimg';
var imgGalleryLock = false;
var contentsToggleSpeed = 500;
var sitemapToggleSpeed = 500;
var isIE6 = false;

var expanded = true;
var slideLocked = false;
var originalHeight;
var galleryLock = false;
var galleryExpanded = true;
var bgImages;

function ToggleContents() {
    if (!slideLocked) {
        slideLocked = true;
        if (expanded) {
            if (!isIE6) $("#content").animate({
                left: '-=586'
            }, contentsToggleSpeed, function () {
                expanded = false;
                slideLocked = false;
                $('#contentcontrol a').attr("title", "Open the content");
                $('#contentcontrol a img').attr("src", "/style library/qualia/images/pages/viewcontent.png");
                //$('#content').height('200px');
                ConfigureGalleryControls(true);
            });
            else $("#content").animate({
                left: '-=622'
            }, contentsToggleSpeed, function () {
                expanded = false;
                slideLocked = false;
                $('#contentcontrol a').attr("title", "Open the content");
                $('#contentcontrol a img').attr("src", "/style library/qualia/images/ie6/viewcontent.gif");
                ConfigureGalleryControls(true);
            });


        } else {
            var moveLength = '+=586';
            if (isIE6) moveLength = '+=622';

            //$('#content').height('100%');
            $("#content").animate({
                left: moveLength
            }, contentsToggleSpeed, function () {
                expanded = true;
                slideLocked = false;
                $('#contentcontrol a').attr("title", "Close the content");
                if (bgImages.length == 1) {
                    if (!isIE6) $('#contentcontrol a img').attr("src", "/style library/qualia/images/pages/viewimage.png");
                    else $('#contentcontrol a img').attr("src", "/style library/qualia/images/ie6/viewimage.gif");
                } else {
                    if (!isIE6) $('#contentcontrol a img').attr("src", "/style library/qualia/images/pages/viewgallery.png");
                    else $('#contentcontrol a img').attr("src", "/style library/qualia/images/ie6/viewgallery.gif");
                }
            });
            ConfigureGalleryControls(false);
        }
    }
}

var sitemapExpanded = true;
var sitemapSlideLocked = false;
var sitemapToggleSpeed = 1000;

function ToggleSitemap() {
    if (!sitemapSlideLocked) {
        sitemapSlideLocked = true;
        if (sitemapExpanded) {
            $('#sitemap').show();
            $("#sitemap").animate({
                bottom: '0'
            }, sitemapToggleSpeed, function () {
                sitemapExpanded = false;
                sitemapSlideLocked = false; /*$('#contentcontrol').attr("title", "Open the content"); $('#contentcontrol img').attr("src", "/style library/qualia/images/pages/viewcontent.gif");*/
            });
        } else $("#sitemap").animate({
            bottom: '-=' + $('#sitemap').height()
        }, sitemapToggleSpeed, function () {
            sitemapExpanded = true;
            sitemapSlideLocked = false;
            $('#sitemap').hide(); /* $('#contentcontrol').attr("title", "Close the content"); $('#contentcontrol img').attr("src", "/style library/qualia/images/pages/viewgallery.gif");*/
        });
    }
}


function addListener(element, type, expression, bubbling) {
    bubbling = bubbling || false;
    if (window.addEventListener) { // Standard
        element.addEventListener(type, expression, bubbling);
        return true;
    } else if (window.attachEvent) { // IE
        element.attachEvent('on' + type, expression);
        return true;
    } else return false;
}

function ShowPreviousImage() {
    if (currentIndex - 1 > 0) ShowImage(currentIndex - 1);
    else if (!imgGalleryLock) {
        imgGalleryLock = true;
        RemoveItem(true);
        MoveImage(lastIndex, false, true, true);
    }
}

function ShowNextImage() {
    if (currentIndex + 1 <= lastIndex) ShowImage(currentIndex + 1);
    else if (!imgGalleryLock) {
        imgGalleryLock = true;
        RemoveItem(false);
        MoveImage(1, false, false, true);
    }
}

function MoveImage(imageIndex, removePrev, isLeftDirection, isSliding) {

    $('#loading').hide();

    $('#thumbgallery a').removeClass('selectedthumb');
    $('#thumb' + imageIndex).toggleClass('selectedthumb');

    var nexId = '#' + imgIdPrefix + imageIndex;
    var screenWidth = $(window).width();
    var browserwidth = $(window).width();
    var browserheight = $(window).height();
    var previousIndex = currentIndex;
    newItem = $(nexId);
    var directionChar = "-=";
    if (isLeftDirection) directionChar = "+=";

    // position the next image to show so it has the correct size in line with the current dimensions of the browser screen.
    if ((browserheight / browserwidth) > ratio) {
        $(newItem).height(browserheight);
        $(newItem).width(browserheight / ratio);
    } else {
        $(newItem).width(browserwidth);
        $(newItem).height(browserwidth * ratio);
    }

    $(newItem).css('top', (browserheight - $(newItem).height()) / 2);
    if (isLeftDirection) $(newItem).css('left', (screenWidth * -1) + 'px');
    else $(newItem).css('left', screenWidth + 'px');

    $(newItem).css("z-index", "20");
    $(newItem).css("position", "fixed");
    $('#bgimg' + previousIndex).css("z-index", "1");

    $(newItem).show();

    var leftValue = directionChar + screenWidth;
    $(newItem).animate({
        left: leftValue
    }, 600);
    $('#bgimg' + previousIndex).animate({
        left: leftValue
    }, 800, function () {

        $(window).unbind("resize");

        FullscreenrOptions = {
            width: 1440,
            height: 900,
            bgID: nexId
        };
        jQuery.fn.fullscreenr(FullscreenrOptions);

        currentImageId = nexId;
        currentIndex = imageIndex;

        var indexToAddArray = currentIndex;
        var indexToAddId = currentIndex + 1;
        if (isLeftDirection) {
            if (isSliding) {
                indexToAddArray = bgImages.length - 2;
                indexToAddId = bgImages.length - 1;
            } else if (currentIndex > 1) {
                indexToAddArray = currentIndex - 2;
                indexToAddId = currentIndex - 1;
            } else {
                indexToAddArray = bgImages.length - 1;
                indexToAddId = bgImages.length;
            }
        } else if (isSliding) {
            indexToAddArray = 1;
            indexToAddId = 2;
        } else if (currentIndex == lastIndex) {
            indexToAddArray = 0;
            indexToAddId = 1;
        }

        if (bgImages.length > 2) {
            var cacheImage = document.createElement('img');
            cacheImage.src = bgImages[indexToAddArray];
            cacheImage.id = "bgimg" + indexToAddId;
            cacheImage.setAttribute('class', 'backgroundimage');
            cacheImage.style.cssText = 'display:none;';

            if (isLeftDirection) $(currentImageId).before(cacheImage);
            else $(currentImageId).after(cacheImage);
        }

        if (removePrev) {
            $(this).remove();
            var cacheImage = document.createElement('img');
            if (isLeftDirection) {
                cacheImage.src = bgImages[currentIndex];
                cacheImage.id = "bgimg" + (currentIndex + 1);
            } else {
                cacheImage.src = bgImages[currentIndex - 2];
                cacheImage.id = "bgimg" + (currentIndex - 1);
            }

            cacheImage.setAttribute('class', 'backgroundimage');
            cacheImage.style.cssText = 'display:none;';

            if (isLeftDirection) $(currentImageId).after(cacheImage);
            else $(currentImageId).before(cacheImage);
        } else $(this).hide();
        imgGalleryLock = false;
    });
}

function RemoveItem(isLeftDirection) {
    if (bgImages.length > 2) {
        if (isLeftDirection) {
            if ((currentIndex + 1) <= bgImages.length) $("#bgimg" + (currentIndex + 1)).remove();
            else $("#bgimg1").remove();
        } else {
            if ((currentIndex - 1) > 0) $("#bgimg" + (currentIndex - 1)).remove();
            else $("#bgimg" + bgImages.length).remove();
        }
    }
}

function ShowImage(imageIndex, isSliding) {

    //if (!isIE6)
    //{
    if (!imgGalleryLock && imageIndex != currentIndex) {
        imgGalleryLock = true;
        if (imageIndex > (currentIndex + 1)) { //requested image is at least 1 image after the next image (which is currently loaded).
            RemoveItem(false);
            $('#' + imgIdPrefix + (currentIndex + 1)).remove(); // remove the next img in line as the request is for an image after it
            var cacheImage = document.createElement('img'); // create the requested image
            cacheImage.id = imgIdPrefix + imageIndex;
            cacheImage.setAttribute('class', 'backgroundimage');
            cacheImage.style.cssText = 'display:none;';
            addListener(cacheImage, 'load', function (e) {
                MoveImage(imageIndex, true, false, false);
            }, false);
            $(currentImageId).after(cacheImage);
            $('#loading').show();
            cacheImage.src = bgImages[imageIndex - 1];
        } else if (imageIndex < (currentIndex - 1)) {
            RemoveItem(true);
            $('#bgimg' + (currentIndex - 1)).remove();
            var cacheImage = document.createElement('img');
            cacheImage.id = "bgimg" + imageIndex;
            cacheImage.setAttribute('class', 'backgroundimage');
            cacheImage.style.cssText = 'display: none;';
            addListener(cacheImage, 'load', function (e) {
                MoveImage(imageIndex, true, true, false);
            }, false);
            $(currentImageId).before(cacheImage);
            $('#loading').show();
            cacheImage.src = bgImages[imageIndex - 1];
        } else {
            if (imageIndex > currentIndex) {
                RemoveItem(false);
                MoveImage(imageIndex, false, false, false);
            } else {
                RemoveItem(true);
                MoveImage(imageIndex, false, true, false);
            }
        }
    }
    //}
}

/**
 * Fullscreenr - lightweight full screen background jquery plugin
 * By Jan Schneiders
 * Version 1.0
 * www.nanotux.com
 **/
(function ($) {
    $.fn.fullscreenr = function (options) {
        if (options.height === undefined) alert('Please supply the background image height, default values will now be used. These may be very inaccurate.');
        if (options.width === undefined) alert('Please supply the background image width, default values will now be used. These may be very inaccurate.');
        if (options.bgID === undefined) alert('Please supply the background image ID, default #bgimg will now be used.');
        var defaults = {
            width: 1440,
            height: 900,
            bgID: 'bgimg'
        };
        var options = $.extend({}, defaults, options);
        $(document).ready(function () {
            $(options.bgID).fullscreenrResizer(options);
        });
        $(window).bind("resize", function () {
            $(options.bgID).fullscreenrResizer(options);
        });
        return this;
    };
    $.fn.fullscreenrResizer = function (options) {

        // configure the left vertical bar to go to the bottom
        //var newHeight = $('#contentarea').height() + 10; // $(document).height() - 188;
        //alert(newHeight); 
        
        //if (newHeight < originalHeight) $('#Verticalbar').height(originalHeight + 'px');
        //else $('#Verticalbar').height(newHeight + 'px');
        
        var contentHeight = $('#contentarea').height();
        var docHeight = $(document).height();
	    if (contentHeight <= 20 || (contentHeight + 167) < docHeight)
	    {
	    	$('#Verticalbar').height('auto');
	    	
	    	if ($.browser.msie && $.browser.version.substr(0,1)==8)
		    	$('#Verticalbar').height(($(document).height() - 172) + 'px');
		    else
			    $('#Verticalbar').height(($(document).height() - 167) + 'px');
	    }
	    
        // Set bg size
        var ratio = options.height / options.width;
        // Get browser window size
        var browserwidth = $(window).width();
        var browserheight = $(window).height();

        var selectorWidth = (browserwidth - 275) / 2;
        $('#DirectionControl').width(browserwidth - 273);
        $('#PrevImg1').width(selectorWidth);
        $('#PrevImg2').width(selectorWidth);
        $('#PrevImg1').height(browserheight);
        $('#PrevImg2').height(browserheight);

        $('#loading').css('left', ((browserwidth + 230) / 2) + 'px');
        $('#loading').css('top', (browserheight / 2) + 'px');

        // Scale the image
        if ((browserheight / browserwidth) > ratio) {
            $(this).height(browserheight);
            $(this).width(browserheight / ratio);
        } else {
            $(this).width(browserwidth);
            $(this).height(browserwidth * ratio);
        }
        // Center the image
        $(this).css('left', (browserwidth - $(this).width()) / 2);
        $(this).css('top', (browserheight - $(this).height()) / 2);
        return this;
    };
})(jQuery);

$(document).ready(function () {

/*$("div.LightBoxHtmlContent").each(function(index) {
        $(this).find("a.HtmlLightBox").fancybox('<b>THIS IS HTML content</b>',{
            'width': '75%',
            'height': '75%',
            'autoScale': false,
            'transitionIn': 'none',
            'transitionOut': 'none'
        });
    });*/

    $("div.LightBoxVideoContent").each(function (index) {
        $(this).find("a.VideoLightBox").fancybox({
            'width': '100%',
            'height': '100%',
            'autoScale': true,
            'transitionIn': 'none',
            'transitionOut': 'none',
            'type': 'iframe'
        });
    });


});

function ShowHTMLFancybox(htmlContent) {

    $.fancybox(htmlContent, {
        'padding': 10,
        'transitionIn': 'none',
        'transitionOut': 'none',
        'changeFade': 0,
        'titleShow': true
    });
}

function ShowImageFancybox(imagesArray) {
    $.fancybox(imagesArray, {
        'padding': 0,
        'transitionIn': 'none',
        'transitionOut': 'none',
        'type': 'image',
        'changeFade': 0,
        'titleShow': true
    });
}

function ShowVideoFancybox(videoArray) {

    $.fancybox(videoArray, {
        'width': '100%',
        'height': '100%',
        'autoScale': true,
        'transitionIn': 'none',
        'transitionOut': 'none',
        'type': 'iframe'
    });
}


function MoveItems(sourceControlId, destinationControlId, hiddenFieldId, moveAllItems, selectItemsControlId) {
    var selectedItemIds = "";
    var selector = ' option';
    if (!moveAllItems) selector += ':selected';

    $('#' + sourceControlId + selector).each(function () {
        $('#' + destinationControlId).append($("<option></option>").attr("value", $(this).val()).text($(this).text()));
        $(this).remove();
    });

    $('#' + selectItemsControlId + ' option').each(function () {
        selectedItemIds += $(this).val() + ',';
    });

    $('#' + hiddenFieldId).val(selectedItemIds);
    //alert($('#' + hiddenFieldId).val());
}

function SliderContent(parentId, idToShow) {
    if ($('#' + parentId + ' #' + idToShow).is(':hidden')) {
        $('#' + parentId + ' .SliderContentTitleSection').removeClass('active').next().slideUp(); //Remove all .trigger classes and slide up the immediate next container
        $('#' + parentId + ' #' + idToShow).prev().toggleClass('active');
        $('#' + parentId + ' #' + idToShow).slideDown(function () {
            var newHeight = $(document).height() - 188;
            $('#Verticalbar').height(newHeight + 'px');
        });

    }
    return false; //Prevent the browser jump to the link anchor
}

function ToggleGalleryThumbs() {
    if (!galleryLock && !galleryExpanded) {

        if ($.browser.msie && $.browser.version.substr(0, 1) == 7) $("#showgallerythumbs").css('padding-bottom', '0px');
        else $("#showgallerythumbs").css('margin-bottom', '10px');

        $("#thumbgallery").show();
        galleryLock = true;
        $("#gallerycontrol").animate({
            bottom: '0'
        }, 500, function () {
            $('#showgallerythumbs').html("HIDE IMAGE THUMBNAILS").attr("title", "Click to hide the image thumbnails");
            galleryExpanded = true;
            galleryLock = false;
        });
    } else if (!galleryLock && galleryExpanded) {
        galleryLock = true;
        $("#gallerycontrol").animate({
            bottom: '-=' + ($("#gallerycontrol").height() - 25)
        }, 500, function () {
            $('#showgallerythumbs').html("SHOW IMAGE THUMBNAILS").attr("title", "Click to show the image thumbnails");
            galleryExpanded = false;
            galleryLock = false;

            if ($.browser.msie && $.browser.version.substr(0, 1) == 7) $("#showgallerythumbs").css('padding-bottom', '72px');
            else $("#showgallerythumbs").css('margin-bottom', '72px');

            $("#thumbgallery").hide();
        });
    }
}

function HideGalleryThumbs() {
    if (galleryExpanded && !galleryLock) {
        galleryLock = true;
        $("#GalleryControl").animate({
            bottom: '-=' + ($("#GalleryControl").height() - 25)
        }, 500, function () {
            $('#showgallerythumbs').html("SHOW IMAGE THUMBNAILS");
            galleryExpanded = false;
            galleryLock = false;
        });
    }
}

function ConfigureGalleryControls(showControls) {
    if (bgImages.length > 1) {
        if (showControls) {
            $('#gallerycontrol').fadeIn('fast');
            if (!isIE6) $('#DirectionControl').show();
        } else {
            $('#gallerycontrol').fadeOut('fast');
            $('#DirectionControl').hide();
        }
    }
}

function SlideToggleWebPart(bodySectionId) {
    // (!webPartSlideLock)
    //
    //webPartSlideLock = true;
    var iconControl = $('#' + bodySectionId).prev().children().eq('1');
    if ($('#' + bodySectionId).is(':visible')) {
        iconControl.addClass('contractedicon');
        iconControl.removeClass('expandedicon');

    } else {
        iconControl.addClass('expandedicon');
        iconControl.removeClass('contractedicon');
    }
    var prevHeight = $(document).height();
    $('#' + bodySectionId).slideToggle('slow', function () {
        

    }); }

function InitialiseBackgroundGallery() {
    GetBackgroundImages();
    $('#showgallerythumbs').click(ToggleGalleryThumbs);
    lastIndex = bgImages.length;

    $('#PrevImg1').mouseenter(function () {
        $('#PrevImgP').show()
    }).mouseleave(function () {
        $('#PrevImgP').hide()
    }).mousemove(function (e) {
        $('#PrevImgP').css('left', (e.pageX - 250) + 'px').css('top', (e.pageY - 220) + 'px');
    });
    $('#PrevImg2').mouseenter(function () {
        $('#PrevImgN').show()
    }).mouseleave(function () {
        $('#PrevImgN').hide()
    }).mousemove(function (e) {
        $('#PrevImgN').css('left', (e.pageX - 250) + 'px').css('top', (e.pageY - 220) + 'px');
    });

    if (bgImages.length == 1) {
        if (!isIE6) $('#contentcontrol a img').attr("src", "/style library/qualia/images/pages/viewimage.png");
        else $('#contentcontrol a img').attr("src", "/style library/qualia/images/ie6/viewimage.gif");
    }
	$('#contentcontrol a').show();
}

function InitialiseMenu() {
    var marginRight = $('#mainmenu').css('margin-right');
    var size = $('#mainmenu>ul>li').size();
    var oneSidePadding = 3;
    var fullMenuWidth = oneSidePadding + size * 85 + (size * (2 * oneSidePadding));
    var counter = 0;
    var leftPosition = 0;
    var items;
    $('#mainmenu>ul').children().each(function () {
        items = $(this).find('.submenu');
        if (items.size() == 1) {
            leftPosition = oneSidePadding + counter * 85 + (counter * (2 * oneSidePadding)); // includes padding between the menu items
            if ($(items[0]).width() + leftPosition > fullMenuWidth) $(items[0]).css('right', marginRight);
        }
        counter++;
    });

    // main menu drop down
    $("#mainmenu>ul>li").hover(function () {
        $('ul.submenu', this).show();
    }, function () {
        $('ul.submenu', this).hide();
    });

    //Set default open/close settings
    $('.SliderContentBodySection').hide(); //Hide/close all containers
    $('.SliderContentTitleSection:first').addClass('active').next().show(); //Add "active" class to first trigger, then show/open the immediate next container
}

function InitialisePage() {
    $('#sitemap').css('bottom', '-' + $('#sitemap').height() + 'px'); // initialise the site map
    
    //var contentHeight = $('#contentarea').height();
    //alert(contentHeight);
    
    //if (contentHeight <= 20)
    //{
    //	$('#Verticalbar').height(($(document).height() - 167) + 'px');
    //}
    
    /*
    var newHeight = $('#contentarea').height();
    if ($('#content').length > 0) {
        // for chrome and ff images haven't loaded just yet
        if ($(document).height() > newHeight) {
            if ($.browser.msie && $.browser.version.substr(0, 1) < 7) newHeight = $(document).height() - 100;
            else if ($.browser.msie) newHeight = $(document).height() - 210;
            else newHeight = $(document).height(); // - 167;
        }
    } else {
        if ($.browser.msie && $.browser.version.substr(0, 1) == 7) newHeight = $(document).height() - 207;
        else if ($.browser.msie && $.browser.version.substr(0, 1) < 7) newHeight = $(document).height() - 100;
        else if ($.browser.msie) newHeight = $(document).height() - 210;
        else newHeight = $(document).height() - 167;
    }
    originalHeight = newHeight;

    $('#Verticalbar').height((newHeight ) + 'px');*/
}
