(function ($) {

    /* experience bar local vars */

    var exbarTrigger;
    var exbarTargetCamId;

    $.vail_mntwebcams = {

        id: 'webCams',
        view: $('#webCams'),
        noCam: $('#webcamNone'),

        //Here ID relates to id in FEED
        resortData: { keystone: { id: resortID, name: resortName} },
        //Resorts that have at least one cam in feed are listed here
        activeResorts: [],
        //jQuery XML Obj
        webcamFeed: {},
        feedStatus: { loaded: false, error: false },

        // experience bar selected
        exTrigger: false,
        exTargetCamId: null,
        exCallSource: null, // exCallSource = "expbar" or "galleryThumb";

        /*
        * check for experience bar selection
        */

        getExperienceBarSelection: function () {

            var querystring;
            var getTargetCamId;

            /* get querystring values to set these vars and then clear the qstring */
            $.vail_mntwebcams.exCallSource = "expbar";
            $.vail_mntwebcams.exTrigger = true;
            exbarTrigger = true;

            querystring = location.search.replace('?', '').split('&');
            getTargetCamId = querystring[1].split('=');
            $.vail_mntwebcams.exTargetCamId = getTargetCamId[1];
            exbarTargetCamId = $.vail_mntwebcams.exTargetCamId;
        },


        /*
        * Display the large photo and supporting data for a give cam, like maps and description, time stamp
        */
        displayCam: function (camElem) {

            if (camElem.find('img')[0] == undefined) {
                $(" #errormsg").html("Webcams not available currently. Kindly check back later.").show();
                $(" .introText").hide();
                $('.webCam_imgHolder').hide();
                $('.map_imgHolder').hide();
                $('.photoDecription').hide();
                $('.webCamContentHolder').hide();
                return;
            } else {
                var cam = camElem.data('cam');
                // Get WebCam id for displaying embed code in large photo area.
                /*if (camElem.data('cam') == undefined)
                {
                return false;
                }*/

                if ($.vail_mntwebcams.exTrigger && $.vail_mntwebcams.exCallSource == "expbar") {
                    camId = $.vail_mntwebcams.exTargetCamId;
                }
                else {
                    camId = camElem.attr('camid'); // camElem.data('cam').attr('id'); 
                }

                var len = $(camId).length;

                // if hidden field with WebCam id exist.
                if ($('#' + camId).length > 0) {
                    $('.webCam_imgHolder img').hide();
                    $('.webCam_imgHolder embed').show();
                    var embeddedCode = $('#' + camId).val();
                    if ($('.webCam_imgHolder embed').length > 0) {
                        $('.webCam_imgHolder embed').replaceWith(embeddedCode).show();
                    }
                    else {
                        $('.webCam_imgHolder').append(embeddedCode).show();
                    }
                }
                else {
                    $('.webCam_imgHolder embed').hide();
                    $('.webCam_imgHolder img').show();
                    if ($('.webCam_imgHolder img').length > 0) {
                        //$('.webCam_imgHolder img').attr('src',cam.attr('imageurl')).show();
                    }
                    //$('.webCam_imgHolder img').attr('src',cam.attr('imageurl')); 
                    if (camElem.find('img')[0] != undefined) {
                        $('.webCam_imgHolder img').attr('src', camElem.find('img')[0].src);
                    }
                }

                $('.map_imgHolder img').attr('src', $.vail_mntwebcams.getLocationImageUrl(camId));

                if (camElem.attr('name') != undefined) {
                    $('.photoDecription h3').text(camElem.attr('name'));
                }
                if ((camElem.attr('description') != undefined) && (camElem.attr('overnightimagetime') != undefined)) {
                    $('.photoDecription p').html(camElem.attr('description') + '<br />' + $.vail_mntwebcams.ISOtoDate(camElem.attr('overnightimagetime')));
                }
            }
        },

        //Set the Date time stamp in our own defined format 1900-1-1T13:00:00
        //new Date ( year, month, date, hour, minute, second )
        ISOtoDate: function (ISO) {
            ISO = ISO.split('T');
            ISO = ISO[0].split('-').concat(ISO[1] ? ISO[1].split(':') : [0, 0, 0]);
            return Date(ISO[0], ISO[1], ISO[2], ISO[3], ISO[4], ISO[5]);
        },
        /*
        * calculate the full URL for the location image based on whats in the
        * page currently
        */
        getLocationImageUrl: function (camId) {
            var url = $('.map_imgHolder img').get(0).src;
            return "/VailResorts/sites/" + resortName + "/assets/img/mountain_conditions/cams/locations/" + "cam_" + camId + ".png";
        }
    }

    $(function () {

        setTimeout(function () {
            $(".webCam_thumbs li").hover(function () {
                $(this).addClass('imgOver');
            },
				function () {
				    $(this).removeClass('imgOver');
				});
        }, 500);

        $(".webCam_thumbs li img").bind("click", function () {
            $(this).parents('div').find('li').removeClass('selected');
            $(this).parent('li').addClass('selected');
            $.vail_mntwebcams.exCallSource = "galleryThumb";
            $.vail_mntwebcams.displayCam($(this).closest('li'));
            return false;

        });

        $(".webCam_thumbs li span a").bind("click", function () {
            $(this).parents('div').find('li').removeClass('selected');
            $(this).parent('span').parent('li').addClass('selected');
            $.vail_mntwebcams.exCallSource = "galleryThumb";
            $.vail_mntwebcams.displayCam($(this).closest('li'));
            return false;

        });
    });



    /*
    * These variables are set to the params the arrays that caontains the name/value 
    * pair for the query string defaultCamId,resortName and resortID
    */

    var defaultCamId = document.getElementById('hdnCameraId').value;
    var resortName = document.getElementById('hdnResortName').value;
    var resortID = document.getElementById('hdnResortId').value;

    var defaultLi = $(".webCam_thumbs li.selected");

    if (document.URL != document.referrer && document.URL.indexOf("?expbar") >= 0) {
        $.vail_mntwebcams.getExperienceBarSelection();
    } else {
        $.vail_mntwebcams.exCallSource = "galleryThumb";
    }

    if ($.vail_mntwebcams.exTrigger) {
        $('.webCam_thumbs li.selected').removeClass('selected');
        defaultLi = $('.webCam_thumbs li[camid=' + $.vail_mntwebcams.exTargetCamId + ']');
        //defaultLi = $('.webCam_thumbs li').attr('camid', $.vail_mntwebcams.exTargetCamId);
        if (defaultLi.length > 0) {
            defaultLi.addClass('selected');
        };
    }
    else {
        defaultLi = $(".webCam_thumbs li.selected");
        $.vail_mntwebcams.exCallSource = "galleryThumb";
        if (!defaultLi.length) {
            defaultLi = $(".webCam_thumbs li:first").addClass('selected');
        };
    }

    $.vail_mntwebcams.displayCam(defaultLi);

    if (defaultCamId == "") { defaultCamId = "-1"; };

})(jQuery);




