﻿var roro = roro || {};

roro.market = (function ($, undefined) {

    var init = function () {

        var $wrapper = $('#mainContainer div.ourMarketMapArea');

        if ($wrapper.length === 0) { return; }

        var $image = $wrapper.find('img'),
			map = '<map name="Map"><area href="/OurMarket/" coords="1,1,163,61" alt="Back to map" target="_self" shape="RECT" title="Back to map"></map>',
			description = $('div.stenaTitle').text(),
			description = $.trim(description);

        $image
			.attr('usemap', '#Map')
			.attr('title', description)
			.attr('alt', description)
			.after(map);
    };

    return {
        init: init
    }

} (jQuery));


roro.imagemap = (function ($, undefined) {

    var init = function () {

        var $wrapper = $('.ourMarketMap');
        if ($wrapper.length === 0) { return; }

        // make the image map outline dissapear in IE
        // it is visible, but dissapears on click
        $wrapper.delegate('area', 'click', function () {
            $(this).blur();
        });
    };

    return {
        init: init
    }

} (jQuery));


roro.webpartListDiv = (function ($, undefined) {

    var init = function () {

        var $wrapper = $('#webpartListDiv');
        if ($wrapper.length === 0) { return; }

        // move stena challenger to further down the list, if the url contains "offering"
        /*if (/\/offering/gi.test(location.pathname)) {
        var stenaChallenger = $wrapper.find('li').eq(1).detach();
        $wrapper.find('li').eq(4).after(stenaChallenger);
        }*/

        var $anchors = $wrapper.find('a');
        $anchors.each(function () {
            var $this = $(this),

            // replace + by \+ since it is a regexp special char
				url = this.pathname.replace(/\+/g, "\\+"),
				re = new RegExp(url, 'i');


            // add a class to the current page									
            if (re.test(location.pathname)) {
                $this.addClass('selected');
            }

            // hide '2nd-Stena-Seabridger-Class-MkII-'
            if (this.href.search('2nd-Stena-Seabridger-Class-MkII-') !== -1) {
                $this.parent('li').remove();
            }

        });

        // hide erroneous link		
        $wrapper.find('a[href=/ourmarket/Pages/Head-Office-Göteborg.aspx]').parent('li').remove();

    };

    return {
        init: init
    }

} (jQuery));


roro.fleetlisthover = (function ($, undefined) {

    var init = function () {

        var $wrapper = $('#mainContainer div.fleetListHover');
        if ($wrapper.length === 0) { return; }

        // move stena challenger to further down the list
        /*var stenaChallenger = $('div.fleetListHover').eq(1).detach();
        $('div.fleetListHover').eq(4).after(stenaChallenger);*/

        var $anchors = $wrapper.children('a');
        $anchors.each(function () {
            var $this = $(this),
				pathname = this.pathname,
				pageTitle = pathname.substring(pathname.lastIndexOf('/') + 1)
									.replace(/-/g, ' ')
									.replace('.aspx', '');

            $this.attr('title', pageTitle);
        });

    };

    return {
        init: init
    }

} (jQuery));

roro.redirect = (function ($, undefined) {

    var init = function () {

        var $wrapper = $('#mainContainer a');
        if ($wrapper.length === 0) { return; }

        $wrapper.each(function () {
            var $this = $(this);

            if (this.href.search('2nd-Stena-Seabridger-Class-MkII-') !== -1) {
                this.href = this.href.replace('2nd-Stena-Seabridger-Class-MkII-', '1st-Stena-Seabridger-Class-MkII-');
            }
        });
    };

    return {
        init: init
    }

} (jQuery));


jQuery(document).ready(function () {
    roro.market.init();
    roro.imagemap.init();
    roro.webpartListDiv.init();
    roro.fleetlisthover.init();
    roro.redirect.init();
});
