/* ********************************************************************** */
/* *                                                                    * */
/* * jQuery functions                                                   * */
/* *                                                                    * */
/* ********************************************************************** */
(function($) {
    /**
     * Return the class, starting with 'page' of an object
     *
     * @return  string  The page..
     */
    $.fn.get_page_class = function() {
        var classes = this.attr('class').split(' ', 10);

        var value;
        for (key in classes) {
            value = classes[key];
            if (value.indexOf('page')===0) {
                return value;
            }
        }
        return '';
    }

    var cache = [];
    /**
     * Image preload
     *
     * Arguments are image paths relative to the current page.
     */
    $.preLoadImages = function() {
        var args_len = arguments.length;
        for (var i = args_len; i--;) {
            var cacheImage = document.createElement('img');
            cacheImage.src = arguments[i];
            cache.push(cacheImage);
        }
    }

})(jQuery)

/* ********************************************************************** */
/* *                                                                    * */
/* * Basic functions                                                    * */
/* *                                                                    * */
/* ********************************************************************** */
/**
 * Open a link to the frontpage
 *
 * @param   that    object  The <li> item
 */
homelink_click = function(that) {
    link = $(that).children('a').attr('href');
    if (!link) {return true;} //Fallback
    $('#header').animate({
        marginTop: '140px'
    }, 500, function() {
        window.location = link;
    });
    return false;
}

/**
 * Open a link to a page, except the frontpage
 *
 * @param   that    object  The <li> item
 */
pagelink_click = function(that) {
    link = $(that).children('a').attr('href');
    if (!link) {return true;} //Fallback
    $('#header').animate({
        marginTop: '22px'
    }, 500, function() {
        window.location = link;
    });
    return false;
}

/* ********************************************************************** */
/* *                                                                    * */
/* * Events                                                             * */
/* *                                                                    * */
/* ********************************************************************** */

$(document).ready(function() {

    /**
     * Add to page link effects
     */
    $('li.topalnk').hover(
        function () {
            var the_page = $(this).get_page_class();
            $('div#header-image').addClass(the_page);
        },
        function () {
            var the_page = $(this).get_page_class();
            $('div#header-image').removeClass(the_page);
        }
    );

    /**
     * Page logo change on hover
     */
    $('#header-left a img').hover(
        function () {
            $(this).attr('src', '/images/header1.png');
            $('#frontpage').fadeIn('fast');
        },
        function () {
            $(this).attr('src', '/images/header0.png');
            $('#frontpage').fadeOut('fast');
        }
    );

    jQuery.preLoadImages('/images/Home.png', '/images/Who.png', '/images/What.png');
    jQuery.preLoadImages('/images/Webdesign.png', '/images/SEO.png', '/images/eCommerce.png');
    jQuery.preLoadImages('/images/Contact.png', '/images/header0.png', '/images/header1.png');

});

