﻿if (!NorkayWeb) var NorkayWeb = new Object();

/** FONTSETTINGS **/
Cufon.replace(['h1', 'h2', 'h4'], {
    fontFamily: 'Bodoni MT Black'
});

Cufon.replace(['.refspan span', '#menu a', '.co-details p', '#copyright p', 'span.type', 'label'], {
    fontFamily: 'Trade Gothic LT STD'
});

NorkayWeb.CoWorkers = function() {

    $.ajax({
        type: "POST",
        url: "/Ajax.aspx",
        data: { getCoWorkers: "now" },
        success: function(resp) {
            $("#specialcontent").html(resp);
            Cufon.refresh('h3');
        }
    });
}

//$('a[@rel*=lightbox]').lightBox();


$(function() {
    $('.refimage a').lightBox(
    {
        imageBtnClose: '../../img/lightbox-btn-close.gif',
        imageLoading: '../../img/lightbox-ico-loading.gif',
        imageBtnPrev: '../../img/prev.gif',
        imageBtnNext: '../../img/next.gif',
        fixedNavigation: true,

        txtImage: 'Bild',
        txtOf: 'av'
    });
});


/** INIT **/


function movr(el, hurl) {
    el.oldsrc = el.src;
    el.src = hurl;
}

function mout(el) {
    if (el.oldsrc)
        el.src = el.oldsrc;
}

/**
* Removes duplicates in the array 'a'
* @author Johan Känngård, http://johankanngard.net/
*/
function unique(a) {
    tmp = new Array(0);
    for (i = 0; i < a.length; i++) {
        if (!contains(tmp, a[i])) {
            tmp.length += 1;
            tmp[tmp.length - 1] = a[i];
        }
    }
    return tmp;
}

/**
* Returns true if 's' is contained in the array 'a'
* @author Johan Känngård, http://johankanngard.net/
*/
function contains(a, e) {
    for (j = 0; j < a.length; j++) if (a[j] == e) return true;
    return false;
}



function removeItems(array, item) {
    var i = 0;
    while (i < array.length) {
        if (array[i] == item) {
            array.splice(i, 1);
        } else {
            i++;
        }
    }
    return array;
}



$(document).ready(function() {

    // everything hinges on creating a string of class names, 
    // so i'll create a variable to hold that first

    var stringOfClassNames = '';

    // grab the class name of each list item to build that string
    $('.referenceitems > li').each(function(i) {
        var thisClassString = $(this).attr('class');
        stringOfClassNames = stringOfClassNames + ' ' + thisClassString
    });

    // now i have a space-delimited string of all class names stored
    // in the stringOfClassNames variable.	
    // Trim spaces from the ends of that string:
    stringOfClassNames = jQuery.trim(stringOfClassNames);

    // i can't really do anything with it until it's an array, so
    // convert that string to an array.
    var arrayClasses = stringOfClassNames.split(' ');


    // now for the isolating the filter that is common to all.
    // must do before extracting only the unique classes
    // one way to approach: count the number of times classes occur, and
    // if any occur the same number of times as how many list items i have,
    // assume that class is common to all list items, and remove it from
    // the filter list. duplicate class on same item = problem, but 
    // i'm not thinking about that right now.
    // i've also chosen sort the pre-unique'd array
    // instead of sorting the unique'd array.  i think i have to for the count.
    var arrayClasses = arrayClasses.sort();
    totalNumberOfItemsToFilter = $('.referenceitems > li').length;


    // borrowed from http://stackoverflow.com/questions/348021/counting-results-in-an-array
    // for counting up items.  do i even need this?
    var result = new Object();
    for (var filterClass in arrayClasses) {
        if (result[arrayClasses[filterClass]] === undefined) {
            result[arrayClasses[filterClass]] = 1;
        } else {
            result[arrayClasses[filterClass]]++;
        }
    }
    var resultsToRemoveFromFilters = new Array();
    for (var item in result) {
        if (result[item] == totalNumberOfItemsToFilter) {
            resultsToRemoveFromFilters.push(item);
        }
    }




    // pull out only unique values from that array.  Otherwise
    // i'll end up with duplicate filter checkboxes.
    arrayUniqueClasses = (unique(arrayClasses));


    // and now remove classes that appear in every result from my 'unique
    // classes' array
    for (x in resultsToRemoveFromFilters) {
        arrayUniqueClasses = removeItems(arrayUniqueClasses, resultsToRemoveFromFilters[x]);
    }



    // we only want to create filters if there are multiple classes. check
    // length of that array to see if it worth going forward.  I need at least
    // two, so my value has to be greater than 1.
    if (arrayUniqueClasses.length > 1) {

        // it must be worth it, because everything else is
        // within the true side of this if statement.	
        // so since we're going to have some filters, 
        // lets give them a place to live
        //$('<ul class="filters"><\/ul>').insertBefore('.filterThis');

        // then build the filter checkboxes based on all the class names
        

       

        // lets throw in the 'show all' checkbox for giggles
       

        // now lets give those filters something to do
        $('.filters input').click(function() {
            var value = $(this).val();
            if ((value == 'filterAll') && ($(this).is(':checked'))) {
                $('.filters input').attr('checked', 'checked');
                $('.referenceitems li').slideDown();
            } else {
                stringValue = '.referenceitems > li.' + value;
                stringValueOpposite = '.referenceitems > li:not(.' + value + ')';
                if ($(this).is(':checked')) {
                    classesOfItemToShow = '';
                    $(stringValue).fadeIn().each(function() {
                        classesOfItemToShow = classesOfItemToShow + ' ' + $(this).attr('class');
                    });
                    // trim spaces, then turn to an array, then
                    // exclude non-unique classes
                    classesOfItemToShow = jQuery.trim(classesOfItemToShow);
                    classesOfItemToShow = classesOfItemToShow.split(' ');
                    classesOfItemToShow = (unique(classesOfItemToShow));
                    if (classesOfItemToShow.length > 1) {
                        $.each(classesOfItemToShow, function() {
                            $('.filters input[value=' + this + ']').attr('checked', 'true');
                        });
                    }
                    if ($('.dynamicFilterInput').not(':checked').length == 0) {
                        $('#filterIDall').attr('checked', 'true');
                    };
                } else {
                    // all my new stuff goes here to tackle that 'webdev'
                    // and 'resources' class issue.
                    OtherClassesAssociatedWithTheItemToBeRemoved = '';
                    Oca = OtherClassesAssociatedWithTheItemToBeRemoved;
                    $(stringValue).each(function(i) {
                        Oca = Oca + ' ' + $(this).attr('class');
                    });

                    // trim spaces, then turn to an array, then
                    // exclude non-unique classes
                    Oca = jQuery.trim(Oca);
                    Oca = Oca.split(' ');
                    Oca = (unique(Oca));
                    if (Oca.length > 1) {
                        $.each(Oca, function() {
                            classToCompare = this;
                            if (!($('.' + classToCompare).is(stringValueOpposite))) {
                                // uncheck the checkbox that classToCompare represents
                                $('.filters input[value=' + classToCompare + ']').removeAttr('checked');
                            }
                        });
                    }
                    $(stringValue).fadeOut();
                    $('.filters #filterIDall').removeAttr('checked');
                }
            }
        });
    }
});































