function sleep(millis)  {   var date = new Date();   var curDate = null;   do { curDate = new Date(); }   while(curDate-date < millis); } 

var BrowserDetect = {
    init: function () {
        this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
        this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
        this.OS = this.searchString(this.dataOS) || "an unknown OS";
    },
    searchString: function (data) {
        for (var i = 0; i < data.length; i++) {
            var dataString = data[i].string;
            var dataProp = data[i].prop;
            this.versionSearchString = data[i].versionSearch || data[i].identity;
            if (dataString) {
                if (dataString.indexOf(data[i].subString) != -1)
                    return data[i].identity;
            }
            else if (dataProp)
                return data[i].identity;
        }
    },
    searchVersion: function (dataString) {
        var index = dataString.indexOf(this.versionSearchString);
        if (index == -1) return;
        return parseFloat(dataString.substring(index + this.versionSearchString.length + 1));
    },
    dataBrowser: [
    {
        string: navigator.userAgent,
        subString: "Chrome",
        identity: "Chrome"
    },
    {
        string: navigator.userAgent,
        subString: "OmniWeb",
        versionSearch: "OmniWeb/",
        identity: "OmniWeb"
    },
    {
        string: navigator.vendor,
        subString: "Apple",
        identity: "Safari",
        versionSearch: "Version"
    },
    {
        prop: window.opera,
        identity: "Opera",
        versionSearch: "Version"
    },
    {
        string: navigator.vendor,
        subString: "iCab",
        identity: "iCab"
    },
    {
        string: navigator.vendor,
        subString: "KDE",
        identity: "Konqueror"
    },
    {
        string: navigator.userAgent,
        subString: "Firefox",
        identity: "Firefox"
    },
    {
        string: navigator.vendor,
        subString: "Camino",
        identity: "Camino"
    },
    {		// for newer Netscapes (6+)
        string: navigator.userAgent,
        subString: "Netscape",
        identity: "Netscape"
    },
    {
        string: navigator.userAgent,
        subString: "MSIE",
        identity: "Explorer",
        versionSearch: "MSIE"
    },
    {
        string: navigator.userAgent,
        subString: "Gecko",
        identity: "Mozilla",
        versionSearch: "rv"
    },
    { 		// for older Netscapes (4-)
        string: navigator.userAgent,
        subString: "Mozilla",
        identity: "Netscape",
        versionSearch: "Mozilla"
    }
	],
    dataOS: [
    {
        string: navigator.platform,
        subString: "Win",
        identity: "Windows"
    },
    {
        string: navigator.platform,
        subString: "Mac",
        identity: "Mac"
    },
    {
        string: navigator.userAgent,
        subString: "iPhone",
        identity: "iPhone/iPod"
    },
    {
        string: navigator.platform,
        subString: "Linux",
        identity: "Linux"
    }
	]

};
BrowserDetect.init();


var LOADING = false;

var SECTIONWEIGHT = 50;
var THUMBNAILWEIGHT = 10;
var PAGEWEIGHT = 40;
var COLLECTIONWEIGHT = 90;

var STATUS = {};
var THUMBNAILS = [];
var CURRENTPAGE = null;
var CURRENTSPPAGE = null;

var SPMODE = false;
var SPTIMER = null;

var COLLECTIONMODE = false;
var PRODUCTMODE = false;
var CURRENTCOLLECTION = null;

var ARROWSON = false;

//var CSSSHOW = { "opacity": 0, "display": "block"

var IDHASH = [];

var AR = 16 / 9;

STATUS.linksShown = false;

var Fx = {
    MainText: 100,
    MainTextIn: 500,
    MainTextOut: 500,
    SubText: 100,
    SubTextIn: 500,
    SubTextOut: 500,
    SubLinks: 100,
    SubLinksIn: 500,
    SubLinksOut: 500,
    Arrows: 150,
    PageNav: 250,
    PageNavIn: 150,
    PageNavOut: 250,
    PageNavContainer: 500,
    Immediately: 10,
    CollectionLinks: 150,
    CollectionLogo: 150,
    ProductImages: 150,
    SpecialPage: 1000,
    SpecialPageIn: 1000,
    SpecialPageOut: 1000,
    NextSpecialPage: 5000,
    Thumbnails: 1500,
    ThumbnailsIn: 1500,
    ThumbnailsOut: 1500,
    MainImage: 250,
    MainImageIn: 250,
    MainImageOut: 250,
    FooterLinks: 1500,
    FooterLinksIn: 500,
    FooterLinksOut: 1500
};

nFx = {
    MainText: 1,
    MainTextIn: 1,
    MainTextOut: 1,
    SubText: 1,
    SubTextIn: 1,
    SubTextOut: 1,
    SubLinks: 1,
    SubLinksIn: 1,
    SubLinksOut: 1,
    Arrows: 0,
    PageNav: 0,
    PageNavIn: 0,
    PageNavOut: 0,
    PageNavContainer: 0,
    Immediately: 0,
    CollectionLinks: 0,
    CollectionLogo: 0,
    ProductImages: 0,
    SpecialPage: 0,
    SpecialPageIn: 0,
    SpecialPageOut: 0,
    NextSpecialPage: 5000,
    Thumbnails: 0,
    ThumbnailsIn: 0,
    ThumbnailsOut: 0,
    MainImage: 0,
    MainImageIn: 0,
    MainImageOut: 0,
    FooterLinks: 0,
    FooterLinksIn: 0,
    FooterLinksOut: 0
};
/*
nFx = {
    MainText            : 150,
    MainTextIn          : 150,
    MainTextOut         : 150,
    SubText             : 150,
    SubTextIn           : 150,
    SubTextOut          : 150,
    SubLinks            : 150,
    SubLinksIn          : 150,
    SubLinksOut         : 150,
    Arrows              : 150,
    PageNav             : 250,
    PageNavIn           : 150,
    PageNavOut          : 250,
    PageNavContainer    : 500,
    Immediately         : 10,
    CollectionLinks     : 150,
    CollectionLogo      : 150,
    ProductImages       : 0,
    SpecialPage         : 0,
    SpecialPageIn       : 0,
    SpecialPageOut      : 0,
    NextSpecialPage     : 5000,
    Thumbnails          : 1500,
    ThumbnailsIn        : 1500,
    ThumbnailsOut       : 1500,
    MainImage           : 0,
    MainImageIn         : 0,
    MainImageOut        : 0,
    FooterLinks         : 1500,
    FooterLinksIn       : 500,
    FooterLinksOut      : 1500
};*/

var RenderModes = {
    Modern: 1000,
    Ie9: 750,
    Old: 1
};

var friendlyModes = [];
friendlyModes[RenderModes.Modern] = "Modern";
friendlyModes[RenderModes.Ie9] = "Ie9";
friendlyModes[RenderModes.Old] = "Old";

var RM = RenderModes.Modern;

var dc = 0;
var d1 = (new Date()).getTime();
for (var i = 0; i < 10; i++) { dc += Math.random(); }
var d2 = (new Date()).getTime();
var diff = d2 - d1;
var Timeouts = Fx; // (diff < 6) ? Fx : nFx;

var browser = BrowserDetect.browser.toLowerCase();
var os = BrowserDetect.OS.toLowerCase();
var version = BrowserDetect.version;

if ("explorer" == browser) {
    if (9 == version) {
        RM = RenderModes.Ie9;
    }
    if (9 > version) {
        RM = RenderModes.Old;
    }
}

var friendlyMode = friendlyModes[RM];

if (RM == RenderModes.Old) {
    Timeouts = nFx;
}

//jQuery.fx.interval = 33;

