var mapLngCoord;
var mapLatCoord;
var accName;
var accType;
var realPath
var myPathParts = window.location.pathname.split('/');
var myPath = '';


$(function () {
    myPath  = '';
    for (i = 0; i <= myPathParts.length - 1; i++) {
        if (i == myPathParts.length - 1) {
            if (myPathParts[i].indexOf('.') == -1 && myPathParts[i] != '') { myPath = myPath + myPathParts[i] + '/'; }
        }
        else {
            myPath = myPath + unescape(myPathParts[i]) + '/';
        }
    }
    myPath='www.yorkshirenet.co.uk'+myPath
    setMapVars(myPath);
    realPath = window.location.href;
    if (accName == null) { setMapVars(realPath.replace('http://', '')); }
    if (accName == null) { setMapVars(myPath.substring(0,myPath.length-1)); }
    if (accName == null) { setMapVars(myPath + 'index.htm'); }
    if (accName == null) { setMapVars(myPath + 'index.asp'); }
    if (accName == null) { setMapVars(myPath + 'page1.asp'); }
    $('.mapLink').ynShowAccMapLink();
    $(window).scroll(function () {
        $('.shownMap').center();
    });
    $(window).resize(function () {
        $('.shownMap').center();
    });
});

function setMapVars(path){
    $.ajax({
        type: "POST",
        url: "/ws/yorkshirenet.asmx/getAccommodationByWebsite",
        data: "{'websiteURL':'" + path + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        async: false,
        success: function(data) {
            mapLngCoord = data.d.mapLngCoord;
            mapLatCoord = data.d.mapLatCoord;
            accName = data.d.Name;
            accType = data.d.AccType;
        }
    });
}

(function ($) {
    $.fn.ynShowAccMapLink = function (options) {
        return this.each(function () {
            var settings = {};
            if (options) {
                $.extend(settings, options);
            }
            $('<div class="shownMap"><div class="shownMapTitle"><span class="closeShownMap" title="close map"></span></div><span class="mapFrame"></span></div>').insertAfter($(this));
            $('.shownMapTitle').live('click',function () { $('.shownMap').css({ 'display': 'none' }); });
                $('.shownMap').css({'position':'absolute','width':'480px','height':'634px','background-color':'#FFF','border':'1px solid #555','-moz-border-radius': '15px','border-radius': '15px'});
                $('.mapFrame').css({'margin':'0 0 0 5px'});
                $('.shownMapTitle').css({'background-image':'url("/YN3_files/images/siteDesign/ssheet.png")', 'background-position':'0px -71px','background-repeat':'repeat-x','height':'19px','color':'#FFF','padding':'4px 0 0 10px','font-weight':'700','font-size':'12px','-moz-border-radius-topleft': '15px','border-top-left-radius': '15px','-moz-border-radius-topright': '15px','border-top-right-radius': '15px','margin-bottom':'4px','cursor':'pointer'});

            $('.shownMap').css({'display':'none'});
            
            $(this).click(function (event) {
                event.preventDefault();
                $('.shownMap').css({'display':''});
		$('.shownMap').css("top", ($(window).scrollTop()+10)+"px");
                $('.shownMap').center();
                $('.shownMapTitle').html(accType+' Map');
                $('.mapFrame').html('<iframe src="http://www.yorkshirenet.co.uk/2011/accgde/showmap.asp?iZoom=10&iLat='+mapLatCoord+'&iLong='+mapLngCoord+'&iName='+accName+'&iType='+accType+'" width="470" height="600" frameborder="no" scrolling="no" marginheight="0" marginwidth="0"></iframe>');            });
        });
    };
})(jQuery);

jQuery.fn.center = function () {
    this.css("position","absolute");
    //this.css("top", ( $(window).height() - this.height() ) / 2+$(window).scrollTop() + "px");
    this.css("left", ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + "px");
    return this;
}
