﻿$(document).ready(function() {
    $("a[class='high'] span").append("&nbsp;»");
    $("a[class='high back'] span").prepend("«&nbsp;");

    $("tr.link td:first-child").addClass("first");
    $("tr.link td:last-child").addClass("last");


    $("tr.link").hover(
        function() {

            $(this).addClass("hover");
        },
        function() {

            $(this).removeClass("hover");
        }
    ).click(
        function(event) {
            var url = $(this).find("a").attr("href");

            if ('A' != $(event.target).parent().get(0).tagName) {
                // only redirect if a link has not been clicked to avoid double requests
                location.href = url;
            }
        }
    );

    cbb.init(); // Rounded corners

    $.datepicker.setDefaults($.datepicker.regional["sv"]);
    $("input.date").datepicker({ dateFormat: 'yy-mm-dd', duration: '', showOn: 'button', buttonImage: '/images/calendar.png', buttonImageOnly: true });
       
    // city(kommun) selector
    $('div.city_selector').click(function() {
        $(this).next().toggle();
    });

    var table = $('#selectcustomernetwork table#selectnetwork');
    
    if(table.length != 0){
        makeNetworkSelectorCollapsable(table);
        $('ul.city_selector_options a').click(function(event) {
            var city = $.trim($(this).html());
            expandCity(table, city);
            event.preventDefault();
        });
    }
});

function makeNetworkSelectorCollapsable(table) {     
    collapseAll(table);

    table.find('tr[class=newcity] td[class=city]').css('cursor', 'pointer').css('color', '#859D2C').click(function() {
        $(this).next().toggle().parent().nextUntil('tr[class=newcity]').toggle();
    });

    // Expand url provided city
    if (window.location.hash) {
        var city = window.location.hash.substring(1);
        expandCity(table, city);
    }
}

function expandCity(table, city) {
    collapseAll(table);
    table.find("td[class=city]:contains('" + city + "')").next().show().parent().nextUntil('tr[class=newcity]').show();
}

function collapseAll(table) {
    table.find('tr[class!=newcity]').hide();
    table.find('tr[class=newcity] td[class!=city]').hide();
}


