﻿/*
#################################################################################
Document Load Script 
#################################################################################
*/

function PopUpNewsCheck() {
    $(window).scroll(function() {
        if (isScrollNearBottom()) {
            //scroll is at the bottom
            //do something...
            $(".newsRelated").show("slow");
        }
        else {
            $(".newsRelated").hide("slow");
         }
        
    });
}


function isScrollNearBottom() {
    var documentHeight = $(document).height();
    var scrollPosition = $(window).height() + $(window).scrollTop();

    if ((documentHeight - scrollPosition) > 700) {
        return false;
    }
    else {
        return true;
    }
    
   
}


$(document).ready(function() {
   
    // Object Init Functions
    InitStyleSwitcherObjects();
    InitBookmarkPageObjects();
    ItemListSummaryImages_InitObjects();

    // Event Init Functions / Binding
    InitStyleSwitcherEvents();
    InitBookmarkPageEvents();
    ItemListSummaryImages_InitEvents();

    // Function Calls
    StyleSwitcherLoadPreferredStyle();


    $('a.author').click(function(ev) {

        var title = ($(this).attr('title'));
        $('#' + title).slideToggle("slow");

    });


    $('.demoForm input').click(function(ev) {
        if ($(this).attr('value') == "Name") {
            $(this).attr('value', ''); $(this).addClass('activeInput');
        }
        else if ($(this).attr('value') == "Email") {
            $(this).attr('value', ''); $(this).addClass('activeInput');
        }
        else if ($(this).attr('value') == "Company/Organisation") {
            $(this).attr('value', ''); $(this).addClass('activeInput');
        }
        else if ($(this).attr('value') == "Tell us a little about your needs") {
            $(this).attr('value', ''); $(this).addClass('activeInput');
        }
    });

    $('.demoForm textarea').click(function(ev) {

        if ($(this).attr('value') == "Tell us a little about your needs") {
            $(this).attr('value', '');
            $(this).addClass('activeInput');
        }
    });

});




$(window).load(function() {
    ItemListSummaryImages_AlignImages();
});

/*
#################################################################################
General Alignment Function
#################################################################################
*/

function ShowHide(container) {


    if ($(container).is(':visible')) {
        $(container).hide('300');
    
    }
    else {
        $(container).show('300');
    }

}


function ShowHide_CenterDiv(parent, object, vCenter, hCenter, positionAbsolute) {
    if (((parent != null) && (parent.length != 0)) && ((object != null) && (object.length != 0))) {
        var top = "";
        var left = "";

        /*var tempString = "parent.height(): " + parent.height() + ", parent.width(): " + parent.width() + "\n";
        tempString = tempString + "object.height(): " + object.height() + ", object.width(): " + object.width() + "\n";
        tempString = tempString + "parent.scrollTop(): " + parent.scrollTop() + ", parent.scrollLeft(): " + parent.scrollLeft();

		alert(tempString);*/

        top = (parent.height() - object.height()) / 2 + parent.scrollTop() + "px";
        left = (parent.width() - object.width()) / 2 + parent.scrollLeft() + "px";

        if (positionAbsolute == "true") {
            object.css("position", "absolute");
        }

        else {
            object.css("position", "relative");
        }

        if (vCenter == "true") {
            //alert("Aligning Top: " + top);
            object.css("top", top);
        }

        if (hCenter == "true") {
            //alert("Aligning Left: " + left);
            object.css("left", left);
        }
    }
}

/*
#################################################################################
Style Switcher 
#################################################################################
*/

var styleSwitcherId = "";
var styleSwitcherContainer = null;
var styleSwitcherButtons = null;

var cssLinkId = "";
var cssLink = null;

var coreCssLink = "";
var contrastCssLink = "";

var styleSwitcherCookieName = "";

function InitStyleSwitcherObjects() {
    styleSwitcherId = "styleswitcher";
    styleSwitcherContainer = $("div[id='" + styleSwitcherId + "']");
    styleSwitcherButtons = $("div[id='" + styleSwitcherId + "'] a");

    cssLinkId = "MainStyleSheet";
    cssLink = $("link[id='" + cssLinkId + "']");

    coreCssLink = "/includes/css/core.css";
    contrastCssLink = "/includes/css/Contrast.css";

    styleSwitcherCookieName = "PreferredStyle";
}

