/****************************************
    Website Coded by Gelo Factory LLC
           All Rights Reserved
           (c)  Copyright 2008
         http://gelofactory.com/
****************************************/

$(function() {
    /*
     * Image Preloader
     */

    var pi = new Array();
    var pio = new Array();

    function queuePreloadImg(arg) {
        pi.push(arg);
    }

    function preloadImgs(to) {
        setTimeout(function() {
            pio = new Array();
            $.each(pi, function(pii, pie) {
                pio.push(new Image());
                pio[pii].src = pie;
            });
            pi = new Array();
        }, to);
    }

    queuePreloadImg("./_img/gl-a-thumb_nav_prev-bg.png");
    queuePreloadImg("./_img/gl-a-thumb_nav_next-bg.png");
    queuePreloadImg("./_img/gl-a-hover-bg.gif");
    queuePreloadImg("./_img/loading_zoom.gif");
    queuePreloadImg("./_img/loading_collection.gif");
    queuePreloadImg("./_img/spacer.gif");
    preloadImgs(0);

	/*
	 * Collections
	 */

    if ($('body.collections').length) {
        // constants
        var anim_sp = "normal";
        var anim_sp_slow = "slow";
        var anim_sp_fast = "fast";
        var anim_stagger = 500;
        var anim_lock = false;
        var preload_to = 10000;
        var preload_max = 25;
        var zoom_enable_to = 1000;
        var zoom_buf = 4;
        var slide_width = 88;
        var slide_left_pos = (0 - (2 * slide_width));
        var img_base = "";
        var img_spacer = "_img/spacer.gif";
        var img_loading = "_img/loading_collection.gif";
        var img_zoom_loading = "_img/loading_zoom.gif";
        var slide_thumb_dims = [78, 151];
        var slide_sa_thumb_count = 24;
        var zoom_w = 680;
        var rs_w = 240;
        var ip_w = 320;
        var img_h = 466;
        var img_buf = 4;

        // vars
        var col_data = null;
        var cur_col = null;
        var cur_sect = null;
        var cur_season = null;
        var cur_slide = null;
        var cur_nav_slide = null;
        var slide_count = 0;
        var slide_arr = [];
        var slide_sa_page = -1;
        var slide_sa_page_count = -1;
        var cur_ip_cat = null;
        var ip_cats = [];
        var ip_top = null;

        // Show a loading image until the JSON is fully loaded
        $('#aside .subnav').hide();
        $('#aside img').show();
		$.getJSON('_js/json/'+$('body').attr('id')+'.json', {}, function(data) {
          $('#aside img').hide();
          $('#aside .subnav').show();
		  $('#aside').css('width', '70px');		
		  col_data = data;
        });
		$('#aside .subnav li a').click(function() {
            if (col_data && !anim_lock) {
                anim_lock = true;
                $("#aside .subnav li").removeClass("selected");
                var col = $(this).attr('id').substr(3);
                $($(this).parent().get(0)).addClass("selected");
                var afterHide = function() {
                    anim_lock = false;
                    buildSubnav(col, null, buildCurSect);
                };
                if (cur_sect)
                    hideCurSect(afterHide);
                else if ($('#img').length)
                    $('#img').fadeOut(anim_sp, afterHide);
                else
                    afterHide();
            }
        });

        // Build the subnav for a collection
        var buildSubnav = function(col, season, cb) {
            $.each(col_data['collections'], function(i, e) {
                if (col == e['id']) {
                    $('#seasons_nav').empty();

                    var fs_h2 = "";
                    var se_lis = [];
                    var rs_count = 0;
                    var rv_exists = false;
                    var ip_lis = [];

                    cur_col = col;
                    if (e['seasons'].length) {
                        if (season == null) season = e['seasons'][0]['id'];
                        cur_season = season;
                        ip_cats = [];

                        $.each(e['seasons'], function(si, se) {
                            var subnav_ul = null;
                            if (se['id'] == cur_season) {
                                rs_count = se['runway_slides'].length;
                                rv_exists = (se['runway_video'] != "");
                                if (se['individual_pieces_categories'].length) {
                                    $.each(se['individual_pieces_categories'], function (ipci, ipce) {
                                        if (ipce['individual_pieces'].length) {
                                            ip_cats.push(ipce);
                                        }
                                    });
                                    ip_lis = buildIPSubnavLIs(ip_cats, !rs_count);
                                }

                                var subnav_lis = [];

                                // build subnav
                                if (rs_count) {
                                    cur_sect = 'rs';
                                    var subnav_rs_a = $.create("a", {href: "javascript:void(0);"}, ["Runway Slides"]);
                                    var subnav_rs = $.create("li", {id: "runway_slid"}, [subnav_rs_a]);
                                    subnav_lis.push(subnav_rs);
                                }
                                if (ip_lis.length) {
                                    if (!rs_count) cur_sect = 'ip';
                                    subnav_lis.push(buildIPLI(ip_lis));
                                }
                                if (rv_exists) {
                                    if (!rs_count && !ip_lis.length) cur_sect = 'rv';
                                    var subnav_rv_a = $.create("a", {href: "javascript:void(0);"}, ["Runway Video"]);
                                    var subnav_rv = $.create("li", {id: "runway_vid"}, [subnav_rv_a]);
                                    subnav_lis.push(subnav_rv);
                                }

                                if (rs_count || rv_exists || ip_lis.length) {
                                    subnav_ul = $.create("ul", {}, subnav_lis);
                                }
                            }

                            var se_a = $.create("a", {href: "javascript:void(0);", id: "seas"+se['id']}, [se['title']+" "+se['year']]);
                            $(se_a).addClass("seas");
                            if (se['id'] == season) $(se_a).addClass("selected");
                            var se_ch = [se_a];
                            if (subnav_ul) se_ch.push(subnav_ul);
                            se_lis.push($.create("li", {}, se_ch));
                        });

                        // build seasons
                        $('#seasons_nav').hide();
                        $('#seasons_nav').append(se_lis);

                        // seasons click events
                        if (e['seasons'].length > 1) {
                            $('#seasons_nav .seas').click(function() {
                                if (!anim_lock) {
                                    anim_lock = true;
                                    var seas = $(this).attr('id').substr(4);
                                    hideCurSect(function() {
                                        anim_lock = false;
                                        buildSubnav(col, seas, function() { buildCurSect(); });
                                    });
                                }
                            });
                        }

                        // runway slides click event
                        if (rs_count) {
                            $('#runway_slid a').click(function() {
                                if (!anim_lock) {
                                    anim_lock = true;
                                    hideCurSect(function() {
                                        anim_lock = false;
                                        cur_sect = 'rs';
                                        buildCurSect();
                                    });
                                }
                            });
                        }

                        // runway video click event
                        if (rv_exists) {
                            $('#runway_vid a').click(function() {
                                if (!anim_lock) {
                                    anim_lock = true;
                                    hideCurSect(function() {
                                        anim_lock = false;
                                        cur_sect = 'rv';
                                        buildCurSect();
                                    });
                                }
                            });
                        }

                        // individual pieces mouseover + click events
                        if (ip_lis.length) enableIPSubnav();

                        // show nav + execute callback
                        if ($.browser.msie) {
                            $('#seasons_nav').show();
                            cb();
                        } else {
                            $('#seasons_nav').fadeIn(anim_sp, function() { cb(); });
                        }
                    }
                }
            });
        };

        var buildIPSubnavLIs = function(ip_cats, only_sect) {
            var ip_lis_new = [];
            if (only_sect && ip_cats.length && !cur_ip_cat) cur_ip_cat = ip_cats[0]['id'];
            $.each(ip_cats, function(ipci, ipce) {
                if (ipce['id'] == cur_ip_cat) {
                    var attrs = {};
                    var ip_el = $.create("strong", attrs, [ipce['category']]);
                    ip_lis_new.push($.create("li", {}, [ip_el]));
                } else {
                    var ip_el = $.create("a", {id: "ipc"+ipce['id'], href: "javascript:void(0);"}, [ipce['category']]);
                    ip_lis_new.push($.create("li", {}, [ip_el]));
                }
            });
            return ip_lis_new;
        };

        var buildIPLI = function(ip_lis_new) {
            var ip_ul = $.create("ul", {}, ip_lis_new);
            var ip_sp = $.create("span", {}, [":"]);
            var ip_a = $.create("a", {href: "javascript:void(0);"}, ["Individual Pieces "]);
            $(ip_a).append(ip_sp, " "); // this must be done this way as opposed to inserting into children above
            return $.create("li", {id: "indiv_pieces"}, [ip_a, ip_ul]);
        };

        var enableIPSubnav = function() {
            var hideIPLock = false;
            var hideIP = function() {
                setTimeout(function() {
                    if (!hideIPLock) $('#indiv_pieces a span, #indiv_pieces ul').hide();
                }, 250);
            };
            if (cur_sect != 'ip') {
                $('#indiv_pieces').mouseover(function() {
                    hideIPLock = true;
                    $('#indiv_pieces a span').show();
                    ip_top = $('#indiv_pieces').offset().top - $('#content').offset().top;
                    ip_buf = ($.browser.msie && $.browser.version >= 6 && $.browser.version < 7) ? -1 : 0;
                    $('#indiv_pieces ul').css("top", ip_top + ip_buf + "px");
                    $('#indiv_pieces ul').show();
                });
                $('#indiv_pieces').mouseout(function() {
                    hideIPLock = false;
                    hideIP();
                });
            } else {
                $('#indiv_pieces a, #indiv_pieces ul').unbind('mouseover').unbind('mouseout');
            }
            $('#indiv_pieces a').click(function() {
                if (!anim_lock) {
                    anim_lock = true;
                    if (ip_cats.length) {
                        var ipc = ip_cats[0]['id'];
                        hideCurSect(function() {
                            anim_lock = false;
                            cur_sect = 'ip';
                            cur_ip_cat = ipc;
                            buildCurSect();
                        });
                    }
                }
            });
            $('#indiv_pieces ul li a').unbind('click').click(function() {
                if (!anim_lock) {
                    anim_lock = true;
                    var ipc = $(this).attr('id').substr(3);
                    hideCurSect(function() {
                        anim_lock = false;
                        cur_sect = 'ip';
                        cur_ip_cat = ipc;
                        buildCurSect();
                    });
                }
            });
        };

        var resetIPSubnav = function() {
            var ip_lis_reset = buildIPSubnavLIs(ip_cats);
            if (ip_lis_reset.length) $('#indiv_pieces').replaceWith(buildIPLI(ip_lis_reset));
            enableIPSubnav();
        };

        var buildCurSect = function() {
            if (cur_sect) {
                $("#seasons_nav li").removeClass("selected");
                setFullImgWidth();
                resetIPSubnav();
                if (cur_sect == 'rs') {
                    $('#runway_slid').addClass("selected");
                    buildRunwaySlides();
                } else if (cur_sect == 'rv') {
                    $('#runway_vid').addClass("selected");
                    buildRunwayVideo();
                } else if (cur_sect == 'ip') {
                    $('#indiv_pieces').addClass("selected");
                    buildIndividualPieces();
                }
            }
        };

        var hideCurSect = function(cb) {
            if (cur_sect) {
                if (!$.browser.msie) $('#seasons_nav').fadeOut(anim_sp);
                if (cur_sect == 'rs') {
                    $('#thumbs').fadeOut(anim_sp);
                    $('#full').fadeOut(anim_sp, function() {
                        if ($.browser.msie) $('#seasons_nav').hide();
                        cb();
                    });
                } else if (cur_sect == 'rv') {
                    $('#runway_video').empty().fadeOut(anim_sp, function() {
                        if ($.browser.msie) $('#seasons_nav').hide();
                        cb();
                    });
                } else if (cur_sect == 'ip') {
                    if (cur_ip_cat) {
                        cur_ip_cat = null;
                        if ($.browser.msie) {
                            $('#indiv_pieces a span, #indiv_pieces ul').hide();
                        } else {
                            $('#indiv_pieces a span, #indiv_pieces ul').fadeOut(anim_sp);
                        }
                        $('#thumbs').fadeOut(anim_sp);
                        $('#full').fadeOut(anim_sp, function() {
                            if ($.browser.msie) $('#seasons_nav').hide();
                            cb();
                        });
                    } else {
                        setTimeout(function() {
                            if ($.browser.msie) $('#seasons_nav').hide();
                            cb();
                        }, 400); // 400 is jquery's "normal" speed
                    }
                }
            }
        };

        var buildRunwaySlides = function() {
            var slides = [];
            $.each(col_data['collections'], function(i, e) {
                if (cur_col == e['id']) {
                    $.each(e['seasons'], function(si, se) {
                        if (se['id'] == cur_season) {
                            $.each(se['runway_slides'], function(ri, re) {
                                slides.push(re);
                            });
                        }
                    });
                }
            });
            buildSlides(slides);
        };

        var buildRunwayVideo = function() {
            if (!anim_lock) {
                anim_lock = true;
                $('#runway_video').empty();
                var rv_path = null;
                $.each(col_data['collections'], function(i, e) {
                    if (cur_col == e['id']) {
                        $.each(e['seasons'], function(si, se) {
                            if (se['id'] == cur_season) rv_path = se['runway_video'];
                        });
                    }
                });

                var so = new SWFObject("./_swf/videoplayer.swf", "runway_video_swf", "100%", "100%", "9", "#FFFFFF");
                so.addParam("scale", "noscale");
                so.addVariable("vidPath", ((rv_path) ? rv_path : ""));
                var afterFadeIn = function() {
                    setTimeout(function() {
                        $('#runway_video').show();
                        so.write("runway_video");
                        setTimeout(function() { anim_lock = false; }, anim_stagger);
                    }, anim_stagger);
                };
                if ($.browser.msie) {
                    $('#seasons_nav').show();
                    afterFadeIn();
                } else {
                    $('#seasons_nav').fadeIn(anim_sp, afterFadeIn);
                }                
            }
        };

        var buildIndividualPieces = function() {
            var slides = [];
            $.each(col_data['collections'], function(i, e) {
                if (cur_col == e['id']) {
                    $.each(e['seasons'], function(si, se) {
                        if (se['id'] == cur_season) {
                            $.each(se['individual_pieces_categories'], function(ipci, ipce) {
                                if (ipce['id'] == cur_ip_cat) {
                                    $.each(ipce['individual_pieces'], function(ipi, ipe) {
                                        slides.push(ipe);
                                    });
                                }
                            });
                        }
                    });

                }
            });
            buildSlides(slides);
        };

        var buildSlides = function(slides) {
            anim_lock = true;

            $('#thumbs_list').empty().width(0);
            $('#full_img_wrap img').attr('src', img_spacer);

            var afterFadeIn = function() {
                if (cur_sect == 'ip') {
                    ip_top = $('#indiv_pieces').offset().top - $('#content').offset().top;
                    ip_buf = ($.browser.msie && $.browser.version >= 6 && $.browser.version < 7) ? -1 : 0;
                    $('#indiv_pieces ul').css("top", ip_top + ip_buf + "px");
                    if ($.browser.msie) {
                        $('#indiv_pieces a span, #indiv_pieces ul').show();
                    } else {
                        $('#indiv_pieces a span, #indiv_pieces ul').fadeIn(anim_sp);
                    }
                }

                slide_arr = [];
                var slide_lis = [];
                var preload_count = 0;
                
                $.each(slides, function(i, e) {
                    slide_arr.push(e);
                    if (i == 0) {
                        $('#full_img_wrap img').attr('src', img_loading);
                        $('#full_img_wrap img').attr('src', img_base + e['image_path']);
                        setSlideDesc(i, 0);
                    } else if (preload_count < preload_max) {
                        queuePreloadImg(img_base + e['image_path']);
                        preload_count++;
                    }
                    var slide_img = $.create("img", {src: img_base + e['image_path_thumb'], width: slide_thumb_dims[0], height: slide_thumb_dims[1]}, []);
                    slide_lis.push($.create("li", {}, [slide_img]));
                });

                // thumbnails
                if (slide_arr.length > 0) {
                    while (slide_arr.length < 3) {
                        slide_arr.push(slide_arr[0]);
                        slide_lis.push($(slide_lis[0]).clone()[0]);
                    }
                    slide_count = slide_lis.length;
                    // adjust the list so it starts with the last 3 and ends with the first
                    // 3 thumbnails so that we can loop around when scrolling horizontally
                    var slide_lis_new = [$(slide_lis[slide_lis.length - 3]).clone()[0], $(slide_lis[slide_lis.length - 2]).clone()[0], $(slide_lis[slide_lis.length - 1]).clone()[0]];
                    $.each(slide_lis, function(ri, re) { slide_lis_new.push(re); });
                    $.each(slide_lis, function(ri, re) { if (ri < 3) slide_lis_new.push($(re).clone()[0]); });
                    $('#thumbs_list').append(slide_lis_new).width(slide_width * (slide_count + 6));
                    // shift to the first slide
                    $('#thumbs_list').css("left", slide_left_pos);
                    setTimeout(function() { $('#thumbs').fadeIn(anim_sp); }, anim_stagger);
                } else {
                    $("#thumbs").hide();
                }

                // main image
                if (slide_lis.length > 0) {
                    setTimeout(function() { $('#full').fadeIn(anim_sp); }, (anim_stagger * 2));
                } else {
                    $('#full').hide();
                }

                // zoom
                $('#full .zoom a').unbind('click').click(function() { zoomImage(); });
                $('#full .back a').unbind('click').click(function() { unZoomImage(); });

                // see all
                $('#see_all_link a').unbind('click').click(function() { seeAll(); });
                $('#see_all .back a').unbind('click').click(function() { unSeeAll(); });
                $("#all_season a").unbind('click').click(function() {
                    cur_slide = ((parseInt(this.id.substr(2), 10) + ((slide_sa_page - 1) * slide_sa_thumb_count)) + 1);
                    if (cur_slide <= $(slide_arr).length) seeImage();
                });

                // slide counter
                cur_slide = 1;
                $('#thumbs_count').html(cur_slide+"/"+slide_count);

                // preload images
                setTimeout(function() {
                    anim_lock = false;
                    preloadImgs(preload_to);
                }, anim_stagger * 3);
            };
            if ($.browser.msie) {
                $('#seasons_nav').show();
                afterFadeIn();
            } else {
                $('#seasons_nav').fadeIn(anim_sp, afterFadeIn);
            }
            
            $('#thumb_nav #prev').unbind('click').click(function() {
                if (!anim_lock) {
                    cur_slide--;
                    animateSlides();
                }
            });

            $('#thumb_nav #next').unbind('click').click(function() {
                if (!anim_lock) {
                    cur_slide++;
                    animateSlides();
                }
            });
        };

        var animateSlides = function() {
            if (!anim_lock) {
                anim_lock = true;
                if ($.browser.msie && $.browser.version >= 6 && $.browser.version < 8)
                    $('#thumb_nav a').hide();
                else
                    $('#thumb_nav a').fadeOut(anim_sp_fast);
                var cur_slide_val = getCurSlideVal();
                $('#thumbs_count').html(cur_slide_val + "/" + slide_count);
                $('#thumbs_list').animate({left: (slide_left_pos - ((cur_slide - 1) * slide_width))}, anim_sp, "linear",    function() {
                    anim_lock = false;
                    if ($.browser.msie && $.browser.version >= 6 && $.browser.version < 7)
                        $('#thumb_nav a').show();
                    else
                        $('#thumb_nav a').fadeIn(anim_sp_fast);
                    if (cur_slide <= -1) {
                        $('#thumbs_list').css("left", (slide_left_pos - ((slide_count - 2) * slide_width)));
                        cur_slide = (slide_count - 1);
                    } else if (cur_slide >= (slide_count + 1)) {
                        $('#thumbs_list').css("left", slide_left_pos);
                        cur_slide = 1;
                    }
                });
                $('#full_img_wrap').fadeOut(anim_sp, function() {
                    $('#full_img_wrap img').attr('src', img_spacer);
                    $('#full_img_wrap img').attr('src', img_loading);
                    $('#full_img_wrap img').attr('src', img_base + slide_arr[cur_slide_val - 1]['image_path']);
                    $('#full_img_wrap').fadeIn(anim_sp);
                });
                $('#full ul').fadeOut(anim_sp, function() { setSlideDesc((cur_slide_val - 1), anim_sp) });
            }
        };

        var setFullImgWidth = function() {
            var w = (cur_sect == 'rs') ? rs_w : ((cur_sect == 'ip') ? ip_w : 0);
            $('#full').css('width', w+img_buf+"px");
            $('#full_img_wrap').css('width', w+'px');
            $('#full_img_wrap img').attr('width', w);
        };

        var seeAll = function() {
            if (!anim_lock) {
                anim_lock = true;
                cur_nav_slide = cur_slide;
                if (!$.browser.msie) $('#seasons_nav, #aside').fadeOut(anim_sp_fast);
                $('#thumbs').fadeOut(anim_sp_fast);
                $('#full').fadeOut(anim_sp_fast, function() {
                    if ($.browser.msie) $('#seasons_nav, #aside').hide();
                    $('#full .zoom a').unbind('click');
                    $('#full .back a').unbind('click').click(function() { unSeeImage(); });
                    $('#full .zoom').hide();
                    $('#full').css('width', zoom_w+img_buf+"px");
                    $('#full_img_wrap').css('width', zoom_w+'px');
                    $('#full_img_wrap img').removeAttr('width');
                    $('#full_img_wrap img').removeAttr('height');
                    $('#full_img_wrap img').attr('src', img_spacer);
                    var sa_a = $("#all_season a");
                    if (sa_a.length) {
                        slide_sa_page = 1;
                        slide_sa_page_count = Math.ceil(slide_arr.length / slide_sa_thumb_count);
                        anim_lock = false;
                        seeAllPagination();
                        seeAllDrawPage();
                    }
                    $('#see_all').fadeIn(anim_sp, function() {
                        anim_lock = false;
                    });
                });
            }
        };

        var unSeeAll = function() {
            if (!anim_lock) {
                anim_lock = true;
                cur_slide = cur_nav_slide;
                var cur_slide_val = getCurSlideVal();
                $('#full .zoom a').unbind('click').click(function() { zoomImage(); });
                $('#full .back a').unbind('click').click(function() { unZoomImage(); });
                $('#full .zoom').show();
                setFullImgWidth();
                $('#full_img_wrap img').attr('height', img_h);
                $('#full_img_wrap img').attr('src', img_base + slide_arr[cur_slide_val - 1]['image_path']);
                setSlideDesc((cur_slide_val - 1), 0);
                $('#see_all').fadeOut(anim_sp_fast, function() {
                    if ($.browser.msie) {
                        $('#seasons_nav, #aside').show();
                    } else {
                        $('#seasons_nav, #aside').fadeIn(anim_sp);
                    }
                    $('#thumbs').fadeIn(anim_sp);
                    $('#full ul, #full .zoom, #full_img, #full_img_wrap').show();
                    $('#full').fadeIn(anim_sp, function() {
                        anim_lock = false;
                    });
                });
            }
        };

        var seeAllDrawPage = function() {
            if (!anim_lock && (slide_sa_page != -1)) {
                anim_lock = true;
                var sa_a = $("#all_season a");
                var rsi_min = ((slide_sa_page - 1) * slide_sa_thumb_count);
                var rsi_max = Math.min((slide_sa_page * slide_sa_thumb_count), $(slide_arr).length);
                $.each(slide_arr, function(rsi, rse) {
                    if ((rsi >= rsi_min) && (rsi < rsi_max)) {
                        var sa_a_index = (rsi - (slide_sa_thumb_count * (slide_sa_page - 1)));
                        $("img", sa_a[sa_a_index]).attr('src', img_spacer);
                        $("img", sa_a[sa_a_index]).attr('src', img_base + slide_arr[rsi]['image_path_thumb']);
                        $(sa_a[sa_a_index]).css('cursor', 'pointer');
                        $("span span", sa_a[sa_a_index]).fadeIn(anim_sp, ((rsi == (rsi_max - 1))?function() {
                            anim_lock = false;
                        }:function(){}));
                    }
                });
                var slide_sa_last_thumb_count = (slide_sa_page_count * slide_sa_thumb_count);
                if (slide_sa_page == slide_sa_page_count) {
                    if (slide_arr.length < (slide_sa_page_count * slide_sa_thumb_count)) {
                        for (var i = slide_arr.length; i < slide_sa_last_thumb_count; i++) {
                            var sa_a_index = (i - (slide_sa_thumb_count * (slide_sa_page - 1)));
                            $("img", sa_a[sa_a_index]).attr('src', img_spacer);
                            $(sa_a[sa_a_index]).css('cursor', 'default');
                            $("span span", sa_a[sa_a_index]).fadeIn(anim_sp);
                        }
                    }
                } else {
                    anim_lock = true;
                }
            }
        };

        var seeAllHidePage = function(cb) {
            if (!anim_lock) {
                anim_lock = true;
                var sa_a = $("#all_season a");
                var rsi_min = ((slide_sa_page - 1) * slide_sa_thumb_count);
                var rsi_max = Math.min((slide_sa_page * slide_sa_thumb_count), $(slide_arr).length);
                $.each(slide_arr, function(rsi, rse) {
                    if ((rsi >= rsi_min) && (rsi < rsi_max)) {
                        var sa_a_index = (rsi - (slide_sa_thumb_count * (slide_sa_page - 1)));
                        $("span span", sa_a[sa_a_index]).fadeOut(anim_sp, ((rsi == (rsi_max - 1))?function() {
                            anim_lock = false;
                            cb();
                        }:function(){}));
                    }
                });
            }
            var slide_sa_last_thumb_count = (slide_sa_page_count * slide_sa_thumb_count);
            if (slide_sa_page == slide_sa_page_count) {
                if (slide_arr.length < (slide_sa_page_count * slide_sa_thumb_count)) {
                    for (var i = slide_arr.length; i < slide_sa_last_thumb_count; i++) {
                        var sa_a_index = (i - (slide_sa_thumb_count * (slide_sa_page - 1)));
                        $("span span", sa_a[sa_a_index]).fadeOut(anim_sp);
                    }
                }
            } else {
                anim_lock = true;
            }
        };

        var zoomImage = function() {
            if (!anim_lock) {
                anim_lock = true;
                var cur_slide_val = getCurSlideVal();
                if (!$.browser.msie) $('#seasons_nav, #aside').fadeOut(anim_sp_fast);                
                $('#thumbs, #full ul, #full .zoom').fadeOut(anim_sp_fast);
                $('#full_img_wrap').fadeOut(anim_sp_fast, function() {
                    if ($.browser.msie) $('#seasons_nav, #aside').hide();
                    $('#full_img_wrap img').attr('src', img_spacer);
                    $('#full_img_wrap img').attr('src', img_zoom_loading);
                    $('#full').animate({width: zoom_w+img_buf}, anim_sp_slow, "linear", function() {
                        $('#full_img_wrap').css('width', zoom_w+'px');
                        $('#full_img_wrap img').removeAttr('width').removeAttr('height');
                        $('#full_img_wrap img').attr('src', img_base + slide_arr[cur_slide_val - 1]['zoom_image_path']);
                        $("#full_img_wrap img").css("margin-left", "0px").css("margin-top", "0px");
                        $('#full_img_wrap, #full ul').fadeIn(anim_sp);
                        $('#full .back').fadeIn(anim_sp, function() {
                            anim_lock = false;
                            enableZoom();
                        });
                    });
                });
            }
        };

        var unZoomImage = function() {
            if (!anim_lock) {
                anim_lock = true;
                disableZoom(false);
                var cur_slide_val = getCurSlideVal();
                $('#full_img_wrap, #full ul').fadeOut(anim_sp_fast);
                $('#full .back').fadeOut(anim_sp_fast, function() {
                    disableZoom();
                    $('#full_img_wrap img').attr('src', img_spacer);
                    $('#full_img_wrap img').attr('src', img_loading);
                    var w = (cur_sect == 'rs') ? rs_w : ((cur_sect == 'ip') ? ip_w : 0);
                    $('#full').animate({width: w+img_buf}, anim_sp_slow, "linear", function() {
                        disableZoom();
                        setFullImgWidth();
                        $('#full_img_wrap img').attr('height', img_h);
                        $('#full_img_wrap img').attr('src', img_base + slide_arr[cur_slide_val - 1]['image_path']);
                        if ($.browser.msie) {
                            $('#seasons_nav, #aside').show();
                        } else {
                            $('#seasons_nav, #aside').fadeIn(anim_sp);
                        }
                        $('#thumbs, #full ul, #full .zoom').fadeIn(anim_sp);
                        $('#full_img_wrap').fadeIn(anim_sp, function() {
                            disableZoom();
                            anim_lock = false;
                        });
                    });
                });
            }
        };

        var seeImage = function() {
            if (!anim_lock) {
                anim_lock = true;
                var cur_slide_val = getCurSlideVal();
                $('#see_all').fadeOut(anim_sp_fast, function() {
                    $('#full_img_wrap img').attr('src', img_base + slide_arr[cur_slide_val - 1]['zoom_image_path']);
                    setSlideDesc((cur_slide_val - 1), 0);
                    $('#full_img, #full_img_wrap, #full ul, #full .back').show();
                    $('#full').fadeIn(anim_sp, function() {
                        anim_lock = false;
                        enableZoom();
                    });
                });
            }
        };

        var unSeeImage = function() {
            if (!anim_lock) {
                anim_lock = true;
                disableZoom(false);
                cur_slide_val = getCurSlideVal();
                $('#full').fadeOut(anim_sp, function() {
                    disableZoom();
                    $('#full_img_wrap img').attr('src', img_spacer);
                    $('#full_img, #full_img_wrap, #full ul, #full .back').hide();
                    $('#see_all').fadeIn(anim_sp_fast, function() {
                        disableZoom();
                        anim_lock = false;
                    });
                });
            }
        };

        var enableZoom = function() {
            setTimeout(function() {
                var panImage = function() {
                    cur_slide_val = getCurSlideVal();
                    if ((parseInt(slide_arr[cur_slide_val - 1]['zoom_image_w'], 10) > 0) && (parseInt(slide_arr[cur_slide_val - 1]['zoom_image_h'], 10) > 0)) {
                        $("#full_img_wrap").mousemove(function(e) { 
                            var fiwo = $("#full_img_wrap").offset();
                            var fiwx = (e.pageX - fiwo.left);
                            var fiwy = (e.pageY - fiwo.top);
                            if (fiwx < zoom_buf) fiwx = 0;
                            if (fiwx > (zoom_w - zoom_buf)) fiwx = zoom_w;
                            if (fiwy < zoom_buf) fiwy = 0;
                            if (fiwy > (img_h - zoom_buf)) fiwy = img_h;
                            var fiwl = parseInt(((fiwx/zoom_w)*(slide_arr[cur_slide_val - 1]['zoom_image_w']-zoom_w)), 10);
                            var fiwt = parseInt(((fiwy/img_h)*(slide_arr[cur_slide_val - 1]['zoom_image_h']-img_h)), 10);
                            $('#full_img_wrap img').css("margin-left", (0-fiwl)+"px").css("margin-top", (0-fiwt)+"px");
                        });
                    }
                };
                panImage();
                $("#full_img_wrap").mouseover(function() { panImage(); });
                $("#full_img_wrap").mouseout(function() { $("#full_img_wrap").unbind('mousemove'); });
                $("#full_img_wrap").css("cursor", "move");
            }, zoom_enable_to);
        };

        var disableZoom = function(repos) {
            repos = (typeof(repos) == "undefined") ? true : repos;
            $("#full_img_wrap").unbind('mouseover').unbind('mousemove').css("cursor", "default");
            if (repos) {
                $('#full_img_wrap img').css("margin-left", "0px");
                $('#full_img_wrap img').css("margin-top", "0px");
            }
        };

        var seeAllPagination = function() {
            if (slide_sa_page_count > 1) {
                $("#pagination").show();
                seeAllPaginationSet();
                $("#pagination li a").unbind('click').click(function() {
                    var offset = ($(this).parent()[0].id == "sa_prev") ? -1 : (($(this).parent()[0].id == "sa_next") ? 1 : 0);
                    seeAllPaginate(offset);
                });
            } else {
                $("#pagination").hide();
            }
        };

        var seeAllPaginationSet = function() {
            if (slide_sa_page == 1) {
                $("#pagination #sa_prev a").css('text-decoration', 'line-through');
                $("#pagination #sa_prev a").css('cursor', 'default');
            } else {
                $("#pagination #sa_prev a").css('text-decoration', 'none');
                $("#pagination #sa_prev a").css('cursor', 'pointer');
            }
            if (slide_sa_page == slide_sa_page_count) {
                $("#pagination #sa_next a").css('text-decoration', 'line-through');
                $("#pagination #sa_next a").css('cursor', 'default');
            } else {
                $("#pagination #sa_next a").css('text-decoration', 'none');
                $("#pagination #sa_next a").css('cursor', 'pointer');
            }
        };

        var seeAllPaginate = function(offset) {
            if (((offset == 1) && (slide_sa_page < slide_sa_page_count)) || ((offset == -1) && (slide_sa_page > 1))) {
                seeAllHidePage(function() {
                    slide_sa_page += offset;
                    seeAllPaginationSet();
                    seeAllDrawPage();
                });
            }
        };

        var setSlideDesc = function(rsi, fsp) {
            var lines = slide_arr[rsi]['description'].split("\n");
            $('#full ul').empty();
            $.each(lines, function(li, le) {
                if (le.length) $('#full ul').append($.create("li", {}, [htmlentities(le)]));
            });
            $('#full ul').fadeIn(fsp);
        };

        var getCurSlideVal = function() {
            return (cur_slide < 1) ? (cur_slide + slide_count) : ((cur_slide > slide_count) ? (cur_slide - slide_count) : cur_slide);
        };

        var htmlentities = function(str) {
            var ta = document.createElement("textarea");
            ta.innerHTML = str;
            return ta.value;
        };
    }
});

/*
 * Runway Video
 * this sits outside of jQuery so Flash can see it
 */

var swf_w_small = 420;
var swf_h_small = 284;
var swf_w_large = 680;
var swf_h_large = 460;
var swf_wpad = 4;
var swf_hpad = 42;
var largeView = false;

function toggleSizeMode() {
    $("#runway_video").stop();
    if (largeView) {
        $("#runway_video").width(swf_w_small + swf_wpad);
        $("#runway_video").height(swf_h_small + swf_hpad);
        if ($.browser.msie) {
            $('#seasons_nav, #aside').show();
        } else {
            $('#seasons_nav, #aside').fadeIn("normal");
        }
    } else {
        var afterFadeOut = function() {
            $("#runway_video").width(swf_w_large + swf_wpad);
            $("#runway_video").height(swf_h_large + swf_hpad);
        };
        if ($.browser.msie) {
            $('#seasons_nav, #aside').hide();
            afterFadeOut();
        } else {
            $('#aside').fadeOut("normal");
            $('#seasons_nav').fadeOut("normal", afterFadeOut);
        }
    }
    largeView = !largeView;
}
