﻿$(document).ready(function () {
    Latisse.setupPopups();
});

var Latisse = {};

Latisse.setupPopups = function () {
    $('.pi-link a').fancybox({
        'hideOnContentClick': false,
        'padding': 0,
        'autoDimensions': false,
        'width': 725,
        'height':402,
        'overlayShow': false
    });
}

Latisse.setupHome = function () {
    Latisse.home = new function () {
        this.setupSideBar = function () {
            Latisse.home.sideBar = {};
            Latisse.home.sideBar.touts = new function () {
                this.toutElement = '#home-touts';
                this.delay = 0;
                this.intervalID;
                this.length = $(this.toutElement + ' li').length;
                this.currentIndex = 1;
                this.selectedTout = {};
                this.fadeOut = function () {
                    if ($.support.opacity)
                        $(Latisse.home.sideBar.touts.toutElement + ' li.selected').fadeOut(Latisse.home.sideBar.touts.fadeOutCompleted)
                    else {
                        $(Latisse.home.sideBar.touts.toutElement + ' li.selected').hide();
                        Latisse.home.sideBar.touts.fadeOutCompleted();
                    }
                };
                this.fadeOutCompleted = function () {
                    $(Latisse.home.sideBar.touts.toutElement + ' li').removeClass('selected');
                    $(Latisse.home.sideBar.touts.selectedTout).addClass('selected');
                    if ($.support.opacity)
                        $(Latisse.home.sideBar.touts.selectedTout).fadeIn();
                    else {
                        $(Latisse.home.sideBar.touts.selectedTout).show();
                    }
                };
                this.startCycle = function (delay) {
                    Latisse.home.sideBar.touts.delay = delay;
                    Latisse.home.sideBar.touts.intervalID = setInterval(Latisse.home.sideBar.touts.cycle, delay);
                };
                this.stopCycle = function () {
                    clearInterval(Latisse.home.sideBar.touts.intervalID);
                };
                this.cycle = function () {
                    Latisse.home.sideBar.touts.currentIndex++;
                    if (Latisse.home.sideBar.touts.currentIndex > Latisse.home.sideBar.touts.length - 1)
                        Latisse.home.sideBar.touts.currentIndex = 0;
                    $('#touts-nav li a img').attr('src', '_/Images/100/tout_btn_hm_st.png');
                    $('#touts-nav li a img').eq(Latisse.home.sideBar.touts.currentIndex).attr('src', '_/Images/100/tout_btn_hm_act.png');
                    Latisse.home.sideBar.touts.selectedTout = $(Latisse.home.sideBar.touts.toutElement + ' li').eq(Latisse.home.sideBar.touts.currentIndex);
                    Latisse.home.sideBar.touts.fadeOut();
                };
            };
            $('#side-bar-nav ul').each(function (index) {
                $(this).css('top', -$(this).height() / 2 + $(this).prev('p').innerHeight() / 2);
                $(this).css('display', 'none');
            });
            $('#side-bar-nav > li').hoverIntent({
                sensitivity: 1,
                over: function show() {
                    var menu = $(this);
                    //if ie don't fade!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
                    if ($.support.opacity)
                        menu.children("ul").fadeIn();
                    else
                        menu.children("ul").show();
                },
                timeout: 100,
                out: function hide() {
                    var menu = $(this);
                    //if ie don't fade!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
                    if ($.support.opacity)
                        menu.children("ul").fadeOut();
                    else
                        menu.children("ul").hide();
                }
            });
            $(Latisse.home.sideBar.touts.toutElement + ' li.selected').fadeIn();
            $('#touts-nav li a').each(function (index) {
                $(this).click({ index: index }, function (event) {
                    $('#touts-nav li a img').attr('src', '_/Images/100/tout_btn_hm_st.png');
                    $(this).find('img').attr('src', '_/Images/100/tout_btn_hm_act.png');
                    Latisse.home.sideBar.touts.selectedTout = $(Latisse.home.sideBar.touts.toutElement + ' li').eq(event.data.index);
                    Latisse.home.sideBar.touts.currentIndex = event.data.index;
                    Latisse.home.sideBar.touts.fadeOut();
                    Latisse.home.sideBar.touts.stopCycle();
                    Latisse.home.sideBar.touts.startCycle(7000);
                    return false;
                });
            });
            Latisse.home.sideBar.touts.startCycle(7000);
        };
        this.setupAnimatedModule = function () {
            Latisse.home.animatedModule = new function () {
                this.debug = false;
                this.currentModuleIndex = 3;
                this.currentModule = "";
                this.moduleUrls = ['/_/Views/animated-module-clinical-study.htm',
        '/_/Views/animated-module-questions.htm',
        '/_/Views/animated-module-b-and-a.htm',
        '/_/Views/animated-module-introduce.htm'
        ];
                this.intervalID = "";
                this.loadModuleData = function (data) {
                    $('#animated-module-container').html(data);
                    Latisse.home.animatedModule.fadeIn();
                };
                this.startCycle = function (delay) {
                    Latisse.home.animatedModule.loadNextModule();
                    Latisse.home.animatedModule.delay = delay;
                    if (!Latisse.home.animatedModule.debug)
                        Latisse.home.animatedModule.intervalID = setInterval(Latisse.home.animatedModule.cycle, delay);
                };
                this.stopCycle = function () {
                    clearInterval(Latisse.home.animatedModule.intervalID);
                };
                this.cycle = function () {
                    Latisse.home.animatedModule.fadeOut(Latisse.home.animatedModule.loadNextModule);
                };
                this.loadNextModule = function () {
                    $.get(Latisse.home.animatedModule.moduleUrls[Latisse.home.animatedModule.currentModuleIndex]).success(Latisse.home.animatedModule.loadModuleData);
                    if (Latisse.home.animatedModule.currentModuleIndex < Latisse.home.animatedModule.moduleUrls.length - 1)
                        Latisse.home.animatedModule.currentModuleIndex++;
                    else
                        Latisse.home.animatedModule.currentModuleIndex = 0;
                };
                this.fadeOut = function (callback) {
                    $('#animated-module-container').fadeOut('slow', callback);
                };
                this.fadeIn = function (callback) {
                    $('#animated-module-container').fadeIn('slow', callback);
                };
                this.delay = 0;
            }
            Latisse.home.animatedModule.startCycle(8000);
        };
    };
    Latisse.home.setupSideBar();
    Latisse.home.setupAnimatedModule();
};

