﻿(function() {
    var window = this,
		document = window.document,
		$ = jQuery,
		isIE = jQuery.browser.msie,
		isIE6 = jQuery.browser.msie && parseInt(jQuery.browser.version, 10) == 6,
		controller,
		view;

    controller = {
        addTableHoverEvent: function() {
            $('#tableArea tr:even').addClass('nth-child');
        },

        addTxtResizeEvent: function() {
            var centerCol = $('#mainBody .centerCol'),
                indexCol = $('#index .leftCol');

            if (centerCol.length) {
                view.txtResize(centerCol);
            }
            else if (indexCol.length) {
                view.txtResize(indexCol);
            }
        },

        addExternalLinkEvent: function() {
            $('a[rel=external]').attr('target', '_blank');
        },

        cheekyClickOutside: function(e, event) {
            $('body').bind('click', function(e) {
                var the_id = e.target.parentNode.id;

                if (the_id !== 'region') {
                    $('#regionList').hide();
                }
                if (the_id !== 'activity') {
                    $('#areaLists').hide();
                    $('#topTools').css("zIndex", "");
                }
            });
        },

        addQuickFinderEvent: function() {
            $('#customDropdown #region').click(function(e) {
                e.preventDefault();
                var selectedId = 'region';
                $('#customDropList #regionList').show();
                view.quickFinder(selectedId);
            });

            $('#customDropdown #activity').click(function(e) {
                e.preventDefault();
                var selectedId = 'area';
                $('#topTools').css("zIndex", "9");
                $('#customDropList #areaLists').show();
                view.quickFinder(selectedId);
            });
        }
    };

    view = {
        init: function() {
            view.lookForTxtCookie();
            if ($('#tableArea').length) {
                controller.addTableHoverEvent();
            }
            controller.cheekyClickOutside();
            controller.addTxtResizeEvent();
            controller.addExternalLinkEvent();
            controller.addQuickFinderEvent();
            if ($('#index').length) {
                view.equalHeights();
                view.indexBoxHoverEffect();
            }
            if ($('#tabBox').length) {
                view.tabBox();
            }
            if ($('#printMe').length) {
                view.printFn();
            }
        },

        printFn: function() {
            $('#printMe').click(function() {

                var printTxt, h1;

                if ($('h1.odd').length) {
                    h1 = $('h1.odd').text();
                }
                else if ($('#ctl00_FullContentPlaceHolder_RightBottomFullWidthArea_Content .row h1').length) {
                    h1 = $('#ctl00_FullContentPlaceHolder_RightBottomFullWidthArea_Content .row h1').text();
                }
                else if ($('.centerCol h1').length) {
                    h1 = $('.centerCol h1').text();
                }
                else if ($('#topFullWidthArea .h1').length) {
                    h1 = $('#topFullWidthArea .h1').text();
                }

                if ($('#unitContactArea').length) {
                    printTxt = $('#unitContactArea .contactListing').html();
                }
                else if ($('#tableArea').length) {
                    printTxt += $('#tableArea').html();
                }
                else if ($('.centerCol').length) {
                    $('.centerCol .row').each(function() {
                        printTxt += $(this).html();
                    });

                    if ($('.rightCol').length) {
                        printTxt += '<h1>' + $('.smallBox h3').html() + '</h1>';
                        printTxt += $('.content').html();
                    }
                }

                $('#docWrap').prepend('<div id="printPreview"><button id="closeBtn">X</button>' + '<h1>' + h1 + '</h1>' + '<div id="printWrapper">' + printTxt + '</div></div>');

                $('#printPreview a').attr('href', 'javascript:void(0);');

                $('#closeBtn').click(function() {
                    $('#printPreview').remove();
                });
            });
        },

        tabBox: function() {
            $('#tabBox .tabContent').hide();
            $('#tabs').show();
            $('#tabBox h3').remove();
            $('#tabBox .tabContent:eq(0)').show();

            var lis = document.getElementById('tabs').getElementsByTagName('li'),
                l = lis.length,
                i;

            for (i = 0; i < l; i += 1) {
                $('#tabs li').click(function(e) {
                    $('#tabs li').removeClass('active');
                    $(this).addClass('active');

                    var index = $('#tabs li').index(this)

                    $('#tabBox .tabContent').hide();
                    $('#tabBox .tabContent:eq(' + index + ')').show();
                });
            }
        },

        quickFinder: function(selectedId) {
            if (selectedId === 'region') {
                var the_ul = $('#regionList'),
                    the_lis = $('#regionList li'),
                    the_input = $('#region input');

                the_lis.click(function(e) {
                    var the_name = $(this).attr('id');
                    e.preventDefault();
                    the_input.val(the_name);
                    compareValue(selectedId, the_name);
                    the_ul.hide();
                });
            }

            else if (selectedId === 'area') {
                var the_ul = $('#areaLists'),
                    the_lis = $('#areaLists li'),
                    the_input = $('#activity input');

                the_lis.click(function(e) {
                    var the_name = $('a', this).text(),
                        the_id = $(this).attr('id');
                    e.preventDefault();
                    the_input.val(the_name);
                    compareValue(selectedId, the_name, the_id);
                    the_ul.hide();
                });
            }

            compareValue = function(selectedId, the_name, the_id) {
                if (selectedId === 'region') {
                    var the_option = $('#standardDropdown .reg option');
                    the_option.each(function() {
                        var theVal = $(this).val();
                        if (the_name === theVal) {
                            $(this).attr('selected', 'selected');
                        }
                    });
                }
                else if (selectedId === 'area') {
                    the_option = $('#standardDropdown .act option');
                    the_option.each(function() {
                        var theVal = $(this).val();
                        if (the_id === theVal) {
                            $(this).attr('selected', 'selected');
                        }
                    });

                    $('#standardDropdown .act').trigger('change');
                }

            }
        },

        txtResize: function(elem) {
            var theT = $('#fontSize span'),
                theTxtClass = elem.hasClass('txtResize');

            theT.click(function() {
                var theId = $(this).attr('id');

                // medium size
                if (theId == 'medium') {
                    if (theTxtClass) {
                        $.cookie('AttendoTxtResize', '', { path: '/', expires: -1 });
                        elem.attr('id', '');
                        elem.attr('id', 'txtSizeM');
                        $.cookie('AttendoTxtResize', 'txtSizeM', { path: '/', expires: 28 });
                    }
                    else {
                        $.cookie('AttendoTxtResize', 'txtSizeM', { path: '/', expires: 28 });
                        elem.addClass('txtResize');
                        elem.attr('id', 'txtSizeM');
                    }
                }
                // big size
                if (theId == 'big') {
                    if (theTxtClass) {
                        $.cookie('AttendoTxtResize', '', { path: '/', expires: -1 });
                        elem.attr('id', '');
                        elem.attr('id', 'txtSizeL');
                        $.cookie('AttendoTxtResize', 'txtSizeL', { path: '/', expires: 28 });
                    }
                    else {
                        $.cookie('AttendoTxtResize', 'txtSizeL', { path: '/', expires: 28 });
                        elem.addClass('txtResize');
                        elem.attr('id', 'txtSizeL');
                    }
                }
                // normalize
                if (theId == 'normal') {
                    $.cookie('AttendoTxtResize', '', { path: '/', expires: -1 });
                    elem.attr('id', '');
                    elem.removeClass('txtResize');
                }

                if (elem.parent().attr('id') == 'index') {
                    view.clearEqualHeights();
                    view.equalHeights();
                }
            });
        },

        lookForTxtCookie: function() {
            var centerCol = $('#mainBody .centerCol'),
                indexCol = $('#index .leftCol');

            if (indexCol.length && $.cookie('AttendoTxtResize')) {
                var theCookie = $.cookie('AttendoTxtResize');

                if (theCookie == 'txtSizeM') {
                    indexCol.addClass('txtResize');
                    indexCol.attr('id', 'txtSizeM');
                }
                else if (theCookie == 'txtSizeL') {
                    indexCol.addClass('txtResize');
                    indexCol.attr('id', 'txtSizeL');
                }
            }
            else if (centerCol.length && $.cookie('AttendoTxtResize')) {
                var theCookie = $.cookie('AttendoTxtResize');

                if (theCookie == 'txtSizeM') {
                    centerCol.addClass('txtResize');
                    centerCol.attr('id', 'txtSizeM');
                }
                else if (theCookie == 'txtSizeL') {
                    centerCol.addClass('txtResize');
                    centerCol.attr('id', 'txtSizeL');
                }
            }
        }
    };

    $.extend(true, $.view, view);

    $(document).ready(function() {
        view.init();
    });

})();
