﻿var isNotifying = false;


if (window.location.href.toLowerCase().indexOf("/english/") > 0) {
    Cufon.replace('.homeIntro h1');
    Cufon.replace('.mainMenu > ul > li > a');
    Cufon.replace('.leftContent h1');
    Cufon.replace('.leftContent h2');
    Cufon.replace('.leftContent .genericLeft .leftNav .levelOne');
    Cufon.replace('.rightContent h1');
    Cufon.replace('.rightContent h2');
}

$(window).load(function () {
    /* Matching Left and Right Content Height */
    function equalHeight(group) {
        tallest = 0;
        group.each(function () {
            thisHeight = $(this).height();
            if (thisHeight > tallest) {
                tallest = thisHeight;
            }
        });
        group.height(tallest);
    }
    equalHeight($(".sameHeight"));
});

$(document).ready(function () {
    Cufon.replace('.mainNav div.dropDown .menuArea > a');

    mdrc.init();
   
    //    $('.mainNav li:last a').attr('class', 'loginLink');

    /* Drop Down Menu */
    $(".mainNav").dropKick({ customNodes: [2] });
    $(".mainNav").hover(function () {
        $('.photoArea').css('background-color', '#425969');
        $('.photoArea img').attr("src", 'Images/portfolio-mdrc.gif');
    });

    $('#dd-2').hover(function () {
        $('.photoArea').css('background-color', '#425969');
        $('.photoArea img').show();
    });

    $(".menuArea a").hover(function () {
        $(this).stop().animate({ backgroundColor: '#6d8391' }, 'slow');
        $('.photoArea').stop().animate({ backgroundColor: '#6d8391' }, 'slow');
        var src = $(this).attr("rel");
        $('.photoArea img').attr("src", src);
        $('.photoArea img').fadeIn('slow');
    },
        function () {
            $(this).stop().animate({ backgroundColor: '#425969' }, 'slow');
            $('.photoArea img').hide();
        });

    /* Photo Gallery */
    $(".photoGallery a").colorbox();

    var photoGalleryImg = $('.photoGallery img');

    if (photoGalleryImg.length > 0) {
        var checkIfLoaded = setInterval(function () {
            if (photoGalleryImg.get(photoGalleryImg.length - 1).complete) {
                clearInterval(checkIfLoaded);
                $('.rightContent').css('height', 'auto');
                equalHeight($(".sameHeight"));
            }
        }, 100);
    }

    /* Portfolio Slideshow */
    $('#slidePhoto1').show();
    var btnPrev = $(".slideshowControls .prev"),
        btnNext = $(".slideshowControls .next"),
        btnPause = $('.slideshowControls .pause'),
        slideshowTimer,
        isPlaying = false,
        SLIDE_DELAY = 5000,
        slidePhoto = $('.slidePhoto'),
        currentPhoto = 0;

    btnPause.attr('src', 'Images/slideshow-control-play.gif');

    btnPause.click(function () {
        if (isPlaying) {
            stopTimer();
            btnPause.attr('src', 'Images/slideshow-control-play.gif');
        } else {
            initTimer();
            btnPause.attr('src', 'Images/slideshow-control-pause.gif');
        }
    });

    var initTimer = function () {
        isPlaying = true;
        slideshowTimer = setInterval(function () {
            switchSlide(currentPhoto + 1);
        }, SLIDE_DELAY);
    };

    var stopTimer = function () {
        isPlaying = false;
        clearInterval(slideshowTimer);
    };

    function switchSlide(idx) {
        slidePhoto.eq(currentPhoto).fadeOut('slow');
        if (idx > slidePhoto.length - 1) {
            idx = 0;
        } else if (idx < 0) {
            idx = slidePhoto.length - 1;
        }
        currentPhoto = idx;
        slidePhoto.eq(currentPhoto).fadeIn('slow');
    }

    btnNext.click(function () {
        if (isPlaying) {
            stopTimer();
            initTimer();
        }
        switchSlide(currentPhoto + 1);
        return false;
    });

    btnPrev.click(function () {
        if (isPlaying) {
            stopTimer();
            initTimer();
        }
        switchSlide(currentPhoto - 1);
        return false;
    });

    var newHeight;
    /* FAQ Accordian */
    $('div.accordian> div').hide();
    $('div.accordian> h2').click(function () {
        $('div.accordian> h2').removeClass('on');
        $(this).next('div').slideToggle('slow', function () {

            //generic right is always 100px shorter than rightContent 50px padding top and bottom

            newHeight = ($('.genericRight').height() + 100) + 'px';
            $('.leftContent').animate({ 'height': newHeight }, 600);
            $('.rightContent').animate({ 'height': newHeight }, 600);

        }).siblings('div:visible').slideUp('slow').removeClass('on');
        $(this).toggleClass("on");

    });


    //Hackasuarus Rex
    $('.leftNav').find('li ul').siblings('.levelOne').each(function () {
        if ($(this).attr('href').toLowerCase().indexOf('media') > 0 || $(this).attr('href').toLowerCase().indexOf('medios') > 0) {
            $(this).addClass('disabled').siblings('ul').addClass('videos').end().click(function (e) {
                e.preventDefault();
            });
        } else if ($(this).attr('href').toLowerCase().indexOf('documents') > 0) {
            $(this).addClass('disabled').click(function (e) {
                e.preventDefault();
            });
        }
    });

    //Hiden Locations
    var hiddenLocations = $('.hiddenLocations').find('li');

    if (hiddenLocations.length > 0) {
        var idx = 0;
        $('.leftNav').find('> li').each(function () {
            $(this).find('a').append('<br/>' + hiddenLocations.eq(idx).html());
            idx++;
        });
    }

    $('.videos a').click(function (e) {
        $.colorbox({ href: '/Resources/Video/Videoplayer.html?' + $(this).attr('href'), innerHeight: 444, innerWidth: 720, iframe: true });
        e.preventDefault();
    });

});
/*MDRC*/
(function (global, undefined) {

    var mdrc = {
        init: function () {
            mdrc.security.checkThatShit();
            $('#loginConfirm').click(mdrc.ajax.signIn);
        },
        version: '0.0.1',
        formatUrl: function (str) {
            var fi = str.substring(0,1);
            return (fi !== "/") ? '/' + str : str;
        }
    };

    if (global.mdrc === undefined) {
        global.mdrc = mdrc;
    }
})(typeof window === 'undefined' ? this : window);

