﻿$j = jQuery.noConflict();

$j(document).ready(function () {
    // slide
    $j('.slidepanel').each(function () {
        $j('.content:not(.expanded)', $j(this)).hide();
        $j('h3', $j(this)).click(function () {

            // p = $j(this).parent('.slidepanel');
            var parents = $j(this).parents('.slidepanel');
            if (parents.length == 1) {
                var parent = parents.get(0);
                $j('.content', parent).slideToggle();
            }

            $j(this).toggleClass('expanded');

        });
    });

    $j('.webradio').expandable({ container_class: 'notice-info', label_expand: 'Klicka här för att lyssna', label_collapse: 'Dölj' });

    $j('#firstpage div.fourcol .inside').eq();
    //$j('#firstpage div.twocol').eq();

    /* carousel implementation */
    //$j('#carousel .img').altOverlay({container:$j('#carousel')});
    $j('#searchbox').watermark();

    $j('#contactform-cancel').click(function () {
        $j('#contactform .inside').slideToggle();
    });
    var cah = $j('#page').height();
    var cv = false;
    var x = $j('#contactform .inside').height();
    $j('#contact').click(function (e) {
        cv = !cv;
        $j('#contactform .inside').slideToggle();
        $j(this).toggleClass('expanded');
        if ($j.browser.msie && parseInt($j.browser.version) == 6) {	/* ie 6 height hack */
            if (cv) { $j('#page').css({ height: cah + x }); } else { $j('#page').css({ height: cah }); }
        }
        $j(this).blur();
        e.preventDefault();
    });
});

(function() {
    $j.fn.googleMap = function(arguments) {
        var options = $j.extend({ zoomLevel: 12 }, arguments);
        var self = this;
        var container = $j(this);
        if (options.width) { container.width(options.width); }
        if (options.height) { container.height(options.height); }

        var map = new google.maps.Map2(document.getElementById(container.attr('id')));
        if (options.controlType != '') {
            map.addControl(options.controlType);
        }
        map.setCenter(new google.maps.LatLng(options.lat, options.lng), options.zoomLevel);

        for (var i = 0; i < options.positions.length; i++) {
            var m = options.positions[i];
            var markerOptions = { icon: m.icon, title: m.title }
            var point;
            if (m.lat != 0.0 && m.lng != 0.0) {
                var point = new GLatLng(m.lat, m.lng);
            }
            else if (m.address) {
                var geocoder = new GClientGeocoder();
                geocoder.getLatLng(m.address, function(p) {
                    if (p) {
                        point = p;
                    }
                });
            }
            if (point) {
                var marker = new GMarker(point, markerOptions);
                map.addOverlay(marker);
                map.setCenter(point);
                var bubbleText = '<strong>' + m.title + '</strong><br/>' + m.description;
                marker.openInfoWindowHtml(bubbleText);
            }
        }

        return this;
    };
})(jQuery);

(function() {
    $j.fn.expandable = function(options) {

        $j.fn.expandable.defaults = {
            container_class: 'expandable',
            container_id: '',
            label_expand: 'Visa',
            label_collapse: 'Dölj'
        };

        var opts = $j.extend({}, $j.fn.expandable.defaults, options);

        return this.each(function() {

            $this = $j(this);
            
            $this.hide();
            
            var o = $j.meta ? $j.extend({}, opts, $this.data()) : opts;

            // Add the containing div with the
            $this.wrap("<div class='" + o.container_class + "'/>");
            $j("<a class='expandable_toggler' href='javascript:void'>" + o.label_expand + "</a>").insertBefore($this);

            $this.parent().children(".expandable_toggler").toggle(
	            function() {
	                $j(this).text(o.label_collapse);
	                $j(this).next().slideDown('slow');
	            },
	            function() {
	                $j(this).text(o.label_expand);
	                $j(this).next().slideUp('slow');
	            }
            );

        });

    };
})(jQuery);

(function() {
    var original;
    $j.fn.watermark = function() {
        var e = $j(this);
        if (!e) return;
        original = $j(this).attr('value');
        e.attr('watermark', original);
        e.focus(function() {
            //            console.log($j(this).attr('watermark'));

            if ($j(this).attr('value') == $j(this).attr('watermark')) {
                $j(this).attr('value', '');
            }
        });
        //        e.blur(function() {
        //                if ($j(this).attr('value')=='') { $j(this).attr('value', e.attr('watermark')); }
        //            }
        //        });
    }
})(jQuery);

(function() {
    $j.fn.altOverlay = function(opts) {
        var e = $j(this);
        if (!e) return;

        // get all images
        var imgs = $j('img', e);
        var overlay = injectOverlayMarkup(opts.container, opts.container.attr('id'));

        imgs.each(function() {
            // get alt
            var alt = $j(this).attr('alt');
            overlay.html(alt);
        });

        positionOverlay(e, overlay.attr('id'));
        overlay.show();
    }

    function positionOverlay(e, id) {
        var container = $j('#' + id);
        var h = container.outerHeight();
        var mh = e.height();
        container.css({ marginTop: mh - h });
    }

    function injectOverlayMarkup(e, id) {
        var div = $j('<div>');
        var id = id + '_container'
        div.attr('id', id);
        div.css({ opacity: 0.8, color: '#000', background: '#fff', position: 'absolute', padding: '8px', fontSize: '0.85em', width: '464px', display: 'none' });
        e.append(div);
        return div;
    }

})(jQuery);