function InitStyleSwitcherEvents() {
    if ((styleSwitcherButtons != null) && (styleSwitcherButtons.length != 0)) {
        styleSwitcherButtons.click(function(event) {
            var cssFileToSwitchTo = $(this).attr("rel");

            if ((cssFileToSwitchTo != null) && (cssFileToSwitchTo.length != 0)) {
                SwitchStyle(cssFileToSwitchTo);
            }
            return false;
        });
    }
}

function SwitchStyle(switchTo) {
    if (((switchTo != null) && (switchTo.length != 0)) && ((cssLink != null) && (cssLink.length != 0))) {
        if (switchTo == "core.css") {
            cssLink.attr("href", coreCssLink);
            StyleSwitcherSavePreferredStyle("core.css");
        }

        else if (switchTo == "contrast.css") {
            cssLink.attr("href", contrastCssLink);
            StyleSwitcherSavePreferredStyle("contrast.css");
        }
    }
}

function StyleSwitcherSavePreferredStyle(preferredStyle) {
    if ((preferredStyle != null) && (preferredStyle.length != 0)) {
        $.cookie(styleSwitcherCookieName, preferredStyle);
    }
}

function StyleSwitcherLoadPreferredStyle() {
    if ($.cookie(styleSwitcherCookieName) != null) {
        SwitchStyle($.cookie(styleSwitcherCookieName));
    }
}

/*
#################################################################################
Favourites
#################################################################################
*/

var bookmarkPageClass = "";
var bookmarkPageButton = null;

function InitBookmarkPageObjects() {
    bookmarkPageClass = "bookmark";
    bookmarkPageButton = $("div[id='leftcontrol'] a." + bookmarkPageClass);
}

function InitBookmarkPageEvents() {
    if ((bookmarkPageButton != null) && (bookmarkPageButton.length != 0)) {
        bookmarkPageButton.click(function(event) {
            AddBookmark(bookmarkPageButton, event);
            event.preventDefault();
            return false;
        });
    }
}

function AddBookmark(linkObject, event) {
    if ((linkObject != null) && (linkObject.length != 0)) {
        if (window.opera) {
            if ($($(linkObject).attr("rel") != "")) {
                $(linkObject).attr("rel", "sidebar");
            }
        }

        var url = window.location;
        var title = document.title;

        // If Mozilla Firefox
        if (window.sidebar) {
            window.sidebar.addPanel(title, url, "");
            return false;
        }

        // If IE or Chrome
        else if (window.external) {
            // If IE
            if (document.all) {
                window.external.AddFavorite(url, title);
            }

            // If Chrome
            else {
                alert("Unfortunately, Google Chrome does not support this function. Please bookmark this page manually (ctrl + d)");
            }

            return false;
        }

        // If Opera
        else if (window.opera) {
            return false;
        }

        // Other
        else {
            alert("Unfortunately, this browser does not support the requested action, please bookmark this page manually");
            return false;
        }
    }
}

/*
#################################################################################
Summary Images in Item List - Align Center
#################################################################################
*/

var itemListClass = "";
var itemList = null;

var itemListSummaryImageContainerClass = "";
var itemListSummaryImageContainers = null;

var itemListSummaryImages = null;

function ItemListSummaryImages_InitObjects() {
    itemListClass = "ItemList";
    itemList = $("div." + itemListClass);

    itemListSummaryImageContainerClass = "ItemSummaryImage";
    itemListSummaryImageContainers = $("div." + itemListSummaryImageContainerClass);

    itemListSummaryImages = $("div." + itemListClass + " div." + itemListSummaryImageContainerClass + " img");
}

function ItemListSummaryImages_InitEvents() {
}

function ItemListSummaryImages_AlignImages() {
    if ((itemListSummaryImages != null) && (itemListSummaryImages.length != 0)) {
        $(itemListSummaryImages).each(function() {
            ShowHide_CenterDiv($(this).parent(), $(this), "true", "true", "false");
        });
    }
}
