var $p = jQuery.noConflict();

$p(function() {
    $p('a[rel*=external]').click(function() {
        window.open(this.href);
        return false;
    });
});

$p(document).ready(function() {

    // fix ie css3	
    $p('.IE7 #getaquoteBox ul li:nth-child(2) a').css('background-position', '10px -79px');
    $p('.IE7 #getaquoteBox ul li:nth-child(3) a').css('background-position', '10px -219px');
    $p('.IE7 #getaquoteBox ul li:nth-child(4) a').css('background-position', '10px -149px');
    $p('.IE7 #getaquoteBox ul li:nth-child(5) a').css('background-position', '10px -289px');
    $p('.IE7 #weatherBox ul li:nth-child(2) a').css('background-position', '10px -639px');
    $p('.IE7 #weatherBox ul li:nth-child(3) a').css('background-position', '10px -709px');
    $p('.IE7 .newsItem:last-child').css('border', 'none');


    var imgDelay = 300;

    function hoverOver(location, bottom, left) {
        $p('#locationFade').stop(true, true).fadeIn('fast').removeClass().addClass(location);
        $p('#mapDot').show().css({ bottom: (bottom) + 'px', left: (left) + 'px' });
    }

    function hoverOut() {
        $p('#locationFade').delay(imgDelay).fadeOut('fast');
        $p('#mapDot').hide();
        if (currentPage) {
            mapOnLoad();
        }
    }

    // Left marina locations - specify class & co-ordinates
    $p('#mapWrap ul.leftList li:nth-child(1) a').hover(function() { hoverOver('eastbourne', 40, 217) }, function() { hoverOut(); });
    $p('#mapWrap ul.leftList li:nth-child(2) a').hover(function() { hoverOver('brighton', 40, 203) }, function() { hoverOut(); });
    $p('#mapWrap ul.leftList li:nth-child(3) a').hover(function() { hoverOver('chichester', 39, 176) }, function() { hoverOut(); });
    $p('#mapWrap ul.leftList li:nth-child(4) a').hover(function() { hoverOver('port-solent', 40, 171) }, function() { hoverOut(); });

    // Right marina locations - specify class & co-ordinates
    $p('#mapWrap ul.rightList li:nth-child(1) a').hover(function() { hoverOver('southsea', 38, 170) }, function() { hoverOut(); });
    $p('#mapWrap ul.rightList li:nth-child(2) a').hover(function() { hoverOver('gosport', 38, 167) }, function() { hoverOut(); });
    $p('#mapWrap ul.rightList li:nth-child(3) a').hover(function() { hoverOver('swanwick', 40, 166) }, function() { hoverOut(); });
    $p('#mapWrap ul.rightList li:nth-child(4) a').hover(function() { hoverOver('falmouth', 9, 31) }, function() { hoverOut(); });

    //set selected marina on map in case this is a marina page
    //Siva - All the marina pages are CMS driven, so the only way of checking current marina is from url
    //     - currentPage -- contains the url segment after http://domainname/pages which is set in page :/app/views/elements/static_modules/ourMarinas.thtml
    if (currentPage) {
        mapOnLoad();
    }

    function mapOnLoad() {
        var ismarinapage = false;
        var arrayMarinas = new Array('sovereign_harbour', 'brighton', 'chichester', 'portsolent', 'southsea', 'gosport', 'swanwick', 'falmouth');
        var arrayMarinasInMap = new Array(new Array('eastbourne', 40, 217), new Array('brighton', 40, 203),
        new Array('chichester', 39, 176), new Array('port-solent', 40, 171), new Array('southsea', 38, 170),
        new Array('gosport', 38, 167), new Array('swanwick', 40, 166), new Array('falmouth', 9, 31));
        for (var i = 0; i <= arrayMarinas.length - 1; i++) {
            //check if current page contains marina name
            if (currentPage.toLowerCase().indexOf(arrayMarinas[i]) != -1) {
                selectMarina(arrayMarinasInMap[i][0], arrayMarinasInMap[i][1], arrayMarinasInMap[i][2], i);
                ismarinapage = true;
                break;
            }
        }
        if (ismarinapage) {
            $p('#locationFade').removeAttr("style");
            $p('#locationFade').removeAttr("onclick");
        }
    }
    function selectMarina(location, bottom, left, index) {
        $p('#locationFade').stop(true, true).fadeIn('fast').removeClass().addClass(location);
        $p('#mapDot').show().css({ bottom: (bottom) + 'px', left: (left) + 'px' });
        if (index < 4)
            $p('#mapWrap ul.leftList li:nth-child(' + (index + 1) + ')').addClass("selected");
        else
            $p('#mapWrap ul.rightList li:nth-child(' + (index - 3) + ')').addClass("selected");
    }
});