(function() {
    /**
    * Sets all children of the element to equal heights.
    * @returns nothing, not chainable.
    */
    $j.fn.eq = function() {
        var e = $j(this);
        if (!e) return; // just to make sure we got an element...
        var max = get_maxHeight(e);

        // set the height on all children
        e.height(max);
        e.css('min-height', max);
        return e;
    }


    function get_maxHeight(children) {
        var max = 0;
        children.each(function() { if ($j(this).height() > max) { max = $j(this).height(); } });
        //children.each(function() { if ($j(this).offsetHeight > max) { max = $j(this).offsetHeight; } });
        return max;
    }
})(jQuery);

(function($) {
    $.fn.carouselX = function(args) {

        var settings = $.extend({}, args);
        var length = this.length;
        var current = 0;
        var slides = $(this);
        this.hide();
        var timer;
        var i = 0;
        var to = function(idx) {
            if (idx == current) return;
            $(slides[current]).fadeOut();
            $(slides[idx]).fadeIn();
            current = idx;
            resetTimer();
        }
        var next = function() {
            current = current++;
            if (current >= length) {
                current = 0;
            }
            to(current);
        }
        var prev = function() {
        };

        var resetTimer = function() {
            if (timer) { window.clearTimeout(timer); }
            timer = window.setInterval(function() { next(); }, 1000);
        };

        var self = $(this);


        var nav = $('<div id="navigator"></div>').css({ position: 'absolute', marginLeft: '480px', marginTop: '20px', paddingRight: '20px', textAlign: 'right', width: '460px' }).appendTo(settings.navigator);

        this.each(function() {
            var slide = $(this);
            if (i++ == current)
                slide.show();
            else
                slide.hide();
            slide.css({ position: 'absolute' });

            var a = $('<a href="javascript:void(0);">' + i + '</a>');
            var index = i - 1;
            a.click(function() {
                to(index);
                $(this).blur();
            });

            a.css({ color: 'white' }).appendTo(nav);
        });
        resetTimer();
    }
})(jQuery);

var $j = jQuery.noConflict();
(function($) {
    $j.fn.carousel2 = function() {
        index = 0;
        var offsetIndex = 0;
        var container = $j(this);
        var navcontainer;
        var isAnimating = false;
        var slides = $j('.slide', container);
        var buttons = [];

        if (slides.length > 0) { $j(slides[0]).show(); }
        slides.each(function() { $j(this).css({ left: 960 * offsetIndex++ }); });
        

        buildnav();
        updatenav();

        function go(args) {
            if (isAnimating) { return; }
            if (args.direction) {
                switch (args.direction) {
                    case 'next':
                        if (index < slides.length - 1) { index++; }
                        else { return; }
                        break;
                    default:
                        if (index > 0) { index--; }
                        else { return; }
                        break;
                }
            }
            if (args.target) { index = args.target - 1; }

            updatenav();

            isAnimating = true;
            /*
            for (var i = 0; i < slides.length; i++) {
            var left = (i - index) * 960;
            $j(slides[i]).animate({ left: left }, 'slow', 'linear', function() { isAnimating = false; });
            }
            */
            left = index * 960;
            $j('.wrapper', container).animate({ marginLeft: -left }, 'slow', 'linear', function() { isAnimating = false; });

        }

        function updatenav() {
            // var button_prev = $j('li.prev', navcontainer);
            if (index == 0) {
                buttons.prev.parent().addClass('disabled');
            } else if (index > 0 && buttons.prev.parent().hasClass('disabled')) {
                buttons.prev.parent().removeClass('disabled');
            }

            if (index == slides.length - 1) {
                buttons.next.parent().addClass('disabled');
            } else if (index < slides.length && buttons.next.parent().hasClass('disabled')) {
                buttons.next.parent().removeClass('disabled');
            }


            setactive();
        }

        function setactive() {
            $j('ul#slidenav li').removeClass('active');
            $j('ul#slidenav li:nth-child(' + parseInt(index + 2) + ')').addClass('active');
        }


        function buildnav() {

            navcontainer = $j('<ul>');
            navcontainer.attr('id', 'slidenav');

            /* back arr */
            buttons['prev'] = $j('<a>').attr('href', '#slide').click(function() { go({ direction: 'prev' }); });
            $j('<li>').addClass('prev').addClass('disabled').append(buttons.prev).appendTo(navcontainer);

            for (var i = 0; i < slides.length; i++) {
                var navitem = $j('<li>');
                var link = $j('<a>');
                link.text(i + 1);
                link.attr('href', '#slide=' + i);

                link.appendTo(navitem);
                navitem.appendTo(navcontainer);
            }

            setactive();

            buttons['next'] = $j('<a>').attr('href', '#slide').click(function() { go({ direction: 'next' }); }).appendTo(navcontainer);
            $j('<li>').addClass('next').append(buttons['next']).appendTo(navcontainer);

            $j('li:not(.next,.prev) a', navcontainer).click(function(e) {
                e.preventDefault();
                go({ target: $j(this).text() });
            })

            container.append(navcontainer);
        }

    }
})(jQuery);

