var overPrefs = {
    'showCloseButton': true,
    'showNavArrows': false,
    'overlayOpacity': .7,
    'autoScale': false,
    'autoDimensions': true,
    'overlayColor': '#000',
    'enableEscapeButton': true,
    'transitionIn': 'none',
    //'modal': true,
    'transitionOut': 'none',
    //	'easingIn': 'easeInCirc',
    'speedIn': 200,
    'speedOut': 200,
    'scrolling': false,
    'centerOnScroll': true
}
var timer;
var nam = {
    vly: '',
    //initial default for the text size page tool
    tSize: '0',
    //current slide
    slide: '1',
    //variable if slide is hovered
    slideHover: "no",
    //variable to determine how many times hero has rotated
    pageCycles: 1,
    //variable to see if user has engaged transitions
    userEngage: "no",

    scrollSpeed: 550,
    fadeSpeed: 350,
    //Used for setTimeout timer
    timer: null,
    Ease: 'easeInOutCubic',
    emailRegEx: /[^\s]+@[^\s]+([.]\w{2,3})+/,
    init: $(document).ready(function() {

        //For PNG issue in IE6
        $(document).pngFix();

        //hide page1 and page2 after PNG has been fixed
        $('#page1').css('display', 'none');
        $('#page2').css('display', 'none');

        $('.hcpLink').fancybox(overPrefs).click(function(e) {
            //$("#hcpPixel").empty().append('<iframe width="1" height="1" frameborder="0" scrolling="No" marginheight="0" marginwidth="0" topmargin="0" leftmargin="0"></iframe>');
        });

        $('.leavePop').fancybox(overPrefs).click(function(e) {
            //$("#exitPixel").empty().append('<img height="1" width="1" src=""/>');
            var href = $(this).attr('rel');
            $('#leavePop .yesBtn').attr('href', href).click(function(e) {
                $("#exitPixel").empty().append('<img height="1" width="1" src="http://view.atdmt.com/action/SAV_Global_ExitInterstitial_Cont"/>');
            });
        });

        //disable tab indices during overlays
        $('.leavePop').click(function(e) {
            $('a').attr('tabIndex', '-1');
            $('.yesBtn').attr('tabIndex', '1');
            $('.noBtn').attr('tabIndex', '2');
            $('#fancybox-close').attr('tabIndex', '3');

        });
        $('.hcpLink').click(function(e) {
            $('a').attr('tabIndex', '-1');
            $('.yesBtn').attr('tabIndex', '1');
            $('.noBtn').attr('tabIndex', '2');
            $('#fancybox-close').attr('tabIndex', '3');

        });

        //restore tab indices after closing overlay
        $('.noBtn').click(function(e) {
            $('a').attr('tabIndex', '');
        });
        $('#fancybox-close').click(function(e) {
            $('a').attr('tabIndex', '');
        });

        $("#linkPrint").click(nam.printPage);

        $('.emailPop').click(function(e) {
            //$("#emailPixel").empty().append('<img height="1" width="1" src=""/>');
            nam.resetEmailForm();
        }).fancybox(overPrefs).click(function(e) {
            $('input[name="fName"]').focus();
        });


        // Create hover effect for tabs 
        $('#tabs a').hover(function() {
            $(this).find('span.ctt').css('background', '#ffcf4e');
            $(this).find('span.ctt').css('color', '#000000');
            $(this).find('span.lct').css('background-position', '-10px');
            $(this).find('span.rct').css('background-position', '-15px');
        },
         function() {
             $(this).find('span.ctt').css('background', '#ffecb8');
             $(this).find('span.ctt').css('color', '#5c5c5c');
             $(this).find('span.lct').css('background-position', '0px');
             $(this).find('span.rct').css('background-position', '-5px');
         });


        $('#linkPrint').hover(function() {
            $(this).find('.utilText').css('text-decoration', 'underline');
        },
         function() {
             $(this).find('.utilText').css('text-decoration', 'none');
         });
        $('#linkEmail').hover(function() {
            $(this).find('.utilText').css('text-decoration', 'underline');
        },
         function() {
             $(this).find('.utilText').css('text-decoration', 'none');
         });


        nam.vly = $('#visiLink').offset();
        $(window).scroll(nam.visiLink);

        //dynamically set width for each nextHoverButton div
        var hoverWidth = $('.nextHoverButton').find('.nextNonHover').css('width');
        $('.nextHoverButton').css('width', hoverWidth);



        //Hover state for next buttons
        $('.nextHoverButton').hover(function(e) {
            $(this).children('.nextNonHover').css('top', "-199px");
            $(this).children('.nextHover').css('top', "0px");
        },
          function(e) {
              $(this).children('.nextNonHover').css('top', "0px");
              $(this).children('.nextHover').css('top', "-199px");
          });

        //hover state for learn button
        $('.learnButton').hover(function(e) {
            $(this).children('.learnNonHover').css('top', "-199px");
            $(this).children('.learnHover').css('top', "0px");
        },
          function(e) {
              $(this).children('.learnNonHover').css('top', "0px");
              $(this).children('.learnHover').css('top', "-199px");
          });

        //hover state for downloadPrint button
        $('.downloadPrintBox').hover(function(e) {
            $(this).children('.downloadPrintNonHover').css('top', "-199px");
            $(this).children('.downloadPrintHover').css('top', "0px");
        },
          function(e) {
              $(this).children('.downloadPrintNonHover').css('top', "0px");
              $(this).children('.downloadPrintHover').css('top', "-199px");
          });

        //Hover state for DDG button
        $('.hoverButton').hover(function(e) {

            $(this).children('.nonHoverState').css('left', "-399px");
            $(this).children('.hoverState').css('left', "0px");
        },
          function(e) {
              $(this).children('.hoverState').css('left', "-399px");
              $(this).children('.nonHoverState').css('left', "0px");
          });

        //Hover state for hero buttons
        $('.heroHoverButton').hover(function(e) {
            $(this).children('.nonHoverState').css('left', '-499px');
            $(this).children('.hoverState').css('left', '24px');
        },
          function(e) {
              $(this).children('.nonHoverState').css('left', '24px');
              $(this).children('.hoverState').css('left', '-499px');
          });

        //Check if cookie for text size has been set and add appropriate class where needed
        if ($.cookies.get('tSize') != '0' && $.cookies.get('tSize') != null) {
            var a = $.cookies.get('tSize');
            nam.tSize = a.replace(/'/gi, '');
            $('#tabContent').addClass('tSize' + a.replace(/'/gi, ''));
        }
        $("#tLarge, #tSmall").click(nam.textSizeChange);


        //on mouse over, stop image rotation
        $('#heroSwipe').hover(function() {
            nam.slideHover = "yes";
        },
          function() {
              nam.slideHover = "no";
          });


        //establish page0 as current frame
        $('#page0').addClass("current");

        //attach click event for Homepage carousel
        $('#buttonNav a').click(nam.slider);


        //initial slide timer for pageload
        nam.timer = setTimeout('nam.preSlide()', 5250);

        $('input[name="email3"]').keydown(function(key) {
            if (key.keyCode == 9) {
                $('#input1').focus();
            }
        });
        $('#emailForm').keydown(function(key) {
            if (key.keyCode == 13) {
                $(this).submit();
            }
        });
        $('#submitBtn').click(function(e) {
            var valid = true;
            $('.inputPane').removeClass('invalid');
            var fn = $.trim($('input[name="fName"]').val());
            var em1 = $.trim($('input[name="email1"]').val());
            var rn = $.trim($('input[name="rName"]').val());
            var em2 = $.trim($('input[name="email2"]').val());
            var em3 = $.trim($('input[name="email3"]').val());

            if (fn == '') {
                $('#firstName').addClass('invalid');
                valid = false;
            }
            if (rn == '') {
                $('#rName').addClass('invalid');
                valid = false;
            }
            if (!nam.emailRegEx.test(em1)) {
                $('#email1').addClass('invalid');
                valid = false;
            }
            if (!nam.emailRegEx.test(em2)) {
                $('#email2').addClass('invalid');
                valid = false;
            }
            if (em2 != em3) {
                $('#email3').addClass('invalid');
                valid = false;
            }
            if (valid) {
                $.ajax({
                    url: "/EmailService.asmx/EmailPage",
                    type: 'post',
                    dataType: 'json',
                    contentType: "application/json; charset=utf-8",
                    data: "{name:'" + fn + "',email:'" + em1 + "',fname:'" + rn + "',femail:'" + em2 + "',link:'" + window.location + "'}",
                    success: function(html) {
                        $('#emailFormPre').hide();
                        $('#emailPost').empty().append(html.d).show();
                        $.fancybox.resize();
                    }
                })
                e.preventDefault();
                return false;
            } else {
                $('div.invalid:eq(0) input').focus();
                e.preventDefault();
                return false;
            }
            e.preventDefault();
        });
    }),

    // called from timers only
    preSlide: function() {
        //run nam.slider with current slide. this is the slide it will animate TO



        if (nam.slideHover == "no" && nam.userEngage == "no") {
            if (nam.pageCycles < 4) {

                nam.slider(parseInt(nam.slide));
                nam.pageCycles++;

                //increment current slide, modulus 3 so it keeps cycling to beginning
                nam.slide = (parseInt(nam.slide) + 1) % 3;
            }

        }
        else {
            nam.timer = setTimeout('nam.preSlide()', 6000);
        }


    },


    //called from preSlide and event clicks
    slider: function(e) {
        //if e = number, it means it was called by preSlide, NOT by user click

        if (typeof (e) == 'number') {
            id = e;

            //this line effectively runs this function again, but will enter the 'else' clause
            $('#buttonNav a:eq(' + e + ')').click();

            return false;
        } else {

            //extract number from link id
            var a = $(this).attr('id');
            var id = a.split('-')[1];



            nam.slide = id;

            $('#buttonNav a').removeClass('on');
            $(this).addClass('on');
            $('#buttonNav #hsl-0').attr('href', '#');
            $('#buttonNav #hsl-1').attr('href', '#');
            $('#buttonNav #hsl-2').attr('href', '#');
            $(this).removeAttr('href');
        }


        nam.slideAnim(id);
        e.preventDefault();
    },
    // the id arg determines which slide will be animated to
    /* slideAnim: function(id){
    $('#slide').animate({
    'marginLeft': '-' + parseInt(id) *s 940 + 'px'
    }, { 'duration': nam.scrollSpeed, 'easing': nam.Ease });
    clearTimeout(nam.timer);
    nam.timer = setTimeout('nam.preSlide()', 4000);
    },*/

    slideAnim: function(id) {

        if ($("#page" + id).hasClass("current")) {
            //$(".current").removeClass("current");
            //nam.preSlide();
            nam.pageCycles = 0;
            nam.userEngage = "yes";
            clearTimeout(nam.timer);
            nam.timer = setTimeout('nam.preSlide()', 2000);
        } else {
            $(".current").fadeOut(800);
            $(".current").removeClass("current");

            $("#page" + id).fadeIn(800);
            $("#page" + id).addClass("current");


            clearTimeout(nam.timer);
            nam.timer = setTimeout('nam.preSlide()', 6000);
        }



    },

    visiLink: function() {
        if (nam.vly) {
            if (window.pageYOffset > nam.vly.top || document.documentElement.scrollTop > nam.vly.top || $(window).scrollTop() > nam.vly.top) {
                nam.hugRight();
                $('#visiLink').addClass('fixedVisi');
                $(window).resize(nam.hugRight);
            } else {
                $('#visiLink').removeAttr('class').removeAttr('style');
                $(window).unbind('resize');
            }
        }
    },
    hugRight: function() {
        if ($('body').outerWidth() > 970) {
            if ($('.ie6').length > 0) {
                $('#visiLink').css({
                    top: $(window).scrollTop() - 400 + 'px'
                });
                //$('#sv').text($(window).scrollTop() + 2);
            } else if ($('.ie7').length > 0) {
                $('#visiLink').css({
                    right: ($('body').outerWidth() - 972) / 2,
                    top: '2px'
                });
            } else {
                $('#visiLink').css({
                    right: ($('body').outerWidth() - 1002) / 2,
                    top: '2px'
                });
            }
        } else {
            $('#visiLink').css('right', ($('body').outerWidth() - 986));
        }
    },
    printPage: function(e) {
        window.print();
        e.preventDefault();
    },
    textSizeChange: function(e) {
        if ($(this).text() == "+") {
            if (parseInt(nam.tSize) < 4) {
                $('#tabContent').removeClass('tSize' + nam.tSize);
                nam.tSize = parseInt(nam.tSize) + 1;
                $.cookies.set('tSize', "'" + nam.tSize + "'");
                $('#tabContent').addClass('tSize' + nam.tSize);
            }
        } else if ($(this).text() == "-") {
            if (parseInt(nam.tSize) > 1) {
                $('#tabContent').removeClass('tSize' + parseInt(nam.tSize));
                nam.tSize = parseInt(nam.tSize) - 1;
                $.cookies.set('tSize', "'" + nam.tSize + "'");
                $('#tabContent').addClass('tSize' + parseInt(nam.tSize));
            } else {
                nam.tSize = '0';
                $.cookies.set('tSize', "'0'");
                $('#tabContent').removeClass('tSize1');
            }
        }
        e.preventDefault();
    },
    resetEmailForm: function() {
        $('.inputPane').removeClass('invalid');
        $('.inputPane input').val('');
        $('#emailFormPre').show();
        $('#emailPost').empty().hide();

    }
}

