﻿String.prototype.startsWith = function (str)
{ return (this.match("^" + str) == str) }

$(document).ready(function () {

    $('#HgfSiteMap').css('left', ($(document).width() - 1024) / 2);

    // Display the light box when the "Innehåll A-Ö" is selected in top navigation
    DisplayLightBox('HgfSiteMapLink');

    // Display the light box when the "Innehåll A-Ö" is selected in the Navigation box
    DisplayLightBox('HgfSearchSiteLink');

    // Display the light box when the "Innehåll A-Ö" is selected in the footer
    DisplayLightBox('HgfSiteMapLinkFooter');

    ApplyCornersSelector(GetSettingsAll(5), ".rounded5");

    $('#slider').nivoSlider({
        effect: 'fade', // Specify sets like: 'fold,fade,sliceDown'
        slices: 15, // For slice animations
        boxCols: 8, // For box animations
        boxRows: 4, // For box animations
        animSpeed: 50, // Slide transition speed
        pauseTime: 8000, // How long each slide will show
        startSlide: 0, // Set starting Slide (0 index)
        directionNav: true, // Next & Prev navigation
        directionNavHide: true, // Only show on hover
        controlNav: true, // 1,2,3... navigation
        controlNavThumbs: false, // Use thumbnails for Control Nav
        controlNavThumbsFromRel: false, // Use image rel for thumbs
        keyboardNav: true, // Use left & right arrows
        pauseOnHover: true, // Stop animation while hovering
        manualAdvance: false, // Force manual transitions
        captionOpacity: 0.8, // Universal caption opacity
        prevText: 'Föregående', // Prev directionNav text
        nextText: 'Nästa', // Next directionNav text
        beforeChange: function () { }, // Triggers before a slide transition
        afterChange: function () { }, // Triggers after a slide transition
        slideshowEnd: function () { }, // Triggers after all slides have been shown
        lastSlide: function () { }, // Triggers when last slide is shown
        afterLoad: function () { } // Triggers when slider has loaded
    });
});

function DisplayLightBox(control) {

    $('#' + control + '').click(function (e) {
        e.preventDefault();
        if ($("#nyroModalContent").val() == null) {
            $.fn.nyroModalManual({
                minWidth: 929,
                minHeight: 361,
                url: '#HgfSiteMap',
                cssOpt: {
                    wrapper: {
                        border: 'none',
                        background: 'url(/_layouts/fs/images/sitemap/a_o_Dialog.png) no-repeat'
                    }
                }
            });
        }
    });
}
function equalHeight(group) {
    if (group.length == 0) return;
    var tallest = 0;
    group.each(function () {
        var thisHeight = $(this).height();
        if (thisHeight > tallest) {
            tallest = thisHeight;
        }
    });
    group.height(tallest);
}
function DisplayActive(elementSelector, activeSelector) {
    $(elementSelector).hide();
    $(activeSelector).show();
}
function RegisterTextBoxStandardText(elementSelector, standardText) {
    $(elementSelector)
        .bind("focus blur", function (event) { ShowHideStandardText(event, standardText); })
        .val(standardText);
}
function ShowHideStandardText(e, standardText) {
    var element = e.target || e.srcElement;
    var jq = $(element);
    if (jq.val().length == 0)
        jq.val(standardText);
    else if (jq.val() == standardText)
        jq.val('');    
}
function ShowHide(elementSelector) {
    $(elementSelector).each(function (index) {
        if ($(this).is(":visible"))
            $(this).hide();
        else
            $(this).show();
    })
}
function AddRemoveClass(elementSelector, cssClass) {
    $(elementSelector).each(function (index) {
        if ($(this).hasClass(cssClass))
            $(this).removeClass(cssClass);
        else
            $(this).addClass(cssClass);
    })
}
function ClickLinkOnEnter(e) {
    var code = (e.keyCode ? e.keyCode : e.which);
    var element = e.target || e.srcElement;
    if (code == 13) {
        $(element).parents('.searchbox').find('a').click();
        if (e.preventDefault) e.preventDefault();
        if (e.stopPropagation) e.stopPropagation();
    }
}
function ShowEmailForms(e) {
    var element = e.target || e.srcElement;
    var list = $(element).parents('.header').find('.emailForms');
    if (list.find('.headline').is(':visible')) {
        list.find('.headline').addClass('hidden');
        list.find('.form').removeClass('hidden');
    }
    else {
        list.find('.headline').removeClass('hidden');
        list.find('.form').addClass('hidden');
    }
}
function PostSearchQuestion(element, queryString, url, standardText) {
    var value = $(element).parents('.searchbox').find('input').val();
    if (queryString == "becomeMemberQuery") //Clean out string if it's a postal code
    {
        value = value.replace(/ /gi, "").replace(/-/gi, "");
    }
    if (value.length == 0 || value == standardText) return;
    window.location = url + '?' + queryString + '=' + encodeURIComponent(value);
}
function ActivatePanePS(span, activePaneId, parentClass) {
    $('#HgfProblemSolverPanes .paneHeadline').removeClass('active');
    $(span).addClass('active');
    ActivatePane(activePaneId, 'problemSolverPane', parentClass);

    if (activePaneId == 'HgfAskUsPane') _gaq.push(['_trackPageview', '/Fraga_Oss/Sidor/default.aspx?virtual']);
    if (activePaneId == 'HgfContactUsPane') _gaq.push(['_trackPageview', '/Kontakta_Oss/Sidor/default.aspx?virtual']);
    if (activePaneId == 'HgfBecomeMemberPane') _gaq.push(['_trackPageview', '/Bli_Medlem/Sidor/default.aspx?virtual']);
}
function ActivatePane(activePaneId, hiddenPaneClass, parentClass) {
    $('.' + hiddenPaneClass).addClass('hidden');
    $('#' + activePaneId).removeClass('hidden').parent().attr('class', parentClass);
}
function ApplyCornersSelector(settings, selector) {
    curvyCorners(settings, selector);
}
function ApplyCornersId(settings, id) {
    var divObj = document.getElementById(id)
    curvyCorners(settings, divObj);
}
function GetSettingsAll(radius) {
    return GetSettings(radius, radius, radius, radius);
}
function GetSettingsTopBottom(top, bottom) {
    return GetSettings(top, top, bottom, bottom);
}
function GetSettings(tl, tr, bl, br) {
    return settings = {
        tl: { radius: tl },
        tr: { radius: tr },
        bl: { radius: bl },
        br: { radius: br },
        antiAlias: true,
        autoPad: false
    }
}

/* BENEFITS */
function benefitsChangePage(dir) {
    var page = benefitsSearchCurrentPage;
    if (dir == 1) {
        page++;
    } else {
        page--;
    }

    if (page < 1) {
        page = 1
    };
    searchBenefits(page);
    benefitsSearchCurrentPage = page
}
/* BENEFITS */

/* ARTICLE FOOTER */
$(document).ready(function () {
    $(".printLink").hover(
      function () {
          $("#printIcon").attr("src", "/_layouts/fs/images/common/printIcon_hover2.png");
          $("#printLabel").css("text-decoration", "underline");
      },
      function () {
          $("#printIcon").attr("src", "/_layouts/fs/images/common/printIcon2.png");
          $("#printLabel").css("text-decoration", "");
      }
    );
      $(".addthis_button").hover(
      function () {
          $("#shareIcon").attr("src", "/_layouts/fs/images/common/shareIcon_hover2.png");
          $("#shareLabel").css("text-decoration", "underline");
      },
      function () {
          $("#shareIcon").attr("src", "/_layouts/fs/images/common/shareIcon2.png");
          $("#shareLabel").css("text-decoration", "");
      }
  );
});
/* END ARTICLE FOOTER */

