﻿(function ($) {
    var cache = [];
    // Arguments are image paths relative to the current page.
    $.preLoadImages = function () {
        var args_len = arguments.length;
        for (var i = args_len; i--; ) {
            var cacheImage = document.createElement('img');
            cacheImage.src = arguments[i];
            cache.push(cacheImage);
        }
    }
})(jQuery);

(function( $ ){
  $.fn.myFade = function(duration, opacity, callback) {
  
    this.fadeTo(duration, opacity, callback);
    return this;

  };
})( jQuery );

function set_left(ele) {
    var left = (265 - parseInt(ele.css("width"))) / 2;
    ele.css({ "left": left });
}

function finish_load() {
    $(document).stopTime("init");
    current_display = $("#medium" + $("img", selected_li).attr("data-n"));
    set_left(current_display);
    current_display.fadeTo(1000, 1, function () {
        selected_li.fadeTo(1000, 1);
    });
}

function boutique_slideshow(short, long) {
    var div = $("#BoutiqueSlideshow");

    $(".a", div).fadeTo(long, 1, function () {
        $(".a", div).fadeOut(short, function () {
            $(".b", div).animate({ left: -255 }, long, function () {
                $(".b", div).fadeOut(short, function () {
                    $(".c", div).animate({ left: 0 }, long, function () {
                        $(".c", div).fadeOut(short, function () {
                            $(".d", div).animate({ left: -255 }, long, function () {
                                $(".d", div).fadeOut(short, function () {
                                    $(".e", div).animate({ left: 0 }, long, function () {
                                        $(".a", div).fadeIn(short, function () {
                                            $("img", div).fadeTo(1, 1);
                                            $(".b, .d", div).css("left", 0);
                                            $(".c, .e", div).css("right", 0);
                                        });
                                    });
                                });
                            });
                        });
                    });
                });
            });
        });
    });
}


function start_load() {
    if ($("#BoutiqueSlideshow").size()) {
        var short = 700;
        var long = 6000;
        boutique_slideshow(short, long);
        $(document).everyTime(5 * (short + long), function () { boutique_slideshow(short, long) });
    }
    if ($("#display img").size()) {
        var parsedHash = parseInt(location.hash.replace("#", ""), 10);
        if (parsedHash) {
            selected_li = $("#thumbnails img[data-n='" + parsedHash + "']").parent();
        } else {
            selected_li = $("#thumbnails li").first();
        }

        var current_li = $("#thumbnails li").first();
        $(document).everyTime(60, "init", function () {
            current_li.fadeTo(1000, 0.5);
            current_li = current_li.next("li");
            if (!current_li.size()) finish_load();
        })

        $("#display img").each(function () {
            $.preLoadImages($(this).attr("src"));
        });

        $("#nav ul .active ul").animate({opacity: 1 }, 1000);
    }

    if ($(".PressThumbnails").size()) {
        var current_li = $(".PressThumbnails.Show li").first();
        $(document).everyTime(60, "init", function () {
            current_li.fadeTo(1000, 1);
            current_li = current_li.next("li");
            if (!current_li.size()) $(document).stopTime("init");
        });
    }

    if ($(".CorporateColumn").size()) {

        $(document).everyTime(3000, function () {
            var fadeTime = 1000;

            var currentA = $("#ColumnA .Show");
            var currentB = $("#ColumnB .Show");
            var nextA;
            var nextB;

            if (currentA.next("li").size()) nextA = currentA.next("li");
            else nextA = $("#ColumnA li").first();

            if (currentB.next("li").size()) nextB = currentB.next("li");
            else nextB = $("#ColumnB li").first();

            currentA.fadeOut(fadeTime, function () { $(this).removeClass("Show") }); ;
            nextA.fadeIn(fadeTime, function () {
                $(this).addClass("Show");
                currentB.fadeOut(fadeTime, function () { $(this).removeClass("Show") });
                nextB.fadeIn(fadeTime).addClass("Show");
            });
        });
    }

   if ($(".SlideShow").size()) {

       $(document).everyTime(3000, function () {
           var fadeTime = 1000;

           $(".SlideShow").each(function () {
               var next;
               var current = $(this).find(".Show");
               if (current.next("li").size()) next = current.next("li");
               else next = $(this).find('li').first();

               current.fadeOut(fadeTime, function () { $(this).removeClass("Show"); });
               next.fadeIn(fadeTime, function () { $(this).addClass("Show") });
           });


       });    

    }
}