Latisse.setupContentPage = function () {
    Latisse.contentPage = new function () {
        this.setupSideBar = function (menu) {
            Latisse.sideMenu.initialize(menu);
            Latisse.sideMenu.highlightCurrentLink();
            Latisse.contentPage.touts = new function () {
                this.toutUrls = ['/_/Views/tout-lashexpert.htm',
        '/_/Views/tout-materials.htm',
        '/_/Views/tout-fad.htm',
        '/_/Views/tout-results.htm'
        ];
                this.toutsContainer = '#content-touts-container';
                this.showTouts = function (first, second) {
                    $(Latisse.contentPage.touts.toutsContainer).load(Latisse.contentPage.touts.toutUrls[first], function () {
                        if (second) {
                            $.get(Latisse.contentPage.touts.toutUrls[second]).success(function (data) {
                                $(Latisse.contentPage.touts.toutsContainer + ' .tout').after(data);
                            });
                        }
                    });
                };
            };

        };
        this.setupEfficacy = function () {
            Latisse.contentPage.efficacy = new function () {
                this.initializeSlider = function (slider) {
                    Latisse.contentPage.efficacy.slider = new function () {
                        this.sliderObject = slider;
                        this.currentSlide = $(slider).find('.selected');
                    };
                    $(slider).find('li a').each(function () {
                        $(this).click(function () {
                            $(Latisse.contentPage.efficacy.slider.currentSlide).toggleClass('selected');
                            Latisse.contentPage.efficacy.slider.currentSlide = $(this).parents(slider + ' li');
                            $(Latisse.contentPage.efficacy.slider.currentSlide).toggleClass('selected')
                            return false;
                        });
                    });
                };
            };
        };
        this.setupCounselingPatients = function () {
            Latisse.contentPage.counselingPatients = new function () {
                this.initialize = function () {
                    Latisse.contentPage.touts.showTouts(0);
                    this.accordion.initialize("#accordion");
                };
                this.accordion = new function () {
                    this.accordionObject = "#accordion";
                    this.initialize = function (obj) {
                        Latisse.contentPage.counselingPatients.accordion.accordionObject = obj;
                        $(obj).accordion({
                            collapsible: true,
                            header: 'h3',
                            autoHeight: false,
                            active: false
                        });
                        Latisse.contentPage.counselingPatients.accordion.expandFirstAfterDelay(1200);
                    };
                    this.expandFirstAfterDelay = function (delay) {
                        setTimeout("Latisse.contentPage.counselingPatients.accordion.expandAt(0)", delay);
                    };
                    this.expandAt = function (index) {
                        $(Latisse.contentPage.counselingPatients.accordion.accordionObject).accordion("activate", index);
                    };
                }
            };
            Latisse.contentPage.counselingPatients.initialize();
        };
        this.setupMarketingMaterials = function () {
            Latisse.contentPage.marketingMaterials = new function () {
                this.initialize = function () {
                    Latisse.contentPage.touts.showTouts(0, 3);
                    $('.marketing-materials .popup-link').fancybox({
                        'hideOnContentClick': false,
                        'padding': 0,
                        //                        'autoDimensions': false,
                        //                        'width': 725,
                        'overlayShow': false
                    });

                };
                this.downloadClicked = function () {
                    var checkedFiles = $('input:checked');
                    if ($(checkedFiles).length == 0)
                        return false;
                    var queryData = "";

                    $(checkedFiles).each(function () {
                        if (queryData != "") {
                            queryData += "&"
                        }
                        queryData += $(this).attr('id') + "=" + $(this).attr('value');
                    });
                    //                    $.ajax({
                    //                        type: "POST",
                    //                        url: "/_/Handlers/MarketingMaterials.ashx",
                    //                        data: queryData
                    //                    });
                    document.location.href = '/_/Handlers/MarketingMaterials.ashx?' + queryData;
                    return false;
                };
            };
            Latisse.contentPage.marketingMaterials.initialize();
            $('.download-link a').live('click', Latisse.contentPage.marketingMaterials.downloadClicked);
        };
        this.setupManagingExpectations = function () {
            Latisse.contentPage.managingExpectations = new function () {
                this.carousel = {};
                this.initialize = function () {
                    Latisse.contentPage.managingExpectations.carousel = Latisse.createCarousel('#progress-carousel',
                    '#progress-carousel .carousel-reel',
                    '#progress-carousel-controls');
                };
            };
            Latisse.contentPage.managingExpectations.initialize();
        };
        this.setupFindADoctor = function () {
            Latisse.contentPage.findADoctor = new function () {
                $('#location2 .location').hide();
                $('#location3 .location').hide();
                $('.add-location').each(function () {
                    var header = this;
                    $(this).children('.add-location-link').click(function () {
                        $(header).siblings('.location').eq(0).slideToggle('fast');
                        return false;
                    });
                });
            };
        };
    };
    Latisse.contentPage.setupSideBar('#side-bar-nav');
};