/*THAT AJAX SHIT*/
mdrc.ajax = function () {
    var a = {};
    a.callbacks = {};
    var ajaxPost = function (url, dataValues, callback) {
        $.ajax({
            type: 'POST',
            contentType: "application/json; charset=utf-8",
            url: url,
            dataType: 'JSON',
            cache: "false",
            data: (dataValues === null) ? null : JSON.stringify(dataValues),
            success: callback || null
        });
    };

    a.send = function (url, dataValues, callback) {
        ajaxPost(url, dataValues, callback);
    };
    a.signIn = function (evt) {
        evt.preventDefault();
        var username = $('#mdrcUsername').val();
        var password = $('#mdrcPassword').val();
        if (nullOrEmpty(username) || nullOrEmpty(password)) {
            $('#loginMessage').text('User Name and Password are Required');
            return;
        }
        ajaxPost('/mdrc.asmx/Authenticate', { username: username, password: password, actionUrl: mdrc.formatUrl($(this).attr('href')) }, mdrc.ajax.callbacks.signIn);
    };
    var nullOrEmpty = function (str) {
        return (str === null || str === '' || str === 'undefined');
    };
    a.callbacks.hasAgreed = function (response) {
        var agreed = response.d;
        if (!agreed === true) {
            $('.mainNav li:nth-child(3) > a').attr('class', 'notify');
            $('.mainNav li ul li > a').removeClass('notify');
            $('#dd-2 a').attr('class', 'notify');
        }
        if (window.location.toString().indexOf('/Portfolio/') !== -1 && agreed === false) {
            window.location = '/';
        }
        //SET LOGIN TO LAST LINK
        $('.mainNav > li:last a').attr('class', 'loginLink');
        mdrc.notify.doIt();
    };
    a.callbacks.userAgreed = function (response) {
        var actionUrl = response.d;
        window.location = actionUrl;
    };
    a.callbacks.signIn = function (response) {
        var data = response.d;
        if (data === 'fail') {
            $('#loginMessage').text('User Name and Password are invalid');
            return;
        }
        if (data === 'false') {
            $.colorbox({ html: '<p>You do not have access to this page</p>' });
            return;
        }
        window.location = data;
    };
    return a;
} ();

