var CWC = function() {

    var WelcomeCenters = [
        'Alpine',
        'Anderson',
        'Arcata',
        'Auburn',
        'Barstow',
        'BuenaPark',
        'ElDoradoHills',
        'MammothLakes',
        'Merced',
        'Oceanside',
        'Oxnard',
        'PismoBeach',
        'Salinas',
        'SanBernardino',
        'SanFrancisco',
        'SanMateo',
        'SantaRosa',
        'Truckee',
        'Tulare',
        'YuccaValley'
    ];

    var map = null;

    return {
        init : function()
        {
            $("#CWCImageMap area").removeAttr('href').css('cursor','pointer');
            $("#CWCImageMap area").click(function(e){
                var x = e.pageX;
                var y = e.pageY;
                $("#regionInfoDiv").css({
                    top: y,
                    left: x,
                    width: 1,
                    height: 1
                });
                var region = $(this).attr('alt').replace(/\s/g,"");
                CWC.showRegionInfoBox(region);

                CWC.getWCHomeContent(region);
            });

            // pre-load map images
            jQuery.each(WelcomeCenters, function() { jQuery('<img>').attr('src', 'images/map_' + this + '.png'); });

            // pre-load popup content
            jQuery.each(WelcomeCenters, function() { CWC.setContent([this, 'CWCPopup'], $('#' + this + 'PopupText')); });

            // pre-load popup images
            jQuery.each(WelcomeCenters, function() { CWC.setMedia(this + '_popup.jpg', 92, 109, $('#' + this + 'PopupImage')); });
        },

        toggleMapImage : function(imageID)
        {
            $('#CWCMapImage').attr('src', 'images/map_' + imageID + '.png');
        },

        setContent : function(tags, container)
        {
            $.ajax({
                type: "POST",
                url: "ajax/CMSDataService.asmx/GetContent",
                data: JSON.stringify({ tagList: tags }),
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function(result) {
                    if (result.hasOwnProperty('d'))
                        result = result.d;
                    container.html(result);
                }
            });
        },

        setMedia : function(fileName, width, height, media)
        {
            $.ajax({
                type: "POST",
                url: "ajax/CMSDataService.asmx/GetMedia",
                data: JSON.stringify({ fileName: fileName, width: width, height: height }),
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function(result) {
                    if (result.hasOwnProperty('d'))
                        result = result.d;
                    media.attr('src', result);
                }
            });
        },

        showRegionInfoBox : function(region)
        {
            var content = $('#' + region + '_InfoContent');

            var parentPos = $("#CWCMap").offset();
            var width = 205;
            var height = 109;
            var toTop;
            var toLeft;

            if (content.hasClass('top')) {
                toTop = parentPos.top + 3;
                toLeft = parentPos.left + $("#CWCMap").width() - width;
            } else {
                toTop = parentPos.top + $("#CWCMap").height() - height + 30;
                toLeft = parentPos.left - 50;
            }

            $("#regionInfoDiv .infoContent").empty();
            $("#regionInfoDiv .infoContent").append(content.html());
            $("#regionInfoDiv .infoContent").show();
            $("#regionInfoDiv").animate({
                    width: width,
                    height: height,
                    left: toLeft,
                    top: toTop
            }, 450);
        },

        getWCHomeContent : function(region)
        {
            $("#HomeContentGMap").hide();
            $('#HomeContentText').text('');
            loadImage = $('<img/>').attr('src', 'images/ajax-loader.gif').attr('class', 'floatLeft').attr('style', 'margin-right: 10px');
            $('#HomeContentText').append(loadImage)
            loadText = $('<p/>').text('Loading Welcome Center Information...');
            $('#HomeContentText').append(loadText);

            $.ajax({
                type: "POST",
                url: "ajax/CMSDataService.asmx/GetHomeContent",
                data: "{ 'wc': '" + region + "' }",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function(result) {
                    if (result.hasOwnProperty('d'))
                        result = result.d;
                    CWC.setWCContent(result, region);
                }
            });
        },

        setWCContent : function(contentArray, region)
        {
            // add welcome center heading
            wcNameLink = $('<a/>').attr('href', contentArray[0] + '/').attr('class', 'red').text(contentArray[1]);
            wcName = $('<h2/>').html(wcNameLink);
            $('#HomeContentText').html(wcName);
            // add welcome center content
            wcContent = $('<div/>').attr('id', 'HomeWCContent').html(contentArray[3]);
            $('#HomeContentText').append(wcContent);
            // add welcome center address / contact info
            wcContact = $('<div/>').attr('id', 'HomeWCContact').attr('class', 'dark').html(contentArray[4]);
            // add directions link
            wcDirectionsLink = $('<a/>').attr('href', region + '/GettingHere/#map').html($('<p/>').attr('class', 'red').text('click here for directions'));
            wcClick = $('<span/>').attr('class', 'HomeContentClick bold').append(wcDirectionsLink);
            wcContact.append(wcClick);
            $('#HomeContentText').append(wcContact);
            // update welcome center image
            $("#HomeContentImage").attr('src', contentArray[2]);

            map = new GMap2($("#HomeContentGMap")[0], { size: new GSize(240, 180) });
            var point = new GLatLng(contentArray[5].lat, contentArray[5].lng);
            map.setCenter(point, 14);
            var marker = new GMarker(point);
            map.addOverlay(marker);

            $("#HomeContentGMap").show();
        },

        toggleResultListItem : function(item)
        {
            $('#ResultList' + item).toggle();
        },

        getDateLabel : function(date, includeDay)
        {
            regex = /\D*(\d*)/;
            time = regex.exec(date)[1];
            date = new Date(parseInt(time));

            label = '';
            if (includeDay)
                label += this.getDayName(date) + ', ';
            label += date.getMonth() + 1 + '/' + date.getDate() + '/' + date.getFullYear();

            return label + ' ' + this.getTime(date);
        },

        getDayName : function(date)
        {
            name = '';

            switch (date.getDay())
            {
                case 0:
                    name = 'Sunday';
                    break;
                case 1:
                    name = 'Monday';
                    break;
                case 2:
                    name = 'Tuesday';
                    break;
                case 3:
                    name = 'Wednesday';
                    break;
                case 4:
                    name = 'Thursday';
                    break;
                case 5:
                    name = 'Friday';
                    break;
                case 6:
                    name = 'Saturday';
                    break;
            }

            return name;
        },

        getTime : function(date)
        {
            hour = date.getHours();
            if (hour >= 12)
            {
                if (hour > 12)
                    hour -= 12;
                ampm = 'pm';
            }
            else
            {
                if (hour == 0)
                    hour += 12;
                ampm = 'am';
            }

            minutes = date.getMinutes();
            if (minutes < 10)
                minutes = '0' + minutes;

            return hour + ':' + minutes + ampm;
        }
    };
}();