Latisse.sideMenu = new function () {
    this.sideMenuObject = $('#side-bar-nav');
    this.sideMenuObject = {};
    this.initialize = function (menu) {
        Latisse.sideMenu.sideMenuObject = menu;
    };
    this.highlightCurrentLink = function () {
        var loc = window.location.href.toLowerCase();
        var currentLink;
        $(Latisse.sideMenu.sideMenuObject).find('a').each(function () {
            var href = this.href.toLowerCase();
            if (href == loc || href + 'default.aspx' == loc) {
                currentLink = this;
                $(this).append('<img src="/_/Images/000/img_menu_select.png" />');
                Latisse.sideMenu.addHighlightClass(this);
                $(this).parentsUntil(Latisse.sideMenu.sideMenuObject).each(function () {
                    Latisse.sideMenu.addHighlightClass(this);
                });
            }
        });
        $(Latisse.sideMenu.sideMenuObject).children('li').each(function () {
            if (!$(this).hasClass('selected')) {
                $(this).children('ul').hide();
            }
        });
    };
    this.addHighlightClass = function (target) {
        $(target).addClass('selected');
    };
};

Latisse.createCarousel = function (carousel, carouselReel, controls) {
    var carouselObject = {};
    //    carousel.carouselElement = '#progress-carousel';
    //    carousel.externalControls = '#progress-carousel-controls';
    carouselObject.carouselReelElement = carouselReel;
    carouselObject.carouselElement = carousel;
    carouselObject.currentIndex = 0;
    carouselObject.frameWidth = {};
    carouselObject.externalControls = controls;
    carouselObject.selectedFrame = {};
    carouselObject.slide = function () {
        var newLeft = carouselObject.currentIndex * carouselObject.frameWidth;
        $(carouselObject.carouselReelElement).animate({
            left: -newLeft
        },
        'normal',
        'easeInOutCirc');
    }
    carouselObject.fadeOut = function () {
        if ($.support.opacity)
            $(carouselObject.carouselElement + ' li.selected').fadeOut(carouselObject.fadeOutCompleted)
        else {
            $(carouselObject.carouselElement + ' li.selected').hide();
            carouselObject.fadeOutCompleted();
        }
    };
    carouselObject.fadeOutCompleted = function () {
        $(carouselObject.carouselElement + ' li').removeClass('selected');
        $(carouselObject.selectedFrame).addClass('selected');
        if ($.support.opacity)
            $(carouselObject.selectedFrame).fadeIn();
        else {
            $(carouselObject.selectedFrame).show();
        }
    };
    carouselObject.setupControls = function () {
        if (carouselObject.externalControls) {
            $(carouselObject.externalControls + ' li a').each(function (index) {
                $(this).click({ index: index }, function (event) {
                    $(carouselObject.externalControls + ' li a img').attr('src', '/_/Images/100/tout_btn_hm_st.png');
                    $(this).find('img').attr('src', '/_/Images/100/tout_btn_hm_act.png');
                    carouselObject.selectedFrame = $(carouselObject.carouselElement + ' li').eq(event.data.index);
                    carouselObject.currentIndex = event.data.index;
                    carouselObject.slide();
                    return false;
                });
            });
        }
    };
    $(carouselObject.carouselElement).css('overflow', 'hidden');
    if (carouselObject.carouselReelElement) {
        carouselObject.selectedFrame = $(carouselObject.carouselReelElement + ' li:first');
        if (carouselObject.selectedFrame) {
            $(carouselObject.selectedFrame).addClass('selected');
            $(carouselObject.externalControls).find('img:first').attr('src', '/_/Images/100/tout_btn_hm_act.png');
        }
    }
    if (carouselObject.externalControls) {
        carouselObject.setupControls();
    }
    if (carouselObject.carouselReelElement) {
        carouselObject.frameWidth = $(carouselObject.carouselReelElement + ' li').outerWidth();
    }
    return carouselObject;
}

