﻿// plugins
(function($) {

    // logging
    $.fn.log = function(value){
        if (window.console && window.console.log) {
            console.log("DEBUG -> ", value);
        }  
    }
    
    // Used by navigation control "binded" code
    function handleDropDownNavigation(dropDown, eventDescription)
    {
        $.fn.log(dropDown);
    
        var url = dropDown.options[dropDown.selectedIndex].value;
        
        dotNetEventTrackingControl_TrackEvent("PhotoGallery:" + eventDescription + ", " + url);
        
        window.location = url;
    }
    
    // Media Gallery plugin
    $.fn.MediaGallery = function(options){
    
        if (album.slides != null && album.slides.length > 0) {
        
            var i;
            
            if (typeof album.CurrentSlideIndex != "undefined")
                i = album.CurrentSlideIndex;
            else
                i = 0;

        
            var slide = album.slides[i];
            
            $.fn.log(slide);
            
            if (slide != null){
            
                $.MediaGallery.currentItem = slide;
                $.MediaGallery.currentItemIndex = i;
                
                $.MediaGallery.updateNavigation(slide);
            }
        }
        
        // Handles navigation controls
        $("#AlbumListDropDown_AlbumListDropDown").bind
        (
            "change", function(e){handleDropDownNavigation(e.target, "Album dropdown changed");}
        );
        $("#KeywordCategoryDropDownList_KeywordCategory1DropDownList").bind
        (
            "change", function(e){handleDropDownNavigation(e.target, "Category 1 dropdown changed");}
        );
        $("#KeywordCategoryDropDownList_KeywordCategory2DropDownList").bind
        (
            "change", function(e){handleDropDownNavigation(e.target, "Category 2 dropdown changed");}
        );

        $(".next, .media-frame", this).click( function(e)
        {
            var nextSlide = $.MediaGallery.nextItem();

            if (nextSlide != null)
            {
                var position = nextSlide.SlidePosition;
                position = position.substring(0, position.indexOf(" of"));
                dotNetEventTrackingControl_TrackEvent("PhotoGallery:Next slide clicked,"
                    + position + "," + nextSlide.ImagePath);
            }
            
            e.preventDefault();
            return false;
        });

        $(".back", this).click(function(e)
        {
            var previousSlide = $.MediaGallery.previousItem();

            if (previousSlide != null)
            {
                var position = previousSlide.SlidePosition;
                position = position.substring(0, position.indexOf(" of"));
                dotNetEventTrackingControl_TrackEvent("PhotoGallery:Previous slide clicked,"
                    + position + "," + previousSlide.ImagePath);
            }
        
            e.preventDefault();
            return false;
        });
               
        $.MediaGallery.centerImage(".media-frame img", ".media-frame");
    };
    
    // public 
    $.MediaGallery = {
    
        counter: 1,
        currentItem: "",
        currentItemIndex: 0,
        
        centerImage: function(image, parentItem){            
        
        
            var imgWidth = null;
            
            if ($(image).length > 0) {
            
                if ($(image).width() != 0) {
                    imgWidth = $(image).width();
                }else if ($(image).attr("width") > 0) {
                    imgWidth = $(image).attr("width");
                }
                
                if (imgWidth != null) {
                    var diffWidth  = imgWidth - $(parentItem).width();
                
                    if (diffWidth > 0) {
                       var marginLeft = (diffWidth / 2) * -1;
                        $(image).css("margin-left", marginLeft);
                    }
                }                
            }
        },
        
        nextItem: function()
        {
            var item = null;

            $.fn.log("nextItem: this.currentItemIndex = " + this.currentItemIndex);
            var index = this.currentItemIndex;
            if (index != -1)
            {
                index++;
                if (index >= album.slides.length)
                {
                    index = 0;
                }
                item = album.slides[index];
                $.fn.log("nextItem: item = " + item);
                
                this.showItem(item);
            }
            
            return item;
        },
        
        previousItem: function()
        {
            var item = null;

            $.fn.log("previousItem: this.currentItemIndex = " + this.currentItemIndex);
            var index = this.currentItemIndex;
            if (index != -1)
            {
                index--;
                if (index < 0)
                {
                    index = album.slides.length - 1;
                }
                item = album.slides[index];
                $.fn.log("previousItem: item = " + item);
                
                this.showItem(item);
            }
            
            return item;
        },
        
        updateNavigation: function(item){
            
            var index = this.currentItemIndex;
            var nextItem, prevItem;
            
            $.fn.log(index);
            
            if (index > 0) {
                prevItem = album.slides[index - 1];
            }else{
                prevItem = album.slides[album.slides.length - 1];
            }
            
            if (index < (album.slides.length - 1)) {
                nextItem = album.slides[index + 1];
            }else{
                nextItem = album.slides[0];
            }
            
            $.fn.log(nextItem.SlideCaption);
            
            $(".back").data("item", prevItem);
            $(".back").attr("href", prevItem.SlidePath);
            
            $(".next").data("item", nextItem);
            $(".next").attr("href", nextItem.SlidePath);
            $(".media-frame").data("item", nextItem);
        },
        
        updateSocialMedia: function(){
           if (typeof updateClickability == 'function') {
               updateClickability(this.currentItem.SlidePath, this.currentItem.SlideTitle);
           }
            
           var mixx="http://mixx.com/submit/story?page_url=+storyURL+";
           mixx = mixx.replace("+storyURL+", this.currentItem.SlidePath);
           mixx = mixx.replace("+storyTitle+", this.currentItem.SlideTitle);
           
           var twitter="http://twitter.com/home?status=Currently%20viewing%20+storyTitle+%20+storyURL+";
           twitter = twitter.replace("+storyURL+", this.currentItem.SlidePath);
           twitter = twitter.replace("+storyTitle+", this.currentItem.SlideTitle);
           
           var digg="http://digg.com/submit?phase=2&amp;url=+storyURL+&amp;title=+storyTitle+&amp;topic=";
           digg = digg.replace("+storyURL+", this.currentItem.SlidePath);
           digg = digg.replace("+storyTitle+", this.currentItem.SlideTitle);
           
           var facebook="http://www.facebook.com/share.php?u=+storyURL+&t=+storyTitle+";
           facebook = facebook.replace("+storyURL+", this.currentItem.SlidePath);
           facebook = facebook.replace("+storyTitle+", this.currentItem.SlideTitle);
           
           var reddit="http://reddit.com/submit?url=+storyURL+&amp;title=+storyTitle+";
           reddit = reddit.replace("+storyURL+", this.currentItem.SlidePath);
           reddit = reddit.replace("+storyTitle+", this.currentItem.SlideTitle);
           
          $(".social-wrapper .twitter").attr("href", twitter);
          $(".social-wrapper .mixx").attr("href", mixx);
          $(".social-wrapper .digg").attr("href", digg);
          $(".social-wrapper .facebook").attr("href", facebook);
          $(".social-wrapper .reddit").attr("href", reddit);
        },
         updateAudioControl: function(){
            
            if (this.currentItem.AudioUrl != "" && typeof displayPlayer != "undefined") {
                displayPlayer(this.currentItem.AudioUrl);
            }
            else if (typeof hidePlayer != "undefined")
            {
                hidePlayer();
            }
        },
        
        updateAdvertising: function() {
            if (typeof writeVideoAd != "undefined") {
                writeVideoAd("");
            }
        },
        
        setItemData: function(item){
            if (item.DatePhotoTaken != null && item.DatePhotoTaken != "") {
                $("#SlideDateTaken").html("Taken: " + item.DatePhotoTaken);            
            }
            else {
                $("#SlideDateTaken").html("");
            }            
        
            $("#SlideTitle").html(item.SlideTitle);
            $("#caption").html(item.SlideCaption);
            $("#SlideCredit").html(item.SlideCredit);
            $("#SlidePosition").html(item.SlidePosition);
            $("#spikes").html("");            
            $.each(item.Links, function(i, link){
                $("#spikes").append(
                                    "<li>" + link +  "</li>"
                                    );
            });
            
            $('.scrollable').jScrollPane({showArrows:true});
            
        },
        showItem: function(item){
            
            if (item != null && this.currentItem != item) {
            
                this.currentItem = item;
                this.currentItemIndex = $(album.slides).index(item);
                
                var image = new Image();
                
                $(image)
                .load(function(){
                    $(".media-frame").html("").append(image);
                    $.MediaGallery.setItemData(item);
                })
                .error(function(){
                     $.fn.log("Error!!!");
                     $(".media-frame").fadeOut(100);
                     $.MediaGallery.setItemData(item);
                })
                .attr('alt', item.AlternateText)
                .attr('src', item.ImagePath);
                
                this.updateNavigation(item);

                this.updateSocialMedia();
                this.updateAudioControl();
                
                if (typeof adRefreshRate != "undefined") {
                    if (this.counter % adRefreshRate == 0) {
                        this.updateAdvertising();
                    }
                    
                    this.counter++;
                } 
                
                $.fn.log(item.SlideCaption);
                               
            }
        }
    };

    // on ready
    $(document).ready(function() {
        
        if (typeof album != "undefined")
        {
            $.fn.log(album); 

            $("#gallery").MediaGallery();
        }

    });

})(jQuery);