function reset(id){
    var count = 0;
    while (count < 11) {
        count++;
        if (count!=id) {
            //$('#c' + count).attr('src', 'http://cdn2.weddingstar.com/images/maps/' + count + '.gif');
            $('#c' + count).attr('src', '/images/maps/' + count + '.gif');
            $('#si' + count).hide();
            $('#sh' + count).hide();
        }
    }
    $('#si' + id).show();
    $('#sh' + id).show();
}

function getLeaf(url) {
    var split = url.split('?');// remove all the parameter from url
    url = split[0];
    return url.substring(url.lastIndexOf("/") + 1);// return file name without domain and path
} 

jQuery.fn.extend({
    enter: function() {//plugins creation
        return this.each(function() {
            var path = $(this).find("img")[0];
            if($(this).children().attr("href") == getLeaf(document.location.href)){// check that the link url and document url is same
                $(path).attr("src",path.src.replace(/.gif/g, '_active.gif'));
                box.style.display = "block";
            } else {
                $(this).hover(function() {
                    $(this).fadeTo("medium", 1.0); // This should set the opacity to 100% on hover
                    var src5 = path.src;
                    var selected = src5.search(/selected/i);
                    if (selected == '-1') {
                        $(path).attr("src", path.src.replace(/.gif/g, '_active.gif'));// mouse over Image
                    }
                }, function() {
                    $(this).fadeTo("medium", 0.8); // This should set the opacity back to 30% on mouseout
                    $(path).attr("src", path.src.replace(/_active.gif/g, '.gif'));// mouse out image

                });
            }
        });
    }
});

$(document).ready(function() {
    var updateMap = function() {
        var id = $(this).attr('id');
        var ch = id.slice(1);
        // stop timers on selected element
        $(this).stopTime();
        reset(ch);
        $('#c' + ch).attr('src', '/images/maps/' + ch + '_selected.gif');

    };

    var shippingFunc = function(e) {
        e.preventDefault();
        var url = jQuery(location).attr('hostname');
        var parse = url.split('.');
        if ((parse[0].indexOf('www') >= 0 && parse[1].indexOf('weddingstar') >= 0)
            || parse[0].indexOf('wholesale') >= 0){
            dialog('', '/shipping-map/index', {}, '#cs', function(data) {
                $('#shipping-content .view', data.html).click(updateMap).mouseover(updateMap);
            });
            $("#shipping-content .mapi").fadeTo("medium", 0.8); // This sets the opacity of the thumbs to fade down to 30% when the page loads
            $("#shipping-content #map a").enter();// call the function
        } else {
            window.location = '/faqs#shippingcosts';
        }
    };
    
    $('#low_rates').click(shippingFunc);
    $('#shipping-start').click(shippingFunc);
    $('#top-shipping-start').click(shippingFunc);
    $('#shipping_cost').live('click',shippingFunc);
    $('span.shipping_map').live('click', shippingFunc);
    $('#shipping_map').live('click', shippingFunc);

    $('#shipping-content .view').live('click', updateMap).live('mouseover', function() {
        $(this).oneTime(1000,updateMap);
        
    }).live('mouseout', function () {
        $(this).stopTime();
    });

    $('#shipping-content span.info').live('mouseover', function() {
        $(this).addClass('hover');
    }).live('mouseout', function() {
        $(this).removeClass('hover');
    }).live('click', function() {
        $('#all-info').show();
        $('#all-info').animate({
            width: '580px'
        }, "1000", "easeOutBounce");
    });

    $('#shipping-content #all-info img').live('click', function() {
        $('#all-info').animate({
            width: '0px'
        }, "1000", "easeOutBounce", function() {
            $('#all-info').hide();
        });
    });
});

$("#shipping-content").livequery(function() {
    $(this).ready(function() {
        if ($("#country_image").children(0).attr("title") == "Weddingstar UK") {
             $("#c10").attr('src','/images/maps/10_selected.gif');
             $("#si10").css('display','block');
             $("#sh10").css('display','block');
             $("#si1").css('display','none');
             $("#sh1").css('display','none');
        }
    });
});