/*
userAgreed
signIn

*/

mdrc.security = function () {
    var s = {};
    s.checkThatShit = function () {
        mdrc.ajax.send('/mdrc.asmx/HasAgreed', null, mdrc.ajax.callbacks.hasAgreed);
    };
    s.getUrl = function (urlTail) {
        var path = /[(a-zA-Z)]+.com\/[(a-zA-Z)]+\//;
        return 'http://www.' + path.exec(window.location.toString()) + urlTail;
    };
    return s;
} ();

mdrc.notify = function () {
    return {
        noticeResponse: function (evt) {
            evt.preventDefault();
            if ($("input[name='agreeOption']:checked").val() === '1') {
                mdrc.ajax.send('/mdrc.asmx/Agreed', { actionUrl: mdrc.formatUrl($(this).attr('href')) }, mdrc.ajax.callbacks.userAgreed);
            } else {
                var cLocation = mdrc.security.getUrl('Contact-Us.aspx');
                window.location = cLocation;
            }
        },
        doIt: function () {
            var config = {
                activateLink: '.notify'
            };

            $('body').append('<div id="notify"></div>');

            var site = $('.siteContainer'),
        siteScroll = $('#siteScroll'),
        activateLink = $(config.activateLink),
        notifyWrapper = $('#notify'),
        notifyConfirm,
        notifyCancel,
        notifyContent;

            siteScroll.append('<div id="notifyOverlay"></div>');
            var notifyOverlay = $('#notifyOverlay');

            activateLink.click(function () {

                //if I would have known there were going to be two different dialogues I wouldn't have done this like a chump.
                if (!isNotifying) {

                    notifyConfirm = $('#notifyConfirm');
                    notifyCancel = $('#notifyCancel');
                    notifyWrapper.append($('#notifyContent'));
                    notifyContent = $('#notifyContent');
                    notifyConfirm.click(mdrc.notify.noticeResponse);

                    notifyCancel.click(function () {
                        closeNotify();
                        return false;
                    });
                    openNotify(mdrc.formatUrl($(this).attr('href')));
                }
                return false;
            });
            $('.loginLink').click(function (evt) {
                evt.preventDefault();
                var actionUrl = mdrc.formatUrl($(this).attr('href'));
                mdrc.ajax.send('/mdrc.asmx/HasAccess', { requestedUrl: actionUrl }, function (response) {
                    if (response.d === 'Not Authenticated') {
                        notifyConfirm = $('#loginConfirm');
                        notifyCancel = $('#loginCancel');
                        notifyWrapper.append($('#notifyLogin'));
                        notifyContent = $('#notifyLogin');
                        notifyCancel.click(function () {
                            closeNotify();
                            return false;
                        });
                        var tmp = $(this).attr('href');
                        //openNotify($(this).attr('href'));
                        openNotify(actionUrl);
                        $('#loginConfirm').attr('href', actionUrl);
                        return;
                    }
                    if (response.d == 'Not Allowed') {
                        $.colorbox({ html: '<p>You do not have access to this page</p>' });
                        return;
                    }
                    window.location = actionUrl;

                });
            });


            function openNotify(lnk) {
                isNotifying = true;
                notifyOverlay.fadeIn(0).fadeTo(0, 0).fadeTo(500, .8);
                notifyConfirm.attr('href', lnk);
                site.css('overflow', 'hidden');
                notifyWrapper.css({ 'left': site.offset().left, 'height': site.height() }).animate({ 'width': '600px' }, 600);
                siteScroll.animate({ 'paddingLeft': '600px' }, 600);
                notifyContent.fadeIn(600);
            }

            function closeNotify() {
                notifyOverlay.fadeOut(500);
                notifyWrapper.animate({ 'width': '0px' }, 600);
                notifyContent.fadeOut(600);
                siteScroll.animate({ 'paddingLeft': '0px' }, 600, function () {
                    site.attr('style', '');
                    isNotifying = false;
                });
            }
        }
    };
} ();