/** CAROUSEL ON FRONT PAGE - http://jqueryfordesigners.com/automatic-infinite-carousel/**/


(function() {
    $.fn.infiniteCarousel = function() {
        function repeat(str, n) {
            return new Array(n + 1).join(str);
        }

        return this.each(function() {
            // magic!
            var $wrapper = $('> div', this).css('overflow', 'hidden'),
                $slider = $wrapper.find('> ul').width(9999),
                $items = $slider.find('> li'),
                $single = $items.filter(':first')

            singleWidth = $single.outerWidth(),
                visible = Math.ceil($wrapper.innerWidth() / singleWidth),
                currentPage = 1,
                pages = Math.ceil($items.length / visible);

            /* TASKS */

            // 1. pad the pages with empty element if required
            if ($items.length % visible != 0) {
                // pad
                $slider.append(repeat('<li class="empty" />', visible - ($items.length % visible)));
                $items = $slider.find('> li');
            }

            // 2. create the carousel padding on left and right (cloned)
            $items.filter(':first').before($items.slice(-visible).clone().addClass('cloned'));
            $items.filter(':last').after($items.slice(0, visible).clone().addClass('cloned'));
            $items = $slider.find('> li');

            // 3. reset scroll
            $wrapper.scrollLeft(singleWidth * visible);

            // 4. paging function
            function gotoPage(page) {
                var dir = page < currentPage ? -1 : 1,
                    n = Math.abs(currentPage - page),
                    left = singleWidth * dir * visible * n;

                $wrapper.filter(':not(:animated)').animate({
                    scrollLeft: '+=' + left
                }, 500, function() {
                    // if page == last page - then reset position
                    if (page > pages) {
                        $wrapper.scrollLeft(singleWidth * visible);
                        page = 1;
                    } else if (page == 0) {
                        page = pages;
                        $wrapper.scrollLeft(singleWidth * visible * pages);
                    }

                    currentPage = page;
                });
            }

            // 5. insert the back and forward link
            //$wrapper.after('<a href="#" class="arrow back"></a><a href="#" class="arrow forward"></a>');

            // 6. bind the back and forward links
            $('a.back', this).click(function() {
                gotoPage(currentPage - 1);
                return false;
            });

            $('a.forward', this).click(function() {
                gotoPage(currentPage + 1);
                return false;
            });

            $(this).bind('goto', function(event, page) {
                gotoPage(page);
            });

            // THIS IS NEW CODE FOR THE AUTOMATIC INFINITE CAROUSEL
            $(this).bind('next', function() {
                gotoPage(currentPage + 1);
            });
        });
    };
})(jQuery);

$(document).ready(function() {
    // THIS IS NEW CODE FOR THE AUTOMATIC INFINITE CAROUSEL
    var autoscrolling = true;

    $('.infiniteCarousel').infiniteCarousel().mouseover(function() {
        autoscrolling = false;
    }).mouseout(function() {
        autoscrolling = true;
    });

    setInterval(function() {
        if (autoscrolling) {
            $('.infiniteCarousel').trigger('next');
        }
    }, 10000);
});