$(window).load(start_load);

$(function () {

    if ($("#display img").size())
        $.preLoadImages($("#display img")[0].src);
    $("#footer a").fadeTo(0, 0.5);
    $("#footer a").hover(function () { $(this).fadeTo(0, 1); }, function () { $(this).fadeTo(0, 0.5); });

    $("#thumbnails li").hover(function () {
        $(this).myFade("fast", 1);
        $(this).myFade("fast", 1);
    }, function () {
        if ($(this)[0] != selected_li[0]) $(this).myFade(1, 0.5);
    });

    $("#thumbnails li").click(function () {
        if (this == selected_li[0]) return;

        selected_li.myFade(400, 0.5);
        selected_li = $(this).myFade(1000, 1);

        var scrim = $(document.createElement("div")).addClass('scrim');
        scrim.css("width", $("img", this).css("width"));
        scrim.css("height", $("img", this).css("height"));

        selected_li.prepend(scrim);
        scrim.myFade(1, 0.75);
        scrim.myFade(1000, 0, function () { $(this).remove(); });

        var new_image_number = $("img", this).attr("data-n");
        location.hash = new_image_number;

        var new_display = $("#medium" + new_image_number);
        $("#display img").css("zIndex", 0);

        var left = (265 - parseInt(new_display.css("width"))) / 2;
        set_left(new_display);
        new_display.css({ "zIndex": 100 }).myFade('300', 1, function () {
            current_display.hide();
            current_display = new_display;
        });

        if (new_image_number.length < 2) new_image_number = "0" + new_image_number;
        $("#current_image_number").text(new_image_number);
    });

    $("#next").click(function (e) {
        e.preventDefault();
        var next = selected_li.next("li");
        if (next.size())
            selected_li.next("li").fadeTo(100, 1).click();
        else {
            var next_page = $("#pages .active").next("li");
            if (next_page.size()) document.location = next_page.find("a")[0].href;
        }
    });

    $("#prev").click(function (e) {
        e.preventDefault();
        var prev = selected_li.prev("li");
        if (prev.size())
            selected_li.prev("li").fadeTo(100, 1).click();
        else {
            var prev_page = $("#pages .active").prev("li");
            if (prev_page.size()) document.location = prev_page.find("a")[0].href + "#14";
        }
    });

    $(".section > a").click(function (e) {
        e.preventDefault();
        $(".section.active").removeClass("active");
        var newSection = $(this).closest(".section");
        if (newSection.find("li").size())
            newSection.addClass("active");
        else document.location = this.href;
    });

    $(".PressThumbnails img").hover(function () {
        $(this).myFade(300, 0.7);
    }, function () {
        $(this).myFade(300, 1);
    }).not(".VideoThumbnail").click(function () {
        window.open($(this).data("full"));
    })


    function refreshEventsArrows() {
        if ($(".activeSection .Show").next("table").size())
            $("#events-navigation .nextPage").show();
        else $("#events-navigation .nextPage").hide();

        if ($(".activeSection .Show").prev("table").size())
            $("#events-navigation .prevPage").show();
        else $("#events-navigation .prevPage").hide();
    }


    $("#events-navigation .prevPage").click(function () {
        $(".activeSection .Show").removeClass("Show").prev("table").addClass("Show");
        refreshEventsArrows();
    });

    $("#events-navigation .nextPage").click(function () {
        $(".activeSection .Show").removeClass("Show").next("table").addClass("Show");
        refreshEventsArrows();
    });

    $("#events-navigation .rtw").click(function () {
        $(this).hide();
        $("#BridalWrapper").hide().removeClass("activeSection");
        $("#events-navigation .bridal").show();
        $("#RTWWrapper").show().addClass("activeSection");
        $("#BridalImage").fadeOut(function () {
            $("#RTWImage").fadeIn();
        });
        refreshEventsArrows();
    });


    $("#events-navigation .bridal").click(function () {
        $(this).hide();
        $("#BridalWrapper").show().addClass("activeSection");
        $("#events-navigation .rtw").show();
        $("#RTWWrapper").hide().removeClass("activeSection");
        $("#RTWImage").fadeOut(function () {
            $("#BridalImage").fadeIn();
        });
        refreshEventsArrows();
    });

    $("#events-navigation .bridal").click();

    if ($(".Pagination").size()) {
        refresh_arrows();
        $(".Pagination .Right").click(function () {
            $(".Show").fadeOut(function () {
                $(this).removeClass("Show");
                $(this).next("ul").fadeIn(function () {
                    $(this).addClass("Show");
                    refresh_arrows();
                });
            });
        });
        $(".Pagination .Left").click(function () {
            $(".Show").fadeOut(function () {
                $(this).removeClass("Show");
                $(this).prev("ul").fadeIn(function () {
                    $(this).addClass("Show");
                    refresh_arrows();
                });
            });
        });
    }

    $(".VideoLink").click(function (e) {

        e.preventDefault();
        $.fancybox({
            'padding': 0,
            'autoScale': false,
            'transitionIn': 'fade',
            'transitionOut': 'fade',
            'title': $(this).data("desc"),
            'width': 680,
            'height': 495,
            'href': this.href.replace(new RegExp("watch\\?v=", "i"), 'v/'),
            'type': 'swf',
            'swf': {
                'wmode': 'transparent',
                'allowfullscreen': 'true'
            }
        });

    });

})

