$(document).ready(function()
{
    /*$('.clickable').click(function()
    {
        if (this.title)
            document.location = this.title;
    });*/

    $('.activatable').mouseover(function()
    {
        $(this).addClass('active');
    }).mouseout(function()
    {
        $(this).removeClass('active');
    });

    /* Show photo byline on mouseover */
    $('.img').mouseover(function()
    {
        $('.caption', this).stop(false, true);
        //        $('.captionicon', this).hide();
        $('.caption', this).show();
    }).mouseout(function()
    {
        $('.caption', this).fadeOut();
        //        $('.captionicon', this).show();
    });

    /* Active mainmenu items with current category */
    $('#mainmenu a[rel='+category+'],#mainmenu a[rel='+profile+']').removeClass('inactive').addClass('active');
    /* Activate submenu items with current category */
    $('.submenu a[rel='+category+'],.submenu a[rel='+profile+'],#newsmenu a[rel='+category+'],#newsmenu a[rel='+profile+']').addClass('active');

    /*$('#submenu li').bind("mouseenter", function() {
        $('ul', this).stop(false, true);
        $('ul', this).show();
    }).bind("mouseleave", function() {
        $('ul', this).fadeOut();
    });*/

    /* Animated menuitems */
    $('.menu>a.inactive').bind("mouseenter", function()
    {
        $('div', this).stop(false, true);
        $('div', this).fadeIn('fast');
    }).bind("mouseleave", function()
    {
        $('div', this).stop(false, true);
        $('div', this).fadeOut();        
    });    

    /* Submit poll with Ajax */
    $('form.pollmini').submit(function()
    {
        $('.loading', this).show();
        var id = this.ID.value;
        $.post(this.action, $(this).serialize(), function(data)
        {
            $('#pollmini' + id).html(data);
        });
        return false;
    });

    // Popup
    $('a.colorbox').colorbox({iframe: true, width: 642, height: 482, opacity: 0.75});
    $('a.colorbox-image').colorbox({opacity: 0.75, photo: true});
    $('a.colorbox-slideshow').colorbox({opacity: 0.75, photo: true, slideshow: true, slideshowSpeed: 5000, current: '{current} av {total}'});
    $('a.colorbox-sudoku').colorbox({iframe: true, width: 360, height:520, opacity: 0.75, scrollbars: false});
    $('a.colorbox-wow').colorbox({iframe: true, width: 640, height:650, opacity: 0.75});
    $('a.colorbox-scoreradar').colorbox({iframe: true, width: 1000, height:700, opacity: 0.75});

    // Slideshows, not preloaded
    $('a.slideshow-link').click(function()
    {
        var container = $('.slideshowimages', this.parentNode).empty();
        var loading = $('.slideshowloading', this.parentNode).show();
        loadSlideshow(this.href, container, loading, this);
        return false;
    });

    // Font resize
    $(".textsize").fontScaler({
        target: '.textresize',
        store:true,
        fixed: {character: '', size1: '100', size2: '150',size3: '200', title1: 'Normal', title2: 'Stor', title3: 'Extra stor'}
    });

    /* Timer for blog rotation */
    var rssitemCounter = 1;
    var rssitemCount = $('.puffhead').length;
    $(document).everyTime(10000, function()
    {
        var show = rssitemCounter < rssitemCount ? rssitemCounter + 1 : 1;
        $('#rssitem_' + rssitemCounter).fadeOut('normal', function()
        {
            $('#rssitem_' + show).fadeIn();
            rssitemCounter = show;
        });
    });

    $('#latest .menuitem').click(function() {
        $('#latest .box').hide();
        $('#latest .menuitem').removeClass('active');
        $(this).addClass('active');
        $('#'+this.id+'_box').show();
    });

    $('#latestbottom #newsmenu > a').click(function() {
        $('#latestbottom .box').hide();
        $('#latestbottom #newsmenu a').removeClass('active');
        $(this).addClass('active');
        $('#'+this.id+'_box').show();
        return false;
    });

    initInputfields();
    // Set background when page has finished loading
    $('body.bg').css("background","#d3d3d3");
});

function loadSlideshow(href, container, loading, link)
{
    $.getJSON(href, function(data) {
        $(container).append(
                $('<a>Bild ' + data.current + '</a>')
                        .attr({href: data.href, title: data.title, rel: data.id})
                        .addClass('colorbox-slideshow'));
        loading.html('Hämtar bild ' + data.current + '/' + data.total);
        if (data.next)
        {
            loadSlideshow(data.next, container, loading, link);
        }
        else
        {
            loading.hide();

            $(link).unbind('click').click(function() {
                $('a:first-child', container).click();
                return false;
            });

            $('a.colorbox-slideshow', container).colorbox({opacity: 0.75, photo: true, slideshow: true, open: true, slideshowSpeed: 5000, current: '{current} av {total}'});
        }
    });
}

function initInputfields() {
    $('.inputfield').focus(function() {
        if(this.value == this.defaultValue) this.value = '';
    }).blur(function() {
        if(this.value == '') this.value = this.defaultValue;
    });
}

String.prototype.beginsWith = function(t, i) {
    if (i==false) { return (t == this.substring(0, t.length)); }
    else { return (t.toLowerCase() == this.substring(0, t.length).toLowerCase()); }
};

String.prototype.endsWith = function(t, i) {
    if (i==false) { return (t == this.substring(this.length - t.length)); }
    else { return (t.toLowerCase() == this.substring(this.length - t.length).toLowerCase()); }
};
String.prototype.trim = function(){ return this.replace(/^\s+|\s+$/g,''); };

var maxLength = 500;

function checkMaxLength(e, maxLength) {
    if(e.value.length > maxLength)
    {
        alert('Max '+maxLength+' tecken!');
    }
    return e.value.length <= maxLength;
}

function updateCounter(e, counter, maxLength) {
    var currentLength = e.value.length;
	if (currentLength > maxLength)
        counter.addClass('toomuch');
    else
		counter.removeClass('toomuch');

    counter.html(currentLength);
}

function checkChecked(e, msg)
{
    if(!e.checked)
    {
        alert(msg);
        return false;
    }
    return true;
}