$(document).ready(function () {

    /* SCRIPTS FOR OUTFITS PAGE */

    // Make big image draggable
    originaloffset = $("#outfit-large").offset();

    $("#outfit-large-image").load(function () {

        $("#outfit-large-image").hover(function () {
            $(document).mousemove(function (e) {
                $('#outfit-large-image-wrap').css({ marginTop: e.pageY, marginLeft: e.pageX });

            });

        });

        /*	$("#outfit-large-image").css({cursor: "move"});
        imgwidth = $(this).attr('width');
        imgheight = $(this).attr('height');
	
        a = originaloffset.left - (imgwidth - 690);
        b = originaloffset.top - (imgheight - 509);
        c = originaloffset.left;
        d = originaloffset.top;

        $("#outfit-large-image").draggable( {
        helper: function(){ return $("#outfit-large-image").clone()[0]; },
        containment: [a, b, c, d],
        drag: function(event, ui) {
        $("#outfit-large-image").css({display: "none"});
        },
        stop: function(event, ui) {
        var finalposition = $(ui.helper).offset();
        $("#outfit-large-image-wrap").offset(finalposition);
        $("#outfit-large-image").css({display: "inline"});
        }
        }); */
    });

    // Actions when an outfit thumbnail is clicked
    $(".outfit-thumbs li a").click(function () {

        // Ascertain outfit ID from link
        var outfitlink = $(this).attr('href');
        // Change this pattern to match your links
        var outfitid = outfitlink.substr(22);

        // Hide 'get the look' menu
        $('.outfit-menu').animate({
            height: ['toggle', 'swing']

        }, 100, function () {

            // Switch medium image (right)
            $('#outfit-medium').animate({
                width: ['toggle', 'swing']
            }, 200, function () {

                // Clear old image & any scroll position
                $(this).html('');
                $("#outfit-large-image-wrap").css({ top: 0, left: 0 });
                // Set new image source
                newimagesource = 'images/dan-images/outfit-medium' + outfitid + '.jpg';

                newimage = new Image();
                $(newimage).load(function () {

                    $('#outfit-medium').append(this);

                    $('#outfit-medium').animate({
                        width: ['toggle', 'swing']
                    }, 400, function () { });

                }).error(function () {
                    // notify the user that the image could not be loaded
                }).attr('src', newimagesource);
            });

            // Switch large image (left), and change the 'get the look menu contents
            $('#outfit-large').animate({
                width: ['toggle', 'swing']
            }, 300, function () {

                // Clear old image
                $("#outfit-large-image-wrap").html('');
                // Set new image source
                newimagesource = 'images/dan-images/outfit-large' + outfitid + '.jpg';

                newimage = new Image();
                $(newimage).load(function () {

                    $('#outfit-large-image-wrap').append(this);

                    $("#outfit-large-image").css({ cursor: "move" });

                    imgwidth = $(this).attr('width');
                    imgheight = $(this).attr('height');
                    e = originaloffset.left - (imgwidth - 690);
                    f = originaloffset.top - (imgheight - 509);
                    g = originaloffset.left;
                    h = originaloffset.top;

                    $("#outfit-large-image").draggable({
                        helper: function () { return $("#outfit-large-image").clone()[0]; },
                        containment: [e, f, g, h],
                        drag: function (event, ui) {
                            $("#outfit-large-image").css({ display: "none" });
                        },
                        stop: function (event, ui) {
                            var finalposition = $(ui.helper).offset();
                            $("#outfit-large-image-wrap").offset(finalposition);
                            $("#outfit-large-image").css({ display: "inline" });
                        }
                    });


                    $('#outfit-large').animate({
                        width: ['toggle', 'swing']
                    }, 200, function () {

                        // Load the 'get the look' panel from a file.
                        // The 'outfitid' from above will presumably be 
                        // inserted into the path here to specify the file to load.
                        $("#getthelook-panel").html('');
                        $("#getthelook-panel").load("includes/dan-exampleproducts.html", function () {

                            // Fire up Cufon on the replaced text
                            Cufon('.outfit-menu');
                        });

                        // Bring back the outfit-menu and the panel 
                        $('.outfit-menu').animate({
                            height: ['toggle', 'swing']
                        }, 300, function () {

                        });
                    });



                }).attr({ src: newimagesource, id: 'outfit-large-image' });



            });
        });
        return false;
    });

    // 'Get the look' section toggle
    $("#getthelook-toggle").toggle(
			function () {

			    $('#getthelook-panel').animate({
			        height: "370px"
			    }, 300)

			    $('#getthelook-toggle').css('background', 'url(images/dan-images/arrowdown.gif) 1px 3px no-repeat');

			},
	                function () {

	                    $('#getthelook-panel').animate({
	                        height: "0px"
	                    }, 300)

	                    $('#getthelook-toggle').css('background', 'url(images/dan-images/arrow.gif) 0 3px no-repeat');

	                }
		);


    // Thumbnail controls
    var numberofthumbs = $(".outfit-thumbs ul li a img").size();
    var position = 0;

    // Show next thumbnail
    $('#next-thumbs a').click(function () {

        if (position == 0) {
            $('#previous-thumbs a').animate({
                opacity: "1"
            }, 150);
        }
        if (position < numberofthumbs - 10) {
            position++;
            if (position == numberofthumbs - 10) {
                $('#next-thumbs a').animate({
                    opacity: ".2"
                }, 150);
            }
            $('.outfit-thumbs ul').animate({
                marginLeft: '-=90px'
            }, 150);
        }
        return false;
    });

    // Show previous thumbnail
    $('#previous-thumbs a').click(function () {

        if (position > 0) {
            position--;
            if (position == 0) {
                $('#previous-thumbs a').animate({
                    opacity: ".2"
                }, 150);
            }
            $('.outfit-thumbs ul').animate({
                marginLeft: '+=90px'
            }, 150);
        }
        if (position == numberofthumbs - 11) {
            $('#next-thumbs a').animate({
                opacity: "1"
            }, 150);
        }
        return false;
    });


    /* SCRIPTS FOR ROOM SETS PAGE */

    // Do actions on click roomset links
    // Dynamic function that supports arbitrary room sets could easily 
    // be made using digit in 'roomsetlink' to find marginTop values

    $("#roomsetlink1").click(function () {

        // Switch image
        $('#roomsets-images div').animate({
            marginTop: '0px'
        }, 600, 'swing')

        // Switch background highlight
        $("#roomsets-menu li").css('backgroundColor', 'transparent');
        $(this).css('backgroundColor', '#fff');

        // Switch items shown
        $("#roomsets-items .roomsets-itemcollection").animate({
            top: '0px'
        }, 0);
    });

    $("#roomsetlink2").click(function () {

        $('#roomsets-images div').animate({
            marginTop: '-343px'
        }, 600, 'swing')

        $("#roomsets-menu li").css('backgroundColor', 'transparent');
        $(this).css('backgroundColor', '#fff');

        $("#roomsets-items .roomsets-itemcollection").animate({
            top: '-375px'
        }, 0);
    });

    $("#roomsetlink3").click(function () {

        $('#roomsets-images div').animate({
            marginTop: '-686px'
        }, 600, 'swing')

        $("#roomsets-menu li").css('backgroundColor', 'transparent');
        $(this).css('backgroundColor', '#fff');

        $("#roomsets-items .roomsets-itemcollection").animate({
            top: '-750px'
        }, 0);
    });


    /* SCRIPTS FOR COLLECTIONS PAGE */

    // 'View' section toggle
    $('#view-panel').toggle();
    $("#view-toggle").toggle(
		function () {

		    $('#view-panel').slideDown();


		    $('#view-toggle').css('background', 'url(/images/dan-images/arrowdown.gif) 1px 3px no-repeat');

		},
	               function () {

	                   $('#view-panel').slideUp();
	                   $('#view-toggle').css('background', 'url(/images/dan-images/arrow.gif) 0 3px no-repeat');

	               }
	);


    /* VIDEO STUFF */

    // separate video in shadowbox
    $("a.videolink").click(function () {

        var videopage = $(this).attr('href');

        Shadowbox.open({
            content: videopage,
            player: "iframe",
            title: "",
            height: 352,
            width: 870,

            options: {
                onClose: function () { }
            }
        });

        return false;

    });





    /* SCRIPTS FOR CT5d PAGE */
    if ($('.ct5dcontainer').length) {
        currPhoto = 1;
        numPhotos = $(".ct5dcontainer #ct5dimages .ct5dmagazinepage").size();
        $(".ct5dcontainer #ct5dimages").width(numPhotos * 940);

        $(".ct5dcontainer a#forward").click(function () {

            if (currPhoto < numPhotos) {

                currPhoto++;

                $(".ct5dcontainer #ct5dimages").stop(true, true).animate({
                    marginLeft: '-=940'
                }, 700, "easeOutCubic", function () {

                    if (currPhoto > 0) {
                        $(".ct5dcontainer a#back").animate({ opacity: 1 }, 300);
                    }

                    if (currPhoto == numPhotos) {
                        $(".ct5dcontainer a#forward").animate({ opacity: 0 }, 300);
                    }
                });
            }
            return false;

        });

        $(".ct5dcontainer a#back").click(function () {

            if (currPhoto > 1) {

                currPhoto--;

                $(".ct5dcontainer #ct5dimages").stop(true, true).animate({
                    marginLeft: '+=940'
                }, 700, "easeOutCubic", function () {

                    if (currPhoto == 1) {
                        $(".ct5dcontainer a#back").animate({ opacity: 0 }, 300);
                    }

                    if (currPhoto < (numPhotos)) {
                        $(".ct5dcontainer a#forward").animate({ opacity: 1 }, 300);
                    }
                });
            }
            return false;
        });
    }



    /* SCRIPTS FOR CT5b PAGE */
    if ($('.ct5bcontainer').length) {
        currPhoto = 1;
        numPhotos = $(".ct5bcontainer #ct5bimages .ct5bmagazinepage").size();

        $(".ct5bcontainer #ct5bimages").width(numPhotos * 940);

        $(".ct5bcontainer a#forward").click(function () {

            if (currPhoto < numPhotos) {

                currPhoto++;

                $(".ct5bcontainer #ct5bimages").stop(true, true).animate({
                    marginLeft: '-=720'
                }, 700, "easeOutCubic", function () {

                    if (currPhoto > 0) {
                        $(".ct5bcontainer a#back").animate({ opacity: 1 }, 300);
                    }

                    if (currPhoto == numPhotos) {
                        $(".ct5bcontainer a#forward").animate({ opacity: 0 }, 300);
                    }
                });
            }
            return false;

        });

        $(".ct5bcontainer a#back").click(function () {

            if (currPhoto > 1) {

                currPhoto--;

                $(".ct5bcontainer #ct5bimages").stop(true, true).animate({
                    marginLeft: '+=720'
                }, 700, "easeOutCubic", function () {

                    if (currPhoto == 1) {
                        $(".ct5bcontainer a#back").animate({ opacity: 0 }, 300);
                    }

                    if (currPhoto < (numPhotos)) {
                        $(".ct5bcontainer a#forward").animate({ opacity: 1 }, 300);
                    }
                });
            }
            return false;
        });
    }


    /* SCRIPTS FOR CT5 PAGE */

    // Change height of main div on load & resize so aspect is correct
    // (correct widths &heights of inner boxes are in CSS
    $(window).load(function () {

        boxHeights = new Array(".34", ".21", ".13", ".13", ".21", ".21", ".21", ".21");
        boxTopMargins = new Array("0", "0", ".21", ".21", ".34", ".34", ".34", ".34");
        $(".homepage-image").height($(window).height() - 72);
        $(".ct5container").height($(window).width() * .56);
        $('.ct5container ul li').each(function (index) {
            $(this).height(boxHeights[index] * $(window).width());
            $(this).css('marginTop', boxTopMargins[index] * $(window).width());
        });

        $(".ct5container ul li a").hover(function () {
            $(this).children('img').eq(1).fadeOut(1000);
        }, function () {
            $(this).children('img').eq(1).stop(true, true).fadeIn(1500);
        });

        // Vertically center main content
        var winheight = $(document).height();
        var divheight = $(window).width() * .56;
        var margintop = ((winheight - divheight) / 2) - 65;
        $(".ct5containerouter").css('marginTop', margintop);

    });

    $(window).resize(function () {
        $(".homepage-image").height($(window).height() - 72);
        $(".ct5container").height($(window).width() * .56);
        $('.ct5container ul li').each(function (index) {
            $(this).height(boxHeights[index] * $(window).width());
            $(this).css('marginTop', boxTopMargins[index] * $(window).width());
        });

        // Vertically center main content
        var winheight = $(document).height();
        var divheight = $(window).width() * .56;
        var margintop = ((winheight - divheight) / 2) - 65;
        $(".ct5containerouter").css('marginTop', margintop);

    });




    /* SCRIPTS FOR H PAGE */
    $(window).load(function () {

        // Start background images cycle
        $('#hcontainer').cycle({
            fx: 'fade',
            before: function () {
                //$(this).css({height: '100%;'});
                $("#hcontainer > a").removeClass('selected');
                $(this).addClass('selected');
                $(this).css({ marginLeft: ($(window).width() - $(this).width()) / 2 });
                //console.log('marginleft (cycle) to :' + ($(window).width() - $(this).width()) / 2);
            },
            delay: 1500,
            speed: 1800,
            timeout: 6000
        });

        // Fade logo overlay
        //$("#hoverlay").delay(1000).fadeOut(1000, "easeInCubic", function(){

        // Briefly show thumbnail bar then hide
        $("#hthumbs").animate({ height: '215' }, 1200, "easeOutCubic").delay(2500).animate({ height: '0' }, 1200, "easeOutCubic", function () {
            $("#hthumbs").animate({ backgroundPositionY: '0' }, 100);
            $("#hthumbs").css({ backgroundPosition: 'right 0' });
        });
        //});
    });

    $(window).resize(function () {
        var current = $("#hcontainer > a.selected");

        $(current).css({ marginLeft: ($(window).width() - $(current).width()) / 2 });
        //console.log('marginleft (resize) to :' + ($(window).width() - $(current).width()) / 2);
    });


    // Allow thumbnail bar to be toggled
    $("#hthumbs").hover(function () {

        // Pause bg cycle (otherwise cycle animation kills thumb 
        // bar animation performance! It does a bit anyway...)
        $('#hcontainer').cycle('pause');

        // Change show/hide bg icon
        $(this).css({ backgroundPosition: 'right -22px' }).stop(true, false).animate({ height: '215' }, 1200, "easeOutCubic");

    }, function () {

        // Animate thumb bar height
        $(this).stop(true, false).animate({ height: '0' }, 1200, "easeOutCubic", function () {

            // Change show/hide bg icon
            $(this).animate({ backgroundPositionY: '0' }, 100);
            $(this).css({ backgroundPosition: 'right 0' });
        });

        // Resume cycle
        $('#hcontainer').cycle('resume');
    });

    $("#hthumbsinner > a").hover(function () {
        // Pause bg cycle (otherwise cycle animation kills thumb 
        // bar animation performance!)
        $('#hcontainer').cycle('pause');
        $(this).children('img').eq(1).fadeOut(20);
    }, function () {
        $(this).children('img').eq(1).stop(true, true).fadeIn(800);
        // Resume cycle
        $('#hcontainer').cycle('resume');

    });

    // No idea why this was here, was screwing with the video method...
    //$("#hthumbsinner > a").click(function(){ 
    //	window.location.href = $(this).attr('href');
    //});



    /* SCRIPTS FOR CT5c PAGE */
    if ($('.ct5ccontainer').length) {

        $(".ct5ccontainer .menu h2 a.reveal").click(function () {

            var index = $(".ct5ccontainer .menu h2 a.reveal").index(this);
            $(".ct5ccontainer .menu ul:not(:eq(" + index + "))").slideUp(1000, "easeOutCubic");
            $(".ct5ccontainer .menu ul:eq(" + index + ")").slideDown(1000, "easeOutCubic");

            $(".ct5ccontainer .menu h2 a.reveal").addClass("closed");
            $(this).removeClass("closed");
        });

        $(".ct5ccontainer .menu h1 a").click(function () {

            $(".ct5ccontainer .menu #collectionswrap").slideToggle(1000, "easeOutCubic");

        });



        var getwidth = $(window).width();
        var getheight = $(window).height();

        var flashvars = {
            'file': '/includes/documents/nicole_farhi_ipadcompatible.m4v',
            'controlbar': 'none',
            'autostart': 'true',
            'repeat': 'always',
            'icons': 'false',
            'stretching': 'fill',
            'screencolor': 'FFFFFF',
            'backcolor': 'FFFFFF',
            'displayclick': 'none'
        };

        var params = {
            'allowfullscreen': 'false',
            'wmode': "opaque",
            'allowscriptaccess': 'always'
        };

        var attributes = {
            'id': 'backgroundvideo',
            'name': 'backgroundvideo'

        };

        swfobject.embedSWF('/includes/jwplayer/player.swf?' + Math.floor(Math.random() * 1000), 'backgroundvideo', getwidth, getheight, '9', 'false', flashvars, params, attributes);

    }


    /* obsolete:

    var screenwidth = $(document).width();
    var screenheight = $(document).height();
    var ratio = (screenwidth / screenheight);
    if (ratio < 1.55)
    {
    var margin = $(document).height();
    margin = (margin *.12);
    $(".ct5containerouter").css('marginTop',margin);
    }
    else 
    {
    $(".ct5containerouter").css('marginTop',"3.5%");
    }

    */






    // End jquery ready
});


/*function onshadowboxclose() {

	$("#videomute").css('backgroundPosition', '0 0');
	
	// BG video won't come back unless 'close' specifically clicked
	$("#video-cont").css({opacity: 1});
	
	flowplayer().unmute();
	Shadowbox.close();

} */





















