﻿$(document).ready(function() {
    $('.opaque').css('opacity', '0.8');
    $('blockquote p').append('<img src="images/quote_right.gif" style="margin-left: 3px;"/>');
    //$('body').append('<a style="position: absolute; top: 3px; right: 10px; font-size: 10px;" href="javascript:void(0);" onclick="if($(\'body\').css(\'background\')) { $(\'body\').css(\'background\', \'\'); } else { $(\'body\').css(\'background\', \'url(http://www.andybudd.com/images/layoutgrid.png)\'); }">View Grid</a>');
    $('.mask').css('opacity', 0);
    $('a.hover').each(function() {
        if ($(this).attr('on') != 'true') {
            var childImage = $(this).children('img')[0];
            //alert (childImage);
            if (childImage != null && childImage.src) {
                childImage.preloaded_image1 = new Image();
                childImage.preloaded_image1.src = childImage.src.replace(/^(.+)(\.[a-z]+)$/, "$1_over$2");
            }
        }
    })
        .mouseover(function() {
            if ($(this).attr('on') != 'true') {
                var childImage = $(this).children('img:first');
                //OLD: if (childImage!=null) $(childImage).src($(childImage).attr('base')+'_over.gif');
                if (childImage != null && childImage.length > 0) $(childImage).attr('src', $(childImage).attr('src').replace(/^(.+)(\.[a-z]+)$/, "$1_over$2"));
            }
        })
        .mouseout(function() {
            if ($(this).attr('on') != 'true') {
                var childImage = $(this).children('img')[0];
                //OLD: if (childImage!=null) $(childImage).src($(childImage).attr('base')+'.gif');
                if (childImage != null && $(childImage).attr('src') != null)
                    $(childImage).attr('src', $(childImage).attr('src').replace(/^(.+)_over(\.[a-z]+)$/, "$1$2"));
            }
        });
    $('a.fade_hover').each(function() {
        var childImage = $(this).children('img')[0];
        $(this).css('backgroundImage', 'url(' + $(childImage).attr('src') + ')');
        $(childImage).attr('src', $(childImage).attr('src').replace(/^(.+)(\.[a-z]+)$/, "$1_over$2"));
        $(childImage).css({ opacity: '0' });
    });
    $('div.hover').mouseover(function() {
        $(this).css('backgroundColor', '#bbb');
        $(this).find('a.fade_hover img').css('opacity', '1');
        $(this).css('color', '#000');
    })
        .mouseout(function() {
            $(this).css('backgroundColor', '');
            $(this).find('a.fade_hover img').css('opacity', '0');
            $(this).css('color', '');
        });


    $('.expanding').each(function() {
        if ($(this).siblings('ul').children().size() > 0) {
            $(this).attr('href', 'javascript:void(0)');

            var childImage = $(this).children('img')[0];
            if (childImage != null) {
                childImage.preloaded_image1 = new Image();
                childImage.preloaded_image1.src = childImage.src.replace(/^(.+)(\.[a-z]+)$/, "$1_on$2");
            }
        }
    });

    $('.main_navigation').accordion({}, function() { alert('done') });

    $('.toggle')
        .each(function() {
            $(this).attr('title', 'Click to read more.');
            if ($(this).attr('href') == null)
                $(this).attr('href', 'javascript:void(0);');
            var following = $(this).next();
            following.hide();
        })
        .click(function() {
            var following = $(this).next();
            following.slideToggle('fast');
        });
    if ($.fn.PopUpWindow) {
        $('.popup').PopUpWindow({ width: 975, height: 650 });
        $('.popup_register').PopUpWindow({ width: 580, height: 600, params: 'scrollbars=yes,resizable=yes' });
    }
});
$.fn.scrollHeight = function() {
    return parseInt(this.get(0).scrollHeight);
};
$.fn.accordion = function(options) {
    // options
    var SLIDE_DOWN_SPEED = 'slow';
    var SLIDE_UP_SPEED = 'fast';
    var startClosed = options && options.start && options.start == 'closed';
    var on = options && options.on && (typeof options.on == 'number' && options.on > 0) ? options.on - 1 : 0;
    return this.each(function() {
        $(this).addClass('accordion'); // use to activate styling
        $(this).find('li > a').click(function() {
            var otherLinks = $(this.parentNode).siblings('li').children('a');
            $(otherLinks).attr('on', 'false');
            var otherImages = $(otherLinks).children('img');
            $(otherImages).each(function() {
                $(this).attr('src', $(this).attr('base') + '.gif');
            });

            var current = $(this.parentNode).children('ul.sublinks');
            var others = $(this.parentNode).siblings('li').children('ul.sublinks:visible').slideUp(SLIDE_UP_SPEED);
            if (current.is(':visible')) {
                var childrn = $(this).children('img');
                childrn.attr('src', $(childrn).attr('base') + '_over.gif');
                $(this).attr('on', 'false');

                current.slideUp(SLIDE_UP_SPEED);
            }
            else {
                var childrn = $(this).children('img');
                childrn.attr('src', $(childrn).attr('base') + '_on.gif');
                $(this).attr('on', 'true');

                current.slideDown(SLIDE_DOWN_SPEED);
            }
        });
        if (!startClosed) {
            $(this).find('dd:eq(' + on + ')').slideDown(SLIDE_DOWN_SPEED);
        }
    });
};

// WINDOW DIMENSIONS
jQuery.WindowSize = function() {
    //
    // getPageSize()
    // Returns array with page width, height and window width, height
    // Core code from - quirksmode.org
    // Edit for Firefox by pHaez
    var xScroll, yScroll;

    if (window.innerHeight && window.scrollMaxY) {
        xScroll = document.body.scrollWidth;
        yScroll = window.innerHeight + window.scrollMaxY;
    } else if (document.body.scrollHeight > document.body.offsetHeight) {
        // all but Explorer Mac
        xScroll = document.body.scrollWidth;
        yScroll = document.body.scrollHeight;
    } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
        xScroll = document.body.offsetWidth;
        yScroll = document.body.offsetHeight;
    }

    var windowWidth, windowHeight;
    if (self.innerHeight) { // all except Explorer
        windowWidth = self.innerWidth;
        windowHeight = self.innerHeight;
    } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
        windowWidth = document.documentElement.clientWidth;
        windowHeight = document.documentElement.clientHeight;
    } else if (document.body) { // other Explorers
        windowWidth = document.body.clientWidth;
        windowHeight = document.body.clientHeight;
    }

    // for small pages with total height less then height of the
    if (yScroll < windowHeight) {
        pageHeight = windowHeight;
    } else {
        pageHeight = yScroll;
    }

    // for small pages with total width less then width of the viewport
    if (xScroll < windowWidth) {
        pageWidth = windowWidth;
    } else {
        pageWidth = xScroll;
    }

    arrayPageSize = new Array(pageWidth, pageHeight, windowWidth, windowHeight);
    return arrayPageSize;
};

jQuery.fn.totalY = function() {
    var result = 0;
    var element = this.get(0);
    if (element.offsetParent) {
        while (element.offsetParent) {
            result += element.offsetTop + (element.clientTop || 0);
            element = element.offsetParent;
            // Hack.. Seems it is a good idea to ignore forms in IE. Don't know why
            if (element.clientTop != null && element.tagName.toLowerCase() == "form") {
                element = element.offsetParent;
            }
        }
    }
    else if (element.y) {
        result += element.y;
    }
    return result;
};