﻿//Define Kpw object only if it's not already defined
if ("undefined" == typeof Kpw) {Kpw = {};}

//Initialize ImageClick
$().ready(function(){
	Kpw.Gallery.bindEvents();
	if (typeof Sys != "undefined" && Sys.WebForms && Sys.WebForms.PageRequestManager && Sys.WebForms.PageRequestManager.getInstance() != null) {
        Sys.WebForms.PageRequestManager.getInstance().add_endRequest(Kpw.Gallery.bindEvents);
    }
});

//Kpw Gallery Object
Kpw.Gallery = {
    bindEvents: function() {
        //Save/Print/e-mail
        $("#toolsSave").bind("click", function(e) {

            //center loading div
            var buttonTop = $("#toolsSave").offset().top;
            var buttonLeft = $("#toolsSave").offset().left;

            $("#savePopup").css({
                "position": "absolute",
                "top": buttonTop - 11,
                "left": buttonLeft - 90
            });

            $("#savePopup").show("blind", null, 500);

        });

        $("#savePopup").hover(
              function() {
              },
              function() {
                  $("#savePopup").hide("blind", 500);
              }
        );

        $("#savePopup").hide();

        $('span.photoWrapper img.galleryThumb').css({ filter: 'alpha(opacity=100)', opacity: '1' });

        $('span.photoWrapper img.galleryThumb').mouseover(
            function() {
                $(this).css({ filter: 'alpha(opacity=55)', opacity: '0.55' });
            }
         );

        $('span.photoWrapper img.galleryThumb').mouseout(
            function() {
                $(this).css({ filter: 'alpha(opacity=100)', opacity: '1' });
            }
         );

        //bind thumb/main swap
        $('img.galleryThumb').click(
            function() {
                var imageSource = $(this).attr('src');
                if (imageSource.indexOf('NoImageAvailableLogo.png') == -1) {
                    //Get the path for the fullsize image
                    var filepath = $(this).parent().find("input.fullsizePath").attr("value");
                    //Get the content id for media popup
                    var contentId = $(this).parent().find("input.photoId").attr("value");
                    //Get the file extension of the gallery item
                    var extension = Kpw.Gallery.getFileExtension(filepath);
                    //Get the media type based on the file extension
                    var mediaPlayer = Kpw.Gallery.getPlayerFromExtension(extension);
                    //Get the type of content
                    var contentType = $(this).parent().find("input.contentType").attr("value");

                    //Swap based on type
                    if (mediaPlayer == 'image') {
                        Kpw.Gallery.imageSwap(filepath, contentId);
                    }
                    else {
                        Kpw.Gallery.mediaSwap(mediaPlayer, filepath, contentId);
                    }

                    if (contentType == "Dress") {
                        var url = "/galleries/dresstaginfo.aspx div.dressTags"
                        $("div.dressGallery div.galleryNavigation div.photoTags").load(url, { 'id': contentId });
                    }

                    if ("undefined" != typeof Kpw.Analytics) {
                        Kpw.Analytics.logContent(contentId);
                    }
                }
            }
        );

        $('div.galleryNavigation div.photo li.previous').click(
            function() {
                ctl00_phContent_GalleryThumbnail1_hdnThumbFullPath;
            }
        );

        $('div.galleryNavigation div.photo li.next').click(
            function() {
                ctl00_phContent_GalleryThumbnail1_hdnThumbFullPath;
            }
        );
    },
    imageSwap: function(largeSource, contentId) {
        var image = new Image();
        var imgHtml = "<img class='imgGalleryPrimary' id='imgGalleryPrimary' src='" + largeSource + "' alt='Main Photo' runat='server'/>";
        var html = $('div.kpw div.gallery div.primaryPhoto span.photoWrapper span.media').html();

        //If the media html is already an image, just change the src - else, change the markup altogether...
        if (html.indexOf('<img') != -1 || ($.browser.msie && html.indexOf('<IMG') != -1)) {
            //don't switch the image til its been loaded.
            $(image).load(function() {

                if ($.browser.safari) {
                    $('div.kpw div.gallery div.primaryPhoto span.photoWrapper span.media img').attr('src', largeSource);
                    $('div.kpw div.gallery div.primaryPhoto span.photoWrapper span.media').parent().find("input.photoId").attr("value", contentId)
                }
                else {
                    //fade out - fade in after load.
                    $("div.kpw div.gallery div.primaryPhoto span.photoWrapper span.media").fadeOut("slow", function() {
                        $('div.kpw div.gallery div.primaryPhoto span.photoWrapper span.media img').attr('src', largeSource);
                        $('div.kpw div.gallery div.primaryPhoto span.photoWrapper span.media').parent().find("input.photoId").attr("value", contentId)
                        $("div.kpw div.gallery div.primaryPhoto span.photoWrapper span.media").fadeIn("slow");
                    });
                }
            });

            image.src = largeSource;

        }
        else {

            //fade in after image is loaded load.
            $(image).load(function() {
                $('div.kpw div.gallery div.primaryPhoto span.photoWrapper span.media').fadeIn("slow");
                $('div.kpw div.gallery div.primaryPhoto span.photoWrapper span.media').html(imgHtml);
                $('div.kpw div.gallery div.primaryPhoto span.photoWrapper span.media').parent().find("input.photoId").attr("value", contentId);
            });

            image.src = largeSource;

        }
    },
    mediaSwap: function(mediaPlayer, filePath, contentId) {
        var markup = Kpw.Gallery.getMarkUp(mediaPlayer, filePath);
        $('div.kpw div.gallery div.primaryPhoto span.photoWrapper span.media').html(markup);
        $('div.kpw div.gallery div.primaryPhoto span.photoWrapper span.media').parent().find("input.photoId").attr("value", contentId);
    },
    getFileExtension: function(file) {
        var dot = file.lastIndexOf(".");
        var extension = "";
        if (dot != -1) {
            extension = file.substr(dot + 1, file.length).toLowerCase();
        }
        return extension;
    },
    getPlayerFromExtension: function(extension) {
        var winmedia = 'asx,asf,avi,wma,wmv';
        var flash = 'flv,swf,mov';
        var quicktimeAudio = 'mp3,wav,mid,midi,m4v';
        var quicktimeVideo = 'mpg,mpeg,m4a';
        var quicktimeMP4 = 'mp4';
        var real = 'ra,ram,rm,rpm,rv,smi,smil';
        var image = 'jpg,bmp,gif,png,jpeg';

        if (winmedia.indexOf(extension) != -1) {
            return 'winmedia';
        }
        else if (image.indexOf(extension) != -1) {
            return 'image';
        }
        else if (flash.indexOf(extension) != -1) {
            return 'flash';
        }
        else if (quicktimeAudio.indexOf(extension) != -1) {
            return 'quicktimeAudio';
        }
        else if (quicktimeVideo.indexOf(extension) != -1) {
            return 'quicktimeVideo';
        }
        else if (quicktimeMP4.indexOf(extension) != -1) {
            return 'quicktimeMP4';
        }
        else if (real.indexOf(extension) != -1) {
            return 'real';
        }
        else {
            return 'none';
        }
    },
    getMarkUp: function(playerType, filePath) {
        var markup = "";

        switch (playerType) {
            case 'winmedia':
                markup = "<object type='video/x-ms-wmv' data=\"" + filePath + "\" width='713' height='528'>" +
                               "<param name='src' value=\"" + filePath + "\" />" +
                               "<param name='autostart' value='true' />" +
                               "<param name='pluginurl' value='http://port25.technet.com/pages/windows-media-player-firefox-plugin-download.aspx' />" +
                           "</object>"
                break;
            case 'flash':
                markup = "<object width='713' height='528' " +
                            "data='http://releases.flowplayer.org/swf/flowplayer-3.1.5.swf' type='application/x-shockwave-flash'>" +
                            "<param name='movie' value='http://releases.flowplayer.org/swf/flowplayer-3.1.5.swf' />" +
                            "<param name='allowfullscreen' value='true' />" +
                            "<param name='allowscriptaccess' value='always' />" +
                            "<param name='flashvars' value='config={\"plugins\":{\"pseudo\":{\"url\":\"flowplayer.pseudostreaming-3.1.3.swf\"},\"controls\":{\"backgroundColor\":\"#46301b\",\"sliderColor\":\"#46301b\",\"sliderGradient\":\"none\", \"volumeSliderColor\":\"#362008\",\"buttonColor\":\"#362008\",\"durationColor\":\"#362008\",\"progressColor\":\"#999999\",\"timeColor\":\"#ffffff\",\"timeBgColor\":\"#999999\",\"bufferColor\":\"#46301b\",\"backgroundGradient\":\"low\"}}, \"play\":{\"opacity\":\"0\"} ,\"canvas\": {\"backgroundColor\": \"#000000\",\"backgroundGradient\":\"none\"},\"clip\":{\"provider\":\"pseudo\",\"scaling\":\"fit\",\"url\":\"" + filePath + "\"},\"playlist\":[{\"provider\":\"pseudo\",\"url\":\"" + filePath + "\"}]}' />" +
                         "</object>"
                break;
            case 'quicktimeAudio':
                markup = "<!--[if !IE]><!-->" +
                            "<object type='audio/x-mpeg' data=\"" + filePath + "\" width='713' height='528'>" +
                            "<param name='src' value=\"" + filePath + "\" />" +
                            "<param name='controller' value='true' />" +
                            "<param name='autoplay' value='false' />" +
                            "<param name='autostart' value='0' />" +
                            "<param name='ShowControls' value='1' />" +
                            "<param name='pluginurl' value='http://www.apple.com/quicktime/download/' />" +
                            "</object>" +
                        "<!--<![endif]-->" +
                        "<!--[if gte IE 7]>" +
                            "<object  classid='clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B' codebase='http://www.apple.com/qtactivex/qtplugin.cab' width='713' height='528'>" +
                            "<param name='src' value=\"" + filePath + "\" />" +
                            "<param name='controller' value='true' />" +
                            "<param name='autoplay' value='false' />" +
                            "<param name='autostart' value='0' />" +
                            "<param name='ShowControls' value='1' />" +
                            "<param name='pluginurl' value='http://www.apple.com/quicktime/download/' />" +
                            "</object>" +
                        "<![endif]-->" +
                        "<!--[if lt IE 7]>" +
                            "<object  classid='clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B' codebase='http://www.apple.com/qtactivex/qtplugin.cab' width='713' height='528'>" +
                            "<param name='src' value=\"" + filePath + "\" />" +
                            "<param name='controller' value='true' />" +
                            "<param name='autoplay' value='false' />" +
                            "<param name='autostart' value='0' />" +
                            "<param name='ShowControls' value='1' />" +
                            "<embed src=\"" + filePath + "\" type='audio/x-mpeg' width='713' height='518' autostart='false' controller='true' ></embed>" +
                            "<param name='pluginurl' value='http://www.apple.com/quicktime/download/' />" +
                            "</object>" +
                        "<![endif]-->"
                break;
            case 'quicktimeVideo':
                markup = "<!--[if !IE]><!-->" +
                            "<object type='video/quicktime' data=\"" + filePath + "\" width='713' height='528'>" +
                            "<param name='controller' value='true' />" +
                            "<param name='autoplay' value='false' />" +
                            "<param name='autostart' value='0' />" +
                            "<param name='pluginurl' value='http://www.apple.com/quicktime/download/' />" +
                            "<param name='showcontrols' value='1' />" +
                            "<param name='scale' value='aspect' />" +
                            "</object>" +
                        "<!--<![endif]-->" +
                        "<!--[if gte IE 7]>" +
                            "<object classid='clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B' width='713' height='528'>" +
                            "<param name='src' value=\"" + filePath + "\" />" +
                            "<param name='controller' value='true' />" +
                            "<param name='autoplay' value='false' />" +
                            "<param name='autostart' value='0' />" +
                            "<param name='ShowControls' value='1' />" +
                            "<param name='pluginurl' value='http://www.apple.com/quicktime/download/' />" +
                            "</object>" +
                        "<![endif]-->" +
                        "<!--[if lt IE 7]>" +
                            "<object classid='clsid:02bf25d5-8c17-4b23-bc80-d3488abddc6b' width='713' height='528' codebase='http://www.apple.com/qtactivex/qtplugin.cab'>" +
                            "<param name='src' value=\"" + filePath + "\">" +
                            "<param name='controller' value='true'>" +
                            "<param name='autoplay' value='false'>" +
                            "<param name='autostart' value='0' /> " +
                            "<param name='ShowControls' value='1' />" +
                            "<embed src=\"" + filePath + "\" width='713' height='528' autoplay='false' controller='true' pluginspage='http://www.apple.com/quicktime/download/'></embed>" +
                            "<param name='pluginurl' value='http://www.apple.com/quicktime/download/' />" +
                            "</object>" +
                        "<![endif]-->"
                break;
            case 'quicktimeMP4':
                markup = "<object classid='clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B' width='713' height='528'" +
                         "codebase='http://www.apple.com/qtactivex/qtplugin.cab'>" +
                         "<param name='src' value=\"" + filePath + "\"/>" +
                         "<param name='autoplay' value='false' />" +
                         "<param name='controller' value='true'/>" +
                         "<embed src=\"" + filePath + "\" width='713' height='528' autoplay='false' controller='true'" +
                         "pluginspage='http://www.apple.com/quicktime/download/'></embed></object>"
                break;
            case 'real':
                markup = "";
                break;

        }

        return markup;
    }
}