jQuery(function () {

    //var dbg = jQuery("<div id='dbg-info' />");
    //dbg.html(friendlyMode + " = " + browser + " / " + version + " / " + os + " / " + location.hash);
    //dbg.appendTo("body");

    var win = jQuery(window);

    var square = jQuery("#square");
    var loader = jQuery("#loader");
    var loaderText = jQuery("#loader-text");
    var loaderPercent = loaderText.find("b");
    var loaderSection = loaderText.find("span");

    var pageNav = jQuery("#page-nav");
    var pageNavContainer = jQuery("#page-nav-container");
    var mainImages = jQuery("#main-image-set");
    var mainText = jQuery("#main-text");
    var subText = jQuery("#sub-text");
    var subLinks = jQuery("#sub-links");

    var videoBox = jQuery("#video-box");
    var videoContent = jQuery("#video-box-content-container");

    var contactBox = jQuery("#contact-box");
    var contactContent = jQuery("#contact-box-content");
    var contactPrevLink = jQuery("#contact-box-prev");
    var contactNextLink = jQuery("#contact-box-next");
    var contactCloseLink = jQuery("#contact-box-close");
    var contactLinks = jQuery("a.contact-main-link");
    var maxContacts = Math.ceil(contactContent.children("div").length / 4);
    var currentContactPage = 0;

    var nwsMain = jQuery("#news-box");
    var nwsImage = jQuery("#news-box-image");
    var nwsContent = jQuery("#news-box-content");
    var nwsLinks = null; // jQuery("#news-box-links");
    var nwsContentHeight = nwsContent.height();
    var nwsInnerHtml = nwsMain.html();
	
	// alert (nwsInnerHtml);

    var cbMain = jQuery("#content-box");
    var cbImage = jQuery("#content-box-image");
    var cbContent = jQuery("#content-box-content");
    var cbContentHeight = cbContent.height();
    var cbInnerHtml = cbMain.html();

    var collectionsLink = jQuery("#link-collections");
    var arrowLeft = jQuery("#arrow-left");
    var arrowRight = jQuery("#arrow-right");
    var exploreCollectionLink = jQuery("#explore-collection-link");
    var shopCollectionLink = jQuery("#shop-collection-link");
    var catalogCollectionLink = jQuery("#catalog-collection-link");
    var videoCollectionLink = jQuery("#video-collection-link");
    var collectionActionLinks = jQuery("#collection-action-links");
    var collectionActions = collectionActionLinks.find("a");

    var collectionLinks = jQuery("#explore-collection-link,#shop-collection-link");
    var collectionLogo = jQuery("#collection-logo");
    var productArrowLeft = jQuery("#product-arrow-left");
    var productArrowRight = jQuery("#product-arrow-right");
    var productImages = jQuery("#product-image-set");
    var productDataWrapper = jQuery("#product-data-wrapper");
    var productData = jQuery("#product-data");

    var worldLink = jQuery("#link-world");
    var locatorLink = jQuery("#link-micro-bl");

    var mainSet = jQuery("#sub-links");

    var footer = jQuery("#footer");
    var footerLinks = jQuery("#links-mini,#links-micro,#links-nano,#copyright-link");

    var toggleLink = function (el, enable) {
        el.css({
        // "opacity" : (enable ? 1 : 0.3),
            "cursor": (enable ? "pointer" : "default")
        });

        if (enable) {
            el.removeClass();
        } else {
            el.addClass("disabled");
        }
    };

    /*
    var resizeImages = function() {
        var ww = win.width();
        var wh = win.height();
        var h = wh - jQuery("#footer").height();
        mainImages.height(h);
        productImages.height(h);
        productDataWrapper.height(h);
        var imgs = mainImages.find("img");

        //var iw = AR * h;
        //var mw = parseInt(ww - iw)/2;

        //imgs.width(iw);
        //imgs.height(h);
        //imgs.css("left" , mw);

        //var ih = ww/AR;

        imgs.width(ww);
        imgs.height(ih);

        if (ih < h) {
            var iw = h * AR;
            imgs.width(iw);
            imgs.height(h);
        } else {
            imgs.width(ww);
            imgs.height(ih);
        }
    };
*/

    var resizeImages = function () {
        var ww = win.width();
        var wh = win.height();
        var h = wh - jQuery("#footer").outerHeight();
        mainImages.height(h);
        productImages.height(h);
        productDataWrapper.height(h);
        var imgs = mainImages.find("img");
        var ih = ww / AR;
        if (ih < h) {
            var iw = h * AR;
            imgs.width(iw);
            imgs.height(h);
        } else {
            imgs.width(ww);
            imgs.height(ih);
        }
    }

    win.resize(function () {
        resizeImages();
        if (COLLECTIONMODE) {
            adjustCollectionElements();
        } else {
            adjustText();
        }

    });

    /*
	jQuery.ajax({
		type: "POST",
		dataType: "json",
		url: "http://192.168.1.104/paspaley/extras/site",
		data: {
			"op": "page-data"
		},
		error: function(jqXHR, textStatus, errorThrown) {
            alert("Error");
		},
		success: function(data, textStatus, jqXHR) {
			if (data && 0 < data.length) {
                DATA = data;
                setTimeout(processData, 50);
                //alert("Success");
			} else {
                //alert("Data Error");
			}
		}
	});*/

    var processSpData = function () {

        msg("Processing sp data...");

        STATUS.spPagesLeft = SPDATA.length;
        STATUS.spIndex = 0;

        jQuery.each(SPDATA, function () {
            var thz = this;
            msg(thz.title);
            processSpecialImages(thz);
        });


    };

    var processSpecialImages = function (spPage) {

        var img = jQuery("<img />");
        img.load(function () {
            msg("Loaded " + spPage.image + "...");
            spPage.img = jQuery(this);
            STATUS.spPagesLeft--;

            var percent = STATUS.spPagesLeft / SPDATA.length;
            var p = parseInt(percent * 128);
            var pp = parseInt((1 - percent) * SECTIONWEIGHT);
            square.animate({ "margin-top": p });
            loaderPercent.html(pp + "%");

            msg("Percentage..." + percent);

            if (0 == STATUS.spPagesLeft) {
                loader.fadeOut(2000, function () {
                    setTimeout(spPagesLoaded, 50);
                });
            }
        });
        img.attr("src", spPage.image);

    };

    var spPagesLoaded = function () {
        jQuery("#footer").show();
        msg("All SP Images loaded...");
        jQuery("#logo").click(logoClicked).css("cursor", "pointer");
        worldLink.click(logoClicked);
        setTimeout(function () { nextSpPage(false); }, 50);
        setTimeout(processData, 50);
    };

    var logoClicked = function () {

        if (COLLECTIONMODE) {
            pageNav.animate({
                "opacity": 0
            }, Timeouts.PageNavOut, "swing", function () {
                jQuery.each(CLDATA, function () {
                    var thz = this;
                    thz.link.detach();
                });
                jQuery.each(DATA, function () {
                    var thz = this;
                    thz.link.appendTo(pageNav);
                });
                var w = 128 * (DATA.length);
                pageNav.width(w);
                pageNav.animate({
                    "opacity": 1
                }, Timeouts.PageNavOut, "swing", function () {
                    setTimeout(nextSpPage, Timeouts.Immediately);
                });
            });
        } else {
            if (!SPMODE) {
                setTimeout(nextSpPage, Timeouts.Immediately);
            } else {
                msg("SPMODE: skipping...");
            }
        }
        return false;
    };

    var IGNORENEXT = false;

    var nextSpPage = function (force) {

        //msg(" ");
        //msg("next sp page");
        //msg(" ");

        if (force || COLLECTIONMODE) {

            if (ARROWSON) {
                arrowLeft.animate({
                    "opacity": 0
                }, Timeouts.Arrows, "swing", function () {
                    arrowLeft.css("display", "none");
                });
                arrowRight.animate({
                    "opacity": 0
                }, Timeouts.Arrows, "swing", function () {
                    arrowRight.css("display", "none");
                });
            }
            msg("Hiding collection links");

            collectionActionLinks.animate({
                "opacity": 0
            }, Timeouts.CollectionLinks, "swing", function () {
                msg("Collection links hidden");
                //exploreCollectionLink.css("display", "none");
                collectionActionLinks.css("display", "none");
            });

            //exploreCollectionLink.animate({
            //    "opacity": 0
            //}, 150, "swing", function () {
            //    exploreCollectionLink.css("display", "none");
            //});
            //shopCollectionLink.animate({
            //    "opacity": 0
            //}, 150, "swing", function () {
            //    shopCollectionLink.css("display", "none");
            //});
            collectionLogo.animate({
                "opacity": 0
            }, Timeouts.CollectionLogo, "swing", function () {
                collectionLogo.css("display", "none");
            });
            productArrowLeft.animate({
                "opacity": 0
            }, Timeouts.Arrows, "swing", function () {
                productArrowLeft.css("display", "none");
            });
            productArrowRight.animate({
                "opacity": 0
            }, Timeouts.Arrows, "swing", function () {
                productArrowRight.css("display", "none");
            });
            if (PRODUCTMODE) {
                productImages.animate({
                    "opacity": 0
                }, Timeouts.ProductImages);
            }

            toggleLink(collectionsLink, true);

        }

        COLLECTIONMODE = false;
        PRODUCTMODE = false;
        SPMODE = true;
        mainSet.css("z-index", 1000);

        toggleLink(worldLink, false);




        if (CURRENTSPPAGE) {
            //CURRENTSPPAGE.img.detach();
        }

        var spPage = SPDATA[STATUS.spIndex];
        spPage.img.css("opacity", 0);
        spPage.img.appendTo(mainImages);
        mainImages.css("background-color", spPage.backgroundColor ? spPage.backgroundColor : "#fff");

        //var tx = spPage.imageTextX ? spPage.imageTextX : "50";
        //var ty = spPage.imageTextY ? spPage.imageTextY : "25";
        //var ta = (spPage.imageTextX || spPage.imageTextY) ? "left" : "center";

        subText.empty();

        subLinks.animate({
            "opacity": 0//,
            //"left": "+=20"
        }, Timeouts.SubLinksOut, "swing", function () {
            subLinks.empty();

            subLinks.addClass("slideshow");

            STATUS.linksShown = false;
            if (spPage.externalUrl) {
                var a = jQuery("<a href='#'></a>");
                a.html(spPage.linkText);
                a.css({
                    "color": spPage.textColor
                });
                a.attr("href", spPage.externalUrl);
                subLinks.append(a);
                STATUS.linksShown = true;
            }

            if (!spPage.externalUrl && spPage.internalLink) {
                var a = jQuery("<a href='#'></a>");
                a.data("hash-id", spPage.internalLink);
                a.html(spPage.linkText ? spPage.linkText : "Explore");
                a.css({
                    "color": spPage.textColor
                });
                a.click(function () {
                    var thz = jQuery(this);
                    var hid = thz.data("hash-id");
                    var link = IDHASH[hid];
                    msg("Internal link clicked, hid: " + hid + " " + link);
                    if (link) {
                        if (SPTIMER) { clearTimeout(SPTIMER); }
                        link.trigger("click");
                    }
                    return false;
                });
                subLinks.append(a);
                STATUS.linksShown = true;
            }

            mainText.animate({
                "opacity": 0//,
                //"left": "-=20"
            }, Timeouts.MainTextOut, "swing", function () {
                spPage.img.animate({ "opacity": 1 }, Timeouts.SpecialPageIn);
				//mainText.text(spPage.title);
                mainText.html(spPage.title);

                //mainText.css({
                //    "color": spPage.textColor,
                //     "left": tx + "%",
                //     "top": ty + "%"
                //});

                updateTextPosition();

                mainText.css({
                    "left": STATUS.Text.MainTextX,
                    "top": STATUS.Text.MainTextY,
                    "color": spPage.textColor
                });

                mainText.css("left", STATUS.Text.MainTextX);
                mainText.animate({
                    //"left": STATUS.Text.MainTextX,
                    "opacity": 1.0
                }, Timeouts.MainTextIn, "swing", function () {
                    var subLinkFrom = {
                        "top": STATUS.Text.SubLinksY + "px",
                        "left": (STATUS.Text.SubLinksX) + "px",
                        "opacity": 0.0
                    };
                    var subLinkTo = {
                        //"top": STATUS.Text.SubLinksY + "px",
                        //"left": STATUS.Text.SubLinksX + "px",
                        "opacity": 1.0
                    };
                    subLinks.css(subLinkFrom);
                    subLinks.animate(subLinkTo, Timeouts.SubLinksIn);
                });
            });
        });

        //a.click(subpageClicked);
        //adjustText();
        resizeImages();

        ///this.appendTo(mainImages);
        ///msg("Image width: " + this.width() + " height: " + this.height());

        CURRENTSPPAGE = spPage;

        STATUS.spIndex++;
        if (SPDATA.length <= STATUS.spIndex) {
            STATUS.spIndex = 0;
        }

        if (SPTIMER) { clearTimeout(SPTIMER); }
        SPTIMER = null;
        SPTIMER = setTimeout(nextSpPage, Timeouts.NextSpecialPage);

        return false;

    };

    /* PAGES */

    var processData = function () {

        STATUS.thumbsLeft = DATA.length;
        STATUS.pagesLeft = DATA.length;
        STATUS.thumbsLoaded = 0;
        STATUS.pagesLoaded = 0;

        loaderSection.html(TEXT["thumbnails"]);

        var w = 128 * (DATA.length);
        pageNav.width(w);
        msg("page nav width " + w + " " + STATUS.thumbsLeft);

        jQuery.each(DATA, function () {
            var thz = this;
            msg(thz.title);
            loadThumbnailImages(thz);
        });

    };


    var hidePageThumbnails = function () {
    };

    var loadThumbnailImages = function (page, isCollection) {

        msg("Loading thumbnails for " + (isCollection ? "collection " : "") + page.title + "...");
        msg("Loading " + page.thumbnail + "...");
        msg("Loading " + page.thumbnailGreyscale + "...");

        var a = jQuery("<a href='#'></a>");
        a.appendTo(pageNav);
        a.data("data", page);

        var checkPageThumbnails = function () {
            if (page.greyLoaded && page.thumbLoaded) {
                msg(page.title + " image thumbnails loaded");

                if (isCollection) {
                    STATUS.Collections.thumbsLoaded++;
                } else {
                    STATUS.thumbsLoaded++;
                }

                var pp = 0;
                if (isCollection) {
                    STATUS.Collections.percent = parseInt(STATUS.Collections.thumbsLoaded / CLDATA.length) * THUMBNAILWEIGHT;
                    pp = STATUS.Collections.percent;
                } else {
                    pp = SECTIONWEIGHT + parseInt(STATUS.thumbsLoaded / DATA.length) * THUMBNAILWEIGHT;
                }
                loaderPercent.html(pp + "%");


                a.append(page.imgThumb);
                a.append(page.imgGrey);
                a.append("<span>" + page.title + "</span>");



                if (isCollection) {
                    a.click(collectionPageClicked);
                } else {
                    if (page.externalUrl) {
                        a.attr("href", page.externalUrl);
                    } else {
                        if (page.title.toLowerCase() == "news") {
                            a.click(newsClicked);
                        } else {
                            a.click(pageClicked);
                        }
                    }
                }

                page.link = a;
                if (isCollection) {

                } else {
                    IDHASH[page.id] = a;
                }

                if (isCollection) {
                    STATUS.Collections.thumbsLeft--;
                    if (0 == STATUS.Collections.thumbsLeft) {
                        msg("All collection thumbnails are loaded ");
                        setTimeout(loadMainCollectionImages, 50);
                        if (RM != RenderModes.Old) {
                            loaderText.animate({
                                "opacity": 0.5
                            });
                        }
                        loaderSection.html(TEXT["collections"]);
                    }
                } else {
                    STATUS.thumbsLeft--;
                    if (0 == STATUS.thumbsLeft) {
                        msg("All thumbnails are loaded ");
                        setTimeout(loadMainImages, 50);
                        if (RM != RenderModes.Old) {
                            loaderText.animate({
                                "opacity": 0.5
                            });
                        }
                        loaderSection.html(TEXT["pages"]);
                    }
                }

                // pageNav
                // setTimeout(function() {
                //    createThumbnail(page);
                // }, 50);

            }
        };

        var img = jQuery("<img />");
        img.load(function () {
            msg("Loaded " + page.thumbnail + "...");
            page.thumbLoaded = true;
            page.imgThumb = jQuery(this);
            checkPageThumbnails();
        });
        img.attr("src", page.thumbnail);

        var imgGrey = jQuery("<img />");
        imgGrey.load(function () {
            msg("Loaded " + page.thumbnailGreyscale + "...");
            page.greyLoaded = true;
            page.imgGrey = jQuery(this);
            checkPageThumbnails();
        });
        imgGrey.attr("src", page.thumbnailGreyscale);

    };

    //var createThumbnail = function(page) {
    //    STATUS.thumbsLeft--;
    //    msg("Creating images for " + page.title + "... " + STATUS.thumbsLeft);
    //
    //    if(0 == STATUS.thumbsLeft) {
    //        msg("All thumbnails are loaded ");
    //        setTimeout(loadMainImages, 50);
    //        loaderText.animate({
    //            "opacity" : 0.5
    //        });
    //        loaderSection.html(TEXT["pages"]);
    //    }
    //
    //};

    var loadMainImages = function () {

        var page = null;

        if (0 < STATUS.pagesLeft) {
            var index = DATA.length - STATUS.pagesLeft--;
            if (index < DATA.length) {
                msg("Loading main images for index: " + index + " / " + STATUS.pagesLeft + " / " + DATA.length);
                page = DATA[index];
            }
        }

        if (!page) {
            msg("Page queue empty");
            setTimeout(initializeComplete, 50);
            return;
        }

        msg("Loading main images for " + page + " " + page.title + "..." + page.images);
        loaderSection.html(TEXT["pages"] + " : " + page.title);

        if (page.image) {
            var img = jQuery("<img />");
            img.load(function () {
                var thzImg = jQuery(this);
                thzImg.removeAttr("width");
                thzImg.removeAttr("height");
                msg("Loaded main image " + page.title + "...");
                //page.imageSet.unshift(thzImg);
                //page.imageSetCount--;
                page.link.addClass("enabled");
                page.imgGrey.fadeOut(Timeouts.ThumbnailsOut);
                page.mainImage = thzImg;

                //if (0 == page.imageSetCount) {
                STATUS.pagesLoaded++;
                var pp = SECTIONWEIGHT + THUMBNAILWEIGHT + parseInt(STATUS.pagesLoaded / DATA.length) * PAGEWEIGHT;
                loaderPercent.html(pp + "%");
                msg("All main images downloaded for " + page.title + "...");
                //setTimeout(function() { postMainImageLoadActions(page); }, 50);
                setTimeout(loadMainImages, 50);
                //}
            });
            img.attr("src", page.image);
        } else {
            msg("Skipping: No images defined for " + page.title + "...");

            STATUS.pagesLoaded++;
            var pp = SECTIONWEIGHT + THUMBNAILWEIGHT + parseInt(STATUS.pagesLoaded / DATA.length) * PAGEWEIGHT;
            loaderPercent.html(pp + "%");

            page.link.addClass("enabled");
            page.imgGrey.fadeOut(Timeouts.ThumbnailsOut);
            setTimeout(loadMainImages, 50);
        }

    };

    var updateTextPosition = function () {

        //  var mh = mainText.height();
        //  var pos = mainText.offset();
        //  var x = pos.left;
        //  var sx = x;
        //  var sly = pos.top + mh;
        //  var sy = sly + subLinks.height() + 20;

        //  if (STATUS.linksShown) {
        //      var mw = mainText.width();
        //      var sw = subLinks.width();
        //      var dx = parseInt((mw - sw) / 2);
        //      sx += dx;
        //      // msg("mw: " + mw + " sw: " + sw + " dx: " + dx);
        //  }

        var mpos = mainImages.offset();
        var mposh = mainImages.height();
        var mposw = mainImages.width();
        var mposx = mpos.left + parseInt(mposw / 2);
        var mposy = mpos.top + parseInt(mposh / 2);

        var mw = mainText.width();
        var mh = mainText.height();
        var mx = mposx - parseInt(mw / 2);
        var my = mposy - parseInt(mh / 2);

        msg("page/sppage " + (CURRENTSPPAGE || CURRENTPAGE));


        if (CURRENTSPPAGE || CURRENTPAGE) {
            var p = SPMODE ? CURRENTSPPAGE : CURRENTPAGE;
            if (p) {
                mx += parseInt(p.imageTextX ? p.imageTextX : 0);
                my += parseInt(p.imageTextY ? p.imageTextY : 0);
                msg("spmode " + SPMODE);
                msg("px " + p.imageTextX);
                msg("py " + p.imageTextY);
            }
        }

        //var pos = mainText.offset();
        //var x = pos.left;
        var sw = subText.width();
        var sh = subText.height();
        var sx = mx + parseInt((mw - sw) / 2);
        var sy = my + mh;

        if (sh > 0) {
            sy += 20;
            //sly = sy + sh + 20;
        } else {
            //var dx = parseInt((mw - sw) / 2);
            //sx += dx;
        }

        var slw = subLinks.width();
        var slx = sx + parseInt((sw - slw) / 2);
        var sly = sy + sh + 20;

        STATUS.Text = {
            MainTextX: mx,
            MainTextY: my,
            SubTextX: sx,
            SubTextY: sy,
            SubLinksX: slx,
            SubLinksY: sly
        };

        //msg("mx: " + mx + "my: " + my + " mw: " + mw + " sw: " + sw + " sy: " + sy + " sly: " + sly + " sh: " + sh);

    };

    var adjustText = function (animate) {

        updateTextPosition();

        var subLinkTo = {
            "top": STATUS.Text.SubLinksY + "px",
            "left": STATUS.Text.SubLinksX + "px",
            "opacity": 1.0
        };

        var subTextTo = {
            "top": STATUS.Text.SubTextY + "px",
            "left": STATUS.Text.SubTextX + "px",
            "opacity": 1.0
        };

        var mainTextTo = {
            "top": STATUS.Text.MainTextY + "px",
            "left": STATUS.Text.MainTextX + "px",
            "opacity": 1.0
        };

        subLinks.css(subLinkTo);
        subText.css(subTextTo);
        mainText.css(mainTextTo);

    };

    var pageClicked = function () {

        if (SPTIMER) { clearTimeout(SPTIMER); }

        SPMODE = false;
        toggleLink(worldLink, true);


        var thz = jQuery(this);
        var page = thz.data("data");
        msg("Page clicked: " + page.title);

        //var tx = page.imageTextX ? page.imageTextX : "50";
        //var ty = page.imageTextY ? page.imageTextY : "25";
        //var ta = (page.imageTextX || page.imageTextY) ? "left" : "center";
        //
        //var stx = page.imageSubTextX ? page.imageSubTextX : "50";
        //var sty = page.imageSubTextY ? page.imageSubTextY : "35";
        //var sta = (page.imageSubTextX || page.imageSubTextY) ? "left" : "center";

        subText.animate({
            "opacity": 0//,
            //"left": "+=20"
        }, Timeouts.SubTextOut, "swing", function () {
            subLinks.animate({
                "opacity": 0//,
                //"left": "+=20"
            }, Timeouts.SubLinksOut, "swing", function () {
                subLinks.empty();
                subLinks.removeClass();
                STATUS.linksShown = false;
                STATUS.SUBTEXT = false;

                var i = 0;
                var count = page.subTitles.length;
                var maxCount = count;
                if (page.embedCode) { maxCount++; }
                if (page.websiteUrl) { maxCount++; }
                var lastIndex = maxCount - 1;

                msg("Count: " + count);

                for (i = 0; i < count; i++) {
                    msg("Sub titles for this page: " + page.subTitles[i]);
                    var subTitle = page.subTitles[i];
                    var subContent = page.subContent[i];
                    var subImage = page.subImages[i];

                    var a = jQuery("<a href='#'></a>");
                    a.html(subTitle);
                    a.data("title", subTitle);
                    a.data("content", subContent);
                    a.data("image", subImage);
                    a.css({
                        "color": page.textColor
                    });

                    a.click(subpageClicked);
                    subLinks.append(a);

                    //msg("i: " + i + " lastIndex: " + lastIndex + " count: " + count);
                    if (i < lastIndex) {
                        var b = jQuery("<b>/</b>");
                        b.css({
                            "color": page.textColor
                        });
                        subLinks.append(b);
                    }

                    if (0 == i && page.imageSubText) {
                        var readMoreLink = jQuery("<a href='#'></a>");
                        readMoreLink.html(page.readmoreText);
                        readMoreLink.click(function () {
                            subLinks.find("a").eq(0).trigger("click");
                        });
                        readMoreLink.addClass("read-more");
                        readMoreLink.appendTo(subText);
                        msg("Appending read more link...");
                    }

                    STATUS.linksShown = true;

                }

                if (page.embedCode) {
                    var a = jQuery("<a href='#'></a>");
                    a.html(page.embedLinkText);
                    a.data("code", page.embedCode);
                    a.css({
                        "color": page.textColor
                    });
                    a.click(videoClicked);
                    subLinks.append(a);

                    if (page.websiteUrl) {
                        var b = jQuery("<b>/</b>");
                        b.css({
                            "color": page.textColor
                        });
                        subLinks.append(b);
                    }
                }

                // $obj->websiteUrl = demonz_get_value($nd->field_pg_website_url, "url");
                // $obj->websiteText = demonz_get_value($nd->field_pg_website_text);
                // $obj->websiteWidth = demonz_get_value($nd->field_pg_website_width);
                // $obj->websiteHeight = demonz_get_value($nd->field_pg_website_height);

                if (page.websiteUrl) {
                    var a = jQuery("<a href='#'></a>");
                    a.html(page.websiteText);
                    a.data("url", page.websiteUrl);
                    a.data("width", page.websiteWidth);
                    a.data("height", page.websiteHeight);
                    a.css({
                        "color": page.textColor
                    });
                    a.click(websiteLinkClicked);
                    subLinks.append(a);
                }

                mainText.animate({
                    "opacity": 0//,
                    //"left": "-=20"
                }, Timeouts.MainTextOut, "swing", function () {

                    if (page.mainImage) {
                        page.mainImage.css("opacity", 0);
                        page.mainImage.appendTo(mainImages);
                        resizeImages();
                        page.mainImage.animate({ "opacity": 1 }, Timeouts.MainImageIn);
                    }

                    mainImages.css("background-color", page.backgroundColor ? page.backgroundColor : "#fff");

                    //mainText.html("<b>" + page.title + "</b>" + page.imageText);
                    mainText.html(page.imageText);
                    mainText.find("b").css("color", page.titleColor);
                    subText.html(page.imageSubText);

                    updateTextPosition();

                    mainText.css({
                        "left": STATUS.Text.MainTextX,
                        "top": STATUS.Text.MainTextY,
                        "color": page.textColor
                    });

                    //mainText.css({
                    //    "left": tx + "%",
                    //    "top": ty + "%",
                    //    "color" : page.textColor
                    //});


                    mainText.css("left", STATUS.Text.MainTextX);
                    mainText.animate({
                        //"left": STATUS.Text.MainTextX,
                        "opacity": 1.0
                    }, Timeouts.MainTextIn, "swing", function () {

                        STATUS.SUBTEXT = page.imageSubText && page.imageSubText.length > 0;


                        //updateTextPosition();
                        var subTextFrom = {
                            "top": STATUS.Text.SubTextY + "px",
                            "left": (STATUS.Text.SubTextX) + "px",
                            "opacity": 0.0,
                            "color": page.textColor
                        };
                        var subTextTo = {
                            //"top": STATUS.Text.SubTextY + "px",
                            //"left": STATUS.Text.SubTextX + "px",
                            "opacity": 1.0
                        };
                        subText.css(subTextFrom);
                        subText.animate(subTextTo, Timeouts.SubTextIn, "swing", function () {
                            var subLinkFrom = {
                                "top": STATUS.Text.SubLinksY + "px",
                                "left": (STATUS.Text.SubLinksX) + "px",
                                "opacity": 0.0
                            };
                            var subLinkTo = {
                                //"top": STATUS.Text.SubLinksY + "px",
                                //"left": STATUS.Text.SubLinksX + "px",
                                "opacity": 1.0
                            };
                            subLinks.css(subLinkFrom);
                            subLinks.animate(subLinkTo, Timeouts.SubLinksIn);
                        });

                        //var subLinkFrom = {
                        //   "top" : STATUS.Text.SubLinksY + "px",
                        //   "left": (STATUS.Text.SubLinksX-20) + "px",
                        //   "opacity": 0.0
                        //};
                        //var subLinkTo = {
                        //   "top" : STATUS.Text.SubLinksY + "px",
                        //   "left": STATUS.Text.SubLinksX + "px",
                        //   "opacity": 1.0
                        //};
                        //
                        //STATUS.SUBTEXT = false;
                        //
                        //subLinks.css(subLinkFrom);
                        //subLinks.animate(subLinkTo, 500, "swing", function() {
                        //    STATUS.SUBTEXT = page.imageSubText && page.imageSubText.length > 0;
                        //    subText.html(page.imageSubText);
                        //    adjustText();
                        //    var subTextFrom = {
                        //       "top" : STATUS.Text.SubTextY + "px",
                        //       "left" : (STATUS.Text.SubTextX-20) + "px",
                        //       "opacity" : 0.0,
                        //       "color" : page.textColor
                        //    };
                        //    var subTextTo = {
                        //       "top" : STATUS.Text.SubTextY + "px",
                        //       "left" : STATUS.Text.SubTextX + "px",
                        //       "opacity" : 1.0
                        //    };
                        //    subText.css(subTextFrom);
                        //    subText.animate(subTextTo);
                        //});

                    });
                });

            });

        });

        /*jQuery.each(page.imageSet, function() {
            //this.appendTo(mainImages);
            //msg("Image width: " + this.width() + " height: " + this.height());
            this.css("opacity", 0);
            this.appendTo(mainImages);
            this.animate({"opacity": 1}, 500);
            return false;
        });*/

        CURRENTPAGE = page;

        //adjustText();


        return false;
    };

    var subpageClicked = function () {

        cbMain.css("visibility", "visible");

        var thz = jQuery(this);
        msg("Sub page title clicked: " + thz.data("title"));

        cbMain.html(cbInnerHtml);
        cbImage = jQuery("#content-box-image");
        cbContent = jQuery("#content-box-content");

        cbContent.html(thz.data("content"));
        cbImage.css({
            "background-image": "url(" + thz.data("image") + ")"
        });

        jQuery.colorbox({
            inline: true,
            opacity: 0.5,
            href: "#content-box",
            top: "50px",
            onClosed: function () {
                cbMain.empty();
                cbMain.css("visibility", "hidden");
            }
        });

        jQuery("#content-box-content").slimScroll({
            height: cbContentHeight,
            width: 460,
            alwaysVisible: true,
            railVisible: true
        });

        jQuery("#content-box-close").click(function () {
            jQuery.colorbox.close();
            return false;
        });

        return false;

    };

    //  var nwsMain = jQuery("#news-box");
    //  var nwsImage = jQuery("#news-box-image");
    //  var nwsContent = jQuery("#news-box-content");

    var nwsIndex = 0;
    var nwsLinkArray = [];
    var newsLinkClicked = function () {
        var thz = jQuery(this);
        nwsIndex = thz.data("index");
        updateNews();
        return false;
    };

    var updateNewsLinks = function () {
        msg("nwsLinks " + nwsLinks.length);
        nwsLinks = jQuery('<div id="news-box-links" />');
        jQuery.each(NWSDATA, function (index, el) {
            var newsData = NWSDATA[index];
            var a = jQuery("<a href='#'>" + newsData.title + "</a>");

            msg("Updating news link" + index + " " + newsData.title);

            a.data("index", index);
            a.click(newsLinkClicked);
            a.appendTo(nwsLinks);
        });
    };

    var updateNews = function () {

        var newsData = NWSDATA[nwsIndex];
        msg("Update news " + newsData.title);
        nwsMain.html(nwsInnerHtml);
		//sleep(100);
        nwsImage = jQuery("#news-box-image");
        nwsContent = jQuery("#news-box-content");
        nwsContent.html(newsData.text);
		//sleep(100);
        nwsImage.css({
            "background-image": "url(" + newsData.image + ")"
        });

        nwsLinks = jQuery("#news-box-links"); // jQuery('<div id="news-box-links" />');
        jQuery.each(NWSDATA, function (index, el) {
            var newsData = NWSDATA[index];
            var a = jQuery("<a href='#'>" + newsData.title + "</a>");
            msg("Updating news link" + index + " " + newsData.title);
            a.data("index", index);
            a.click(newsLinkClicked);
            a.appendTo(nwsLinks);
        });

        nwsContent.slimScroll({
            height: nwsContentHeight,
            width: 460,
            alwaysVisible: true,
            railVisible: true
        });
        var closeLink = jQuery("#news-box-close");
        closeLink.click(function () {
            jQuery.colorbox.close();
            return false;
        });

        nwsLinks.insertBefore(closeLink);

        var prevLink = jQuery("#news-box-prev");
        var nextLink = jQuery("#news-box-next");

        prevLink.click(prevNews);
        nextLink.click(nextNews);
        if (nwsIndex < (NWSDATA.length - 1)) {
            nextLink.css("opacity", 1);
            nextLink.removeClass();
        } else {
            nextLink.css("opacity", 0.2);
            nextLink.addClass("disabled");
        }
        if (nwsIndex > 0) {
            prevLink.css("opacity", 1);
            prevLink.removeClass();
        } else {
            prevLink.css("opacity", 0.2);
            prevLink.addClass("disabled");
        }
    }

    var nextNews = function () {
        if (nwsIndex < (NWSDATA.length - 1)) {
            nwsIndex++;
            updateNews();
        }
        return false;
    };

    var prevNews = function () {
        if (nwsIndex > 0) {
            nwsIndex--;
            updateNews();
        }
        return false;
    };

    var newsClicked = function () {

        nwsMain.css("visibility", "visible");
		nwsMain.empty();
		
		sleep(100);

        jQuery.colorbox({
            inline: true,
            opacity: 0.5,
            href: "#news-box",
            //opacity:0,
            transition:"none",
            onClosed: function () {
                nwsMain.empty();
                nwsMain.css("visibility", "hidden");
            }
        });

        nwsIndex = 0;
        updateNews();
        return false;

    };


    var websiteLinkClicked = function () {
        var thz = jQuery(this);
        var w = thz.data("width");
        var h = thz.data("height");
        var url = thz.data("url");

        jQuery("#cboxClose").addClass("show");

        jQuery.colorbox({
            href: url,
            iframe: true,
            width: w,
            height: h,
            onClosed: function () {
                jQuery("#cboxClose").removeClass();
            }
        });
        return false;
    };

    var videoClicked = function () {

        videoBox.css("visibility", "visible");

        var thz = jQuery(this);
        msg("Video clicked: " + thz.data("code"));

        videoContent.html(thz.data("code"));

        jQuery.colorbox({
            inline: true,
            opacity: 0.5,
            href: "#video-box",
            onClosed: function () {
                videoBox.css("visibility", "hidden");
            }
        });

        return false;

    };



    jQuery("#video-box-close").click(function () {
        videoContent.empty();
        jQuery.colorbox.close();
        return false;
    });

    var updateContact = function () {

        var contactPrevLink = jQuery("#contact-box-prev");
        var nextLink = jQuery("#contact-box-next");

        if (currentContactPage < (maxContacts - 1)) {
            contactNextLink.css("opacity", 1);
            contactNextLink.removeClass();
        } else {
            contactNextLink.css("opacity", 0.2);
            contactNextLink.addClass("disabled");
        }

        if (currentContactPage > 0) {
            contactPrevLink.css("opacity", 1);
            contactPrevLink.removeClass();
        } else {
            contactPrevLink.css("opacity", 0.2);
            contactPrevLink.addClass("disabled");
        }

        contactLinks.removeClass("contact-selected");
        contactLinks.eq(maxContacts - currentContactPage - 1).addClass("contact-selected");

    };

    var nextContact = function () {
        if (currentContactPage < (maxContacts - 1)) {
            currentContactPage++;
            contactContent.animate({
                "margin-left": -currentContactPage * 990
            });
            updateContact();
        }
        return false;
    };

    var prevContact = function () {
        if (currentContactPage > 0) {
            currentContactPage--;
            contactContent.animate({
                "margin-left": -currentContactPage * 990
            });
            updateContact();
        }
        return false;
    };

    contactPrevLink.click(prevContact);
    contactNextLink.click(nextContact);
    contactCloseLink.click(function () {
        jQuery.colorbox.close();
        return false;
    });
    contactLinks.click(function() {
        //contactLinks.removeClass("contact-selected");
        var thz = jQuery(this);
        currentContactPage = thz.attr("rel");
        contactContent.animate({
            "margin-left": -currentContactPage * 990
        });
        updateContact();   
        //thz.addClass("contact-selected");
        return false;     
    });

    updateContact();

    // maxContacts
    // currentContactPage

    //jQuery("#link-micro-pg2").click(newsClicked);

    var locatorLinkClicked = function () {

        //var contactBox = jQuery("#contact-box");
        //var contactContent = jQuery("#contact-box-content");
        //<a href="#" id="contact-box-close"></a>
        //<a href="#" id="contact-box-prev"></a>
        //<a href="#" id="contact-box-next"></a>

        contactBox.css("visibility", "visible");

        jQuery.colorbox({
            inline: true,
            opacity: 0.5,
            href: "#contact-box",
            onClosed: function () {
                contactBox.css("visibility", "hidden");
            }
        });

    };

    locatorLink.click(locatorLinkClicked);

    if (false) {
        locatorLink.click(function () {
            jQuery("#contact-text").css("visibility", "visible");
            jQuery.colorbox({
                inline: true,
                opacity: 0.9,
                href: "#contact-text",
                onClosed: function () {
                    //ideoBox.empty();
                    //videoBox.css("visibility", "hidden");
                }
            });
            return false;
        });
    }

    // Main entry point after preloading
    var initializeComplete = function () {
        //loaderText.fadeOut();
        msg("INITIALIZE COMPLETE...");
        if (CLDATA) {
            setTimeout(processCollectionData, 50);
        }

        // Load Boutique Locator
        if (location.hash == "#boutique-locator") {
            locatorLink.trigger("click");
        }


    };

    // Collections

    var processCollectionData = function () {

        msg("Processing Collection Data...");

        STATUS.Collections = {};
        STATUS.Collections.thumbsLeft = CLDATA.length;
        STATUS.Collections.pagesLeft = CLDATA.length;
        STATUS.Collections.thumbsLoaded = 0;
        STATUS.Collections.pagesLoaded = 0;
        STATUS.Collections.percent = 0;
        STATUS.Collections.WrapperWidth = 0;

        COLLECTIONWEIGHT = COLLECTIONWEIGHT / CLDATA.length;
        msg("Collection Weight " + COLLECTIONWEIGHT);

        loaderSection.html(TEXT["collection-thumbnails"]);

        //var w = 128 * (DATA.length);
        //pageNav.width(w);
        //msg("page nav width " + w + " " + STATUS.thumbsLeft);
        var count = 0;
        jQuery.each(CLDATA, function () {
            var thz = this;
            msg(thz.title);
            thz.index = count++;
            loadThumbnailImages(thz, true);
        });

    };

    var adjustCollectionElements = function () {

        var ww = win.width();
        var wh = win.height();
        var h = wh - jQuery("#footer").outerHeight();

        var poffset = pageNav.offset();
        var arrowY = parseInt(h / 2 - (55 / 2));
        var arrowX = poffset.left + (CLDATA.length * 128);

        var logoW = collectionLogo.find("img").width();
        var logoH = collectionLogo.height();
        var logoY = parseInt(h / 2 - (logoH / 2));
        var logoX = arrowX - logoW - 80 - 10;

        if (ARROWSON) {
            arrowRight.css({
                "top": arrowY,
                "left": arrowX - 40
            });

            arrowLeft.css({
                "top": arrowY,
                "left": arrowX - 80
            });
        }

        collectionLogo.css({
            "top": logoY,
            "left": logoX
        });

        // msg("offset " + poffset.left + " logow " + logoW + " " + logoX);

        var linkWidth = logoW; // + 80 + 10;

        var linkY = logoY + logoH + 40;
        collectionActionLinks.css({
            "top": linkY,
            "left": logoX,
            "width": linkWidth
        });

        //exploreCollectionLink.css({
        //    "top" : linkY,
        //    "left" : logoX,
        //    "width" : linkWidth
        //});
        //
        //var exW = exploreCollectionLink.width();
        //
        //linkY += 25;
        //shopCollectionLink.css({
        //    "top" : linkY,
        //    "left" : logoX,
        //    "width" : linkWidth
        //});

        productArrowLeft.css({
            "top": arrowY
        });

        productArrowRight.css({
            "top": arrowY
        });

        var ph = h - 81 - 50;
        var pw = ph;

        pw = 400;
        ph = 400;

        var pm = 20;
        var pmT = 25;
        var spaceW = pm * 4;
        var elW = 30 + 30 + 10 + 10;

        //ww = 1000;

        if (ww > 1250) {
            spaceW = ww - elW - pw - pw;
            pm = parseInt(spaceW / 4);
        } else {
            //pw = parseInt((ww - elW - spaceW) / 2);
            //ph = pw;

            spaceW = ww - elW - pw - pw;
            pm = parseInt(spaceW / 4);

        }

        //pmT = parseInt((h - 81 - ph) / 2);
        pmT = parseInt((h - ph) / 2);

        //var fs = 16;
        //var lh = 14;
        //
        //if (ww < 1040) {
        //    fs = 14;
        //    lh = 16;
        //}
        //
        //if (ww < 800) {
        //    fs = 12;
        //    lh = 14;
        //}

        // msg("ww: " + ww + " font-size: " + fs + " line-height: " + lh);
        // msg("pw: " + pw + " ph: " + ph + " mm: " + pm);
        // msg("pmt: " + pmT);

        //jQuery("#product-data aside").css({
        //    "font-size" : fs + "px",
        //    "line-height" : lh + "px"
        //});

        var divs = productData.children("div");

        divs.css({
            "width": pw,
            "height": ph,
            "margin-right": pm,
            "margin-left": pm,
            "margin-top": pmT
        });

        if (CURRENTCOLLECTION) {
            if (!STATUS.Collections) {
                STATUS.Collections = {};
            }
            STATUS.Collections.WrapperWidth = ww - 80;
            productDataWrapper.width(STATUS.Collections.WrapperWidth);
            productDataWrapper.css("margin-left", 40);
            var pdM = -CURRENTCOLLECTION.productIndex * STATUS.Collections.WrapperWidth;
            productData.css({
                "margin-left": pdM
            });
        }

    };

    videoCollectionLink.click(videoClicked);

    exploreCollectionLink.click(function () {
        if (COLLECTIONMODE) {
            if (CURRENTCOLLECTION) {

                PRODUCTMODE = true;

                if (ARROWSON) {
                    arrowLeft.animate({
                        "opacity": 0
                    }, Timeouts.Arrows, "swing", function () {
                        arrowLeft.css("display", "none");
                    });
                    arrowRight.animate({
                        "opacity": 0
                    }, Timeouts.Arrows, "swing", function () {
                        arrowRight.css("display", "none");
                    });
                }
                collectionActionLinks.animate({
                    "opacity": 0
                }, Timeouts.CollectionLinks, "swing", function () {
                    collectionActionLinks.css("display", "none");
                    //exploreCollectionLink.css("display", "none");
                    //shopCollectionLink.css("display", "none");
                });



                //msg ("white arrows: " + CURRENTCOLLECTION.whiteArrows + " " + arrowRight.css("background-position") + " " + arrowLeft.css("background-position"));
                //exploreCollectionLink.animate({
                //    "opacity": 0
                //}, 150, "swing", function () {
                //    exploreCollectionLink.css("display", "none");
                //});
                //shopCollectionLink.animate({
                //    "opacity": 0
                //}, 150, "swing", function () {
                //    shopCollectionLink.css("display", "none");
                //});
                collectionLogo.animate({
                    "opacity": 0
                }, Timeouts.CollectionLogo, "swing", function () {
                    collectionLogo.css("display", "none");
                });
                productArrowLeft.css("display", "block");
                productArrowRight.css("display", "block");
                productArrowLeft.animate({
                    "opacity": 1
                }, Timeouts.Arrows);
                productArrowRight.animate({
                    "opacity": 1
                }, Timeouts.Arrows);

                //productImages

                //arrowLeft.animate({"opacity" : 0});
                //arrowRight.animate({"opacity" : 0});
                //productArrowLeft.animate({"opacity" : 1});
                //productArrowRight.animate({"opacity" : 1});
                //exploreCollectionLink.fadeOut();
                //collectionLogo.fadeOut();
                //
                //arrowLeft.fadeOut();
                //arrowRight.fadeOut();
                //
                //productArrowLeft.fadeIn();
                //productArrowRight.fadeIn();

                productData.empty();

                if (CURRENTCOLLECTION.text) {
                    var div = jQuery("<div />");
                    var aside = jQuery("<aside />");
                    aside.html("<img src='" + CURRENTCOLLECTION.logo + "' />" + CURRENTCOLLECTION.text);
                    aside.appendTo(div);
                    aside.css("color", CURRENTCOLLECTION.textColor);
                    div.appendTo(productData);



                }

                jQuery.each(CURRENTCOLLECTION.subImages, function () {
                    var div = jQuery("<div />");
                    this.appendTo(div);
                    div.appendTo(productData);
                });

                adjustCollectionElements();

                productImages.animate({
                    "opacity": 1
                }, Timeouts.ProductImages);

                resetProductArrows();
            }
        }
        return false;
    });

    //page.productIndex = 0;
    //page.productMax = 0;

    var resetProductArrows = function () {
        CURRENTCOLLECTION.productIndex = 0;
        productData.css({
            "margin-left": 0
        });
        changeProductArrowState();
    };

    var changeProductArrowState = function () {
        productArrowRight.stop();
        productArrowLeft.stop();
        if (COLLECTIONMODE && PRODUCTMODE && CURRENTCOLLECTION && CURRENTCOLLECTION.productIndex < (CURRENTCOLLECTION.productMax - 1)) {
            productArrowRight.animate({ opacity: 1 }, Timeouts.Arrows);
        } else {
            productArrowRight.animate({ opacity: 0.1 }, Timeouts.Arrows);
        }
        if (COLLECTIONMODE && PRODUCTMODE && CURRENTCOLLECTION && CURRENTCOLLECTION.productIndex > 0) {
            productArrowLeft.animate({ opacity: 1 }, Timeouts.Arrows);
        } else {
            productArrowLeft.animate({ opacity: 0.1 }, Timeouts.Arrows);
        }
    };

    productArrowRight.click(function () {
        msg("right arrow clicked");
        if (COLLECTIONMODE && PRODUCTMODE && CURRENTCOLLECTION && CURRENTCOLLECTION.productIndex < (CURRENTCOLLECTION.productMax - 1)) {
            msg("move right by " + STATUS.Collections.WrapperWidth + " / " + CURRENTCOLLECTION.productIndex);
            CURRENTCOLLECTION.productIndex++;
            //productData.animate({
            //    "margin-left" : "-=" + STATUS.Collections.WrapperWidth
            //});
            productData.animate({
                "margin-left": "-" + (CURRENTCOLLECTION.productIndex * STATUS.Collections.WrapperWidth)
            });
            changeProductArrowState();
        }
        return false;
    });

    productArrowLeft.click(function () {
        msg("right arrow clicked");
        if (COLLECTIONMODE && PRODUCTMODE && CURRENTCOLLECTION && CURRENTCOLLECTION.productIndex > 0) {
            CURRENTCOLLECTION.productIndex--;
            msg("move left by " + STATUS.Collections.WrapperWidth + " / " + CURRENTCOLLECTION.productIndex);
            productData.animate({
                "margin-left": "-" + (CURRENTCOLLECTION.productIndex * STATUS.Collections.WrapperWidth)
            });
            //productData.animate({
            //    "margin-left": "+=" + STATUS.Collections.WrapperWidth
            //});
            changeProductArrowState();
        }
        return false;
    });

    arrowLeft.click(function () {
        if (COLLECTIONMODE) {
            if (CURRENTCOLLECTION) {
                var x = (CURRENTCOLLECTION.index - 1);
                if (x < 0) { x = CLDATA.length - 1; }
                CLDATA[x].link.trigger("click");
            }
        }
        return false;
    });

    arrowRight.click(function () {
        if (COLLECTIONMODE) {
            if (CURRENTCOLLECTION) {
                var x = (CURRENTCOLLECTION.index + 1);
                if (x >= CLDATA.length) { x = 0; }
                CLDATA[x].link.trigger("click");
            }
        }
        return false;
    });

    var collectionPageClicked = function () {

        if (PRODUCTMODE || !COLLECTIONMODE) {
            if (ARROWSON) {
                arrowLeft.css("display", "block");
                arrowRight.css("display", "block");
            }
            //exploreCollectionLink.css("display", "block");
            //shopCollectionLink.css("display", "block");
            collectionActionLinks.css("display", "block");
            collectionLogo.css("display", "block");

            if (ARROWSON) {
                arrowLeft.animate({
                    "opacity": 1
                }, Timeouts.Arrows);
                arrowRight.animate({
                    "opacity": 1
                }, Timeouts.Arrows);
            }

            collectionActionLinks.animate({
                "opacity": 0
            }, Timeouts.CollectionLinks);
            //exploreCollectionLink.animate({
            //    "opacity": 0
            //});
            //shopCollectionLink.animate({
            //    "opacity": 0
            //});
            collectionLogo.animate({
                "opacity": 0
            }, Timeouts.CollectionLogo);
            productArrowLeft.animate({
                "opacity": 0
            }, Timeouts.Arrows, "swing", function () {
                productArrowLeft.css("display", "none");
            });
            productArrowRight.animate({
                "opacity": 0
            }, Timeouts.Arrows, "swing", function () {
                productArrowRight.css("display", "none");
            });
            //productArrowLeft.fadeOut();
            //productArrowRight.fadeOut();

            productImages.animate({
                "opacity": 0
            }, Timeouts.ProductImages);
        }

        COLLECTIONMODE = true;
        PRODUCTMODE = false;

        var thz = jQuery(this);
        var page = thz.data("data");
        if (!page) { return; }

        collectionLogo.animate({
            "opacity": 0,
            "right": 120
        }, Timeouts.CollectionLogo, "swing", function () {
            collectionActionLinks.animate({
                "opacity": 0,
                "right": 120
            }, Timeouts.CollectionLinks, "swing", function () {

                collectionLogo.find("img").detach();

                if (page.logoImage) {
                    page.logoImage.appendTo(collectionLogo);
                }

                exploreCollectionLink.html(page.exploreText ? page.exploreText : "Explore");
                shopCollectionLink.html(page.shopText ? page.shopText : "Shop");
                shopCollectionLink.attr("href", page.shopUrl);
                collectionActions.css("color", page.textColor);

                if (page.catalogUrl) {
                    catalogCollectionLink.attr("href", page.catalogUrl);
                    catalogCollectionLink.html(page.catalogText);
                    catalogCollectionLink.appendTo(collectionActionLinks);
                } else {
                    catalogCollectionLink.html("");
                    catalogCollectionLink.detach();
                }

                if (page.embedCode) {
                    videoCollectionLink.data("code", page.embedCode);
                    videoCollectionLink.html(page.embedLinkText);
                    videoCollectionLink.appendTo(collectionActionLinks);
                } else {
                    videoCollectionLink.html("");
                    videoCollectionLink.data("code", "");
                    videoCollectionLink.detach();
                }

                //msg ("white arrows" + CURRENTCOLLECTION.whiteArrows);
                var whiteArrows = (CURRENTCOLLECTION && CURRENTCOLLECTION.whiteArrows) || false;

                if (ARROWSON) {
                    arrowRight.css("background-position", "1" == whiteArrows ? "0px -110px" : "0px 0px");
                    arrowLeft.css("background-position", "1" == whiteArrows ? "0px -165px" : "0px -55px");
                }

                resizeImages();
                adjustCollectionElements();

                var bg = page.backgroundColor ? page.backgroundColor : "#fff";
                productImages.css({
                    "opacity": 0,
                    "background-color": bg
                });

                if (page.mainImage) {
                    page.mainImage.css("opacity", 0);
                    page.mainImage.appendTo(mainImages);
                    page.mainImage.animate({
                        "opacity": 1
                    }, Timeouts.MainImageIn, "swing", function () {
                        collectionLogo.animate({
                            "opacity": 1,
                            "right": 80
                        }, Timeouts.CollectionLogo, "swing", function () {
                            collectionActionLinks.animate({
                                "opacity": 1,
                                "right": 80
                            }, Timeouts.CollectionLinks, "swing", function () {
                            });
                        });
                    });
                }
            });
        });

        CURRENTCOLLECTION = page;

        //var collectionsLink = jQuery("#link-collections");
        //var arrowLeft = jQuery("#arrow-left");
        //var arrowRight = jQuery("#arrow-right");
        //var exploreCollectionLink = jQuery("#explore-collection-link");
        //var collectionLogo = jQuery("#collection-logo");
        //collectionLogo.animate({"opacity": 1}, 250);
        //exploreCollectionLink.animate({"opacity": 1}, 250);


    };

    var loadMainCollectionImages = function () {

        var page = null;

        if (0 < STATUS.Collections.pagesLeft) {
            var index = CLDATA.length - STATUS.Collections.pagesLeft--;
            if (index < CLDATA.length) {
                msg("Loading main images for collection index: " + index + " / " + STATUS.Collections.pagesLeft + " / " + CLDATA.length);
                page = CLDATA[index];
            }
        }

        if (!page) {
            msg("Collection page queue empty");
            setTimeout(collectionsInitialized, 50);
            return;
        }

        var ppMainImageIncrement = 0.5 * COLLECTIONWEIGHT;
        var ppLogoIncrement = 0.1 * COLLECTIONWEIGHT;
        var ppSubImageIncrement = (0.4 / page.imageCount) * COLLECTIONWEIGHT;
        var ppTotal = 0;

        msg("Loading main images for collection " + page + " " + page.title + "..." + page.images);
        loaderSection.html(TEXT["collections"] + " : " + page.title);

        var mainImageLoaded = false;
        var logoLoaded = false;
        var subImagesLoaded = false;
        var checkComplete = function (forced) {
            if (forced || (mainImageLoaded && logoLoaded && subImagesLoaded)) {
                STATUS.Collections.pagesLoaded++;
                page.link.addClass("enabled");
                //page.page.imgThumb.fadeIn(1500);
                if (forced) {
                    msg("Forced to trigger completion for collection " + page.title + "...");
                } else {
                    msg("All main images downloaded for collection " + page.title + "...");
                }
                setTimeout(loadMainCollectionImages, 50);
            }
        };

        var skip = false;
        if (!skip && page.image) {
            var img = jQuery("<img />");
            img.load(function () {
                var thzImg = jQuery(this);
                thzImg.removeAttr("width");
                thzImg.removeAttr("height");
                msg("Loaded main collection image " + page.title + "...");
                mainImageLoaded = true;
                page.mainImage = thzImg;

                STATUS.Collections.percent += ppMainImageIncrement;
                loaderPercent.html(parseInt(Math.round(STATUS.Collections.percent)) + "%");
                msg("%" + STATUS.Collections.percent);

                checkComplete();
            });
            img.attr("src", page.image);
        } else {
            skip = true;
        }

        if (!skip && page.logo) {
            var img = jQuery("<img />");
            img.load(function () {
                var thzImg = jQuery(this);
                thzImg.removeAttr("width");
                thzImg.removeAttr("height");
                msg("Loaded main collection logo " + page.title + "...");
                logoLoaded = true;
                page.logoImage = thzImg;

                STATUS.Collections.percent += ppLogoIncrement;
                loaderPercent.html(parseInt(Math.round(STATUS.Collections.percent)) + "%");
                msg("%" + STATUS.Collections.percent);

                checkComplete();
            });
            img.attr("src", page.logo);
        } else {
            skip = true;
        }

        page.productIndex = 0;
        page.productMax = 0;

        if (!skip && 0 < page.imageCount) {
            page.productMax = parseInt(Math.ceil(page.imageCount + (page.text ? 1 : 0)) / 2);
            page.subImageCount = 0;
            page.subImages = [];
            jQuery.each(page.images, function (index, el) {
                var img = jQuery("<img />");
                img.load(function () {
                    page.subImageCount++;
                    var thzImg = jQuery(this);
                    thzImg.removeAttr("width");
                    thzImg.removeAttr("height");
                    page.subImages[index] = thzImg;
                    msg("Loaded main collection sub image " + page.title + "...");

                    STATUS.Collections.percent += ppSubImageIncrement;
                    loaderPercent.html(parseInt(Math.round(STATUS.Collections.percent)) + "%");
                    msg("%" + STATUS.Collections.percent);

                    if (page.subImageCount == page.images.length) {
                        subImagesLoaded = true;
                        checkComplete();
                    }
                });
                msg("Loading main collection sub image " + this + "...");
                img.attr("src", this);
            });
        } else {
            skip = true;
        }



        //if (false) {
        //    msg("Skipping: No images defined for collection " + page.title + "...");
        //
        //    STATUS.Collections.pagesLoaded++;
        //    var pp = 55; // SECTIONWEIGHT + THUMBNAILWEIGHT + parseInt(STATUS.pagesLoaded/DATA.length) * PAGEWEIGHT;
        //    loaderPercent.html(pp + "%");
        //
        //    page.link.addClass("enabled");
        //    page.imgGrey.fadeOut(1500);
        //    setTimeout(loadMainCollectionImages, 50);
        //}


    };

    var collectionsInitialized = function () {
        msg("Collections initialized");
        if (RM != RenderModes.Old) {
            loaderText.animate({
                "opacity": 0
            });
        } else {
            loaderText.hide(); // animate({opacity: 0 });
        }

    };

    /*

    var hideNextLink = function() {
        msg("Hiding " + STATUS.tmpIndex);
        if (STATUS.tmpIndex < DATA.length) {
            DATA[STATUS.tmpIndex].link.animate({
                "opacity": 0
            }, 100, "swing", function() {
                STATUS.tmpIndex++;
                setTimeout(hideNextLink, 10);
            });
        } else {
            jQuery.each(DATA, function() {
                var thz = this;
                thz.link.detach();
            });
        }
    };

    */


    collectionsLink.click(function () {

        msg("Collection clicked" + COLLECTIONMODE);
        if (STATUS && STATUS.Collections && STATUS.Collections.pagesLeft && STATUS.Collections.pagesLeft != 0) {
            msg("Collection pages not loaded... skippping" + COLLECTIONMODE);
            return;
        }

        if (COLLECTIONMODE) { return; }
        if (SPTIMER) { clearTimeout(SPTIMER); }

        toggleLink(collectionsLink, false);
        toggleLink(worldLink, true);
        //COLLECTIONMODE = true;
        PRODUCTMODE = false;

        mainSet.css("z-index", "auto");

        subText.stop();
        subLinks.stop();
        mainText.stop();

        subText.animate({
            "opacity": 0//,
            //"left": "+=20"
        }, Timeouts.SubTextOut, "swing", function () {
            subLinks.animate({
                "opacity": 0//,
                //"left": "+=20"
            }, Timeouts.SubLinksOut, "swing", function () {
                mainText.animate({
                    "opacity": 0//,
                    //"left": "-=20"
                }, Timeouts.MainTextOut);
            });
        });

        pageNav.animate({
            "opacity": 0
        }, Timeouts.PageNavOut, "swing", function () {
            jQuery.each(DATA, function () {
                var thz = this;
                thz.link.detach();
            });
            jQuery.each(CLDATA, function () {
                var thz = this;
                thz.link.appendTo(pageNav);
            });
            var w = 128 * (CLDATA.length);
            pageNav.width(w);
            pageNav.animate({
                "opacity": 1
            });

            adjustCollectionElements();

            if (ARROWSON) {
                arrowLeft.css("display", "block");
                arrowRight.css("display", "block");
                arrowLeft.animate({
                    "opacity": 1
                }, Timeouts.Arrows);
                arrowRight.animate({
                    "opacity": 1
                }, Timeouts.Arrows);
            }

            if (CLDATA && 0 < CLDATA.length) {
                CLDATA[0].link.trigger("click");
            }
        });
        return false;
    });

    /// Footer
    //var footer = jQuery("#footer");
    //var footerLinks = jQuery("#links-mini a,#links-micro a,#links-nano a");
    var deviceAgent = navigator.userAgent.toLowerCase();
    var iOS = deviceAgent.match(/(iphone|ipod|ipad)/);
    var turnOnFooterEffects =   !iOS;

    //console.log(deviceAgent);

    if (turnOnFooterEffects) {
        var pageNavTimer = null;
        var hidePageNav = function () {
            //pageNavContainer.animate({"bottom": 49});
            var pb = footer.outerHeight() - pageNavContainer.height();
            pageNavContainer.animate({ "bottom": pb, "opacity": 0 }, Timeouts.PageNavContainer);
        };
        footer.mouseenter(function () {
            if (pageNavTimer) { clearTimeout(pageNavTimer); }
            //ooterLinks.animate({
            //   "opacity": 1
            //, Timeouts.FooterLinksIn);
            //pageNavContainer.animate({"bottom": 129});
            var pb = footer.outerHeight();
            pageNavContainer.animate({ "bottom": pb, "opacity": 1 }, Timeouts.PageNavContainer);
            return false;
        });
        footer.mouseleave(function () {
            //footerLinks.animate({
            //    "opacity": 0
            //}, Timeouts.FooterLinksOut);
            if (pageNavTimer) { clearTimeout(pageNavTimer); }
            pageNavTimer = setTimeout(hidePageNav, 100);
            return false;
        });
        pageNavContainer.mouseenter(function () {
            if (pageNavTimer) { clearTimeout(pageNavTimer); }
            var pb = footer.outerHeight();
            pageNavContainer.animate({ "bottom": pb, "opacity": 1 }, Timeouts.PageNavContainer);
            return false;
        });
        pageNavContainer.mouseleave(function () {
            if (pageNavTimer) { clearTimeout(pageNavTimer); }
            pageNavTimer = setTimeout(hidePageNav, 100);
            return false;
        });
        pageNavTimer = setTimeout(hidePageNav, 500);
    } else {

    }

    var orientationChanged = function() {
        footerLinks.css("opacity", 1);
        win.trigger("resize");
        var pb = footer.outerHeight();
        pageNavContainer.css({ "bottom": pb + "px", "opacity" : 1});
        window.pageXOffset = 0;
        window.pageYOffset = 0;
        window.scrollTo(0,0);
    };

    if (iOS) {
        document.body.onorientationchange = orientationChanged;
        orientationChanged();
    }

    //document.location.hash = "TIMEZ " + ((new Date()).getMilliseconds());
    /// END


	mainText.html("   &nbsp;             ");
	updateTextPosition();
	mainText.css({
		"left": STATUS.Text.MainTextX,
		"top": STATUS.Text.MainTextY
	});


    // Begin preloading<IMG alt="" src="/paspaley/sites/default/themes/demonz/images/logo-small.png">
    if (loaderImageUrl) {
        var img = jQuery("<img />");
        img.load(function () {
            msg("Loader image loaded...");
            jQuery("#mask").css("background-url", "url(" + loaderImageUrl + ")");
            if (SPDATA) {
                loaderSection.html(TEXT["slideshow"]);
                setTimeout(processSpData, 50);
            }
        });
        img.attr("src", loaderImageUrl);
        //setTimeout(updateNewsLinks, 50);
    }

    //if (SPDATA) {
    //    setTimeout(processSpData, 50);
    //}





});