/* START SEARCH */
$(document).ready(function () {

    // Modify the popup when no search word has been entered for the small search textbox
    $('#ctl00_fsSearchControl_S2A55C6F_go').click(function (e) {

        if ($("#ctl00_fsSearchControl_S2A55C6F_InputKeywords").val() == "Sök hyresgästföreningen.se") {
            e.preventDefault();
            alert("Vänligen ange ett sökord!");
        }
    });

    // Modify the popup when no search word has been entered pressing enter for the small search textbox
    $("#ctl00_fsSearchControl_S2A55C6F_InputKeywords").keydown(function (event) {

        if (event.which == 13 && $(this).val() == "") {
            event.preventDefault();
            alert("Vänligen ange ett sökord!");
        }
    });

    // Modify the popup when no search word has been entered for the large search textbox
    $('#ctl00_PlaceHolderFull_PlaceHolderMain_PlaceHolderMainColumn_fsSearchControlLarge_S62FDCE2C_go').click(function (e) {

        if ($("#ctl00_PlaceHolderFull_PlaceHolderMain_PlaceHolderMainColumn_fsSearchControlLarge_S62FDCE2C_InputKeywords").val() == "Sök hyresgästföreningen.se") {
            e.preventDefault();
            alert("Vänligen ange ett sökord!");
        }
    });

    // Modify the popup when no search word has been entered pressing enter for the small search textbox
    $("#ctl00_PlaceHolderFull_PlaceHolderMain_PlaceHolderMainColumn_fsSearchControlLarge_S62FDCE2C_InputKeywords").keydown(function (event) {

        if (event.which == 13 && $(this).val() == "") {
            event.preventDefault();
            alert("Vänligen ange ett sökord!");
        }
    });

    // Modify the mouse over text for the small/large search textboxes
    $('#ctl00_fsSearchControl_S2A55C6F_InputKeywords').attr('title', 'Sök');
    $('#ctl00_PlaceHolderFull_PlaceHolderMain_PlaceHolderMainColumn_fsSearchControlLarge_S62FDCE2C_InputKeywords').attr('title', 'Sök');

    // Modify the mouse over text for the search buttons (small and large)
    $('.srch-gosearchimg').attr('title', 'Sök');

});

/* END SEARCH */

/* START GA */
///////////////////
// _trackDownloads
jQuery(document).ready(function ($) {
    $.expr[':'].regex = function (elem, index, match) {
        var matchParams = match[3].split(','),
        validLabels = /^(data|css):/,
        attr = {
            method: matchParams[0].match(validLabels) ?
                        matchParams[0].split(':')[0] : 'attr',
            property: matchParams.shift().replace(validLabels, '')
        },
        regexFlags = 'ig',
        regex = new RegExp(matchParams.join('').replace(/^\s+|\s+$/g, ''), regexFlags);
        return regex.test(jQuery(elem)[attr.method](attr.property));
    }

    $('a:regex(href, \\.(zip|mp\\d+|mpe*g|pdf|docx*|pptx*|xlsx*|jpe*g|png|gif|tiff)$)').live('click', function (e) {
        _gaq.push(['_trackEvent', 'download', 'click', this.href.replace(/^.*\/\//, '')]);
    });
});

///////////////////
// _trackMailTo
jQuery(document).ready(function ($) {
    $('a[href^="mailto"]').live('click', function (e) {
        _gaq.push(['_trackEvent', 'email', 'send', this.href.replace(/^mailto:/i, '')]);
    });
});

///////////////////
// _trackOutbound
jQuery(document).ready(function ($) {
    $('a[href^="http"]:not([href*="//' + location.host + '"])').live('click', function (e) {
        _gaq.push(['_trackEvent', 'outbound', 'click', this.href.match(/\/\/([^\/]+)/)[1]]);
    });
});
/* END GA */