function refresh_arrows() {
    if ($(".Show").next(".PressThumbnails").size()) {
        $(".Pagination .Right").show();
    } else {
        $(".Pagination .Right").hide();
    }

    if ($(".Show").prev(".PressThumbnails").size()) {
        $(".Pagination .Left").show();
    } else {
        $(".Pagination .Left").hide();
    }
}


/* CUSTOM SCROLLBAR */
$(function () {
    $("#scrollable").customScroller({innerHeight: 700});
});


/* STORE DIRECTORY PAGINATION
   -------------------------- */
var StoresCategory = "bridal";
var StoresPage = 1;

$(function () {
    if ($("ul#stores-navigation").length) {
        $("ul#stores-navigation li.prevPage").click(function () { navigateTo(StoresCategory, StoresPage - 1); });
        $("ul#stores-navigation li.nextPage").click(function () { navigateTo(StoresCategory, StoresPage + 1); });
        $("ul#stores-navigation li.bridal").click(function () { navigateTo("bridal", 1); });
        $("ul#stores-navigation li.rtw").click(function () { navigateTo("rtw", 1); });
        $("ul#stores-navigation li.accessories").click(function () { navigateTo("accessories", 1); });
        $("ul#stores-navigation li.royal").click(function () { navigateTo("royal", 1); });
        navigateTo(StoresCategory, StoresPage);
    }
});

function navigateTo(category, page) {
    StoresCategory = category;
    StoresPage = page;

    // identify active category divs
    var activeGroup = $("#"+category+"_tables");
    var activeHero = $("#"+category+"_hero");
    var activeNav = $("ul#stores-navigation li."+category);
    
    // identify active page
    var pages = $(activeGroup).children();
    page = (page < 1) ? 1 : page;
    page = (page > pages.length) ? pages.length : page;

    var activePage = pages[page-1];

    // blank out everything
    $("img.hero, div.store_group, table.stores, ul#stores-navigation li").hide();
    $("ul#stores-navigation li").not(".prevPage, .nextPage").show();

    // show the appropriate things
    $(activeGroup).show();
    $(activeHero).show();
    $(activePage).show();
    $(activeNav).hide();

    if(pages.length > 1) {
        if(page > 1) $("ul#stores-navigation li.prevPage").show();
        if(page < pages.length) $("ul#stores-navigation li.nextPage").show();
    }
}
