﻿
// -------------------------------------------------------------------------| $(document).ready
$(document).ready(function()
{
    var lib = $.theknot.uw.view;
    lib.hideMainImageContinerWhenNoImage();
    //lib.breakupLongWebsiteTitleWithoutSpaces();
    lib.breakupLeftNavLinksWithoutSpaces();
    lib.positionElementsByDisposition();
    
    //lib.breakupLongTextInRightContent(45);
    lib.breakupLongTextInRightContent();
    
    if($.browser.msie)
    {
        lib.fixBottomHeaderImageForIE();
    }
});

// -------------------------------------------------------------------------| Namespace definitions.
$.theknot.uw.view = function(){};

// commonMemberElements gets written out by /view/webservices/CommonGuestviewJSElements.ashx
$.theknot.uw.view.commonGuestviewElements = {};

// -------------------------------------------------------------------------| Body
$.extend($.theknot.uw.view,
{
    fieldSelectors : 
    {
        memberNameHeader : 'span[id$=_lblMemberNameHeader]',
        pageOutputContainer : 'div.contentText',
        pageMainImageContainer : 'div.contentImg',
        
        leftNavContainer : 'div#navLeft',
        mainImageContainer : 'div.contentImg',
        rightContent : 'div#rightContent',
        bottomHeaderImageContainer : 'div#bottomheaderimg',
        tickerContainer : 'div[id$=_pnlUserTickerContainer]'
    },
    
    globals :
    {
        defaultBreakupTextPoint : 20,
        longTextBreakupMap : { '12px':45, '14px':40, '16px':35, '18px':30, '20px':25 }
    },
    
    fixBottomHeaderImageForIE : function()
    {
        var bgCss = $(this.fieldSelectors.bottomHeaderImageContainer).css('background-image');
        if(bgCss == 'none' || bgCss == '')
        {
            $(this.fieldSelectors.bottomHeaderImageContainer).hide();
        }
    },
    
    breakupLongTextInRightContent : function()
    {
        var lib = $.theknot.uw.view;
        var map = lib.globals.longTextBreakupMap;
        var defChars = lib.globals.defaultBreakupTextPoint;
        var strRexFormat = '\\w{{0}}';
        
        var rexDefMatch = new RegExp($.theknot.formatStr(strRexFormat, defChars));
        var rexMapMatch = new RegExp();
        var rexMatch = null;
        var howManyChars = 0;
        
        $(lib.fieldSelectors.rightContent).find('*').contents().filter(function() 
        {
            var boolRet = false;
            if($.browser.msie)
            {
              boolRet = (this.nodeType == 3);
            }
            else
            {
                boolRet = (this.nodeType == Node.TEXT_NODE); 
            }
            return boolRet;
        }).each(function()
        { 
            
            var parentNode = $(this.parentNode);
            
            if(typeof(parentNode) !== typeof(void(0)) && parentNode != null)
            {
                var fontSize = '';
                
                try
                {
                    /*
                    In some weird cases, "ParentNode" ends up pointing to the document object itself
                    which causes the "parentNode.css('font-size')" call to bomb. 
                    Just return form the current loop itteration if that happens.
                    */
                    fontSize = parentNode.css('font-size');
                }catch(e)
                {
                    return;
                }
                
                if(fontSize.length && typeof(map[fontSize]) !== typeof(void(0)))
                {
                    rexMapMatch.compile($.theknot.formatStr(strRexFormat, map[fontSize]));
                    rexMatch = rexMapMatch;
                    howManyChars = map[fontSize];
                }
                else
                {
                    rexMatch = rexDefMatch;
                    howManyChars = defChars;
                }
                
                if(this.nodeValue.match(rexMatch))
                {
                    $(this.parentNode).html($(this.parentNode).html().replace(this.nodeValue, $.theknot.breakupLongTextWithoutSpaces(this.nodeValue, howManyChars, '<br />')));
                }
            }
        });
    },
    
    breakupLongWebsiteTitleWithoutSpaces : function()
    {
        var fields = this.fieldSelectors;
        if(typeof($.theknot.uw.view.commonGuestviewElements.Site) !== typeof(void(0)))
        {
            if($.theknot.uw.view.commonGuestviewElements.Site.SiteConfiguration.Disposition == "Parenting")
            {
                $(fields.memberNameHeader).html($.theknot.breakupLongTextWithoutSpaces($(fields.memberNameHeader).html(), 20, '<br />'));
                
            }
        }
    },
    
    positionElementsByDisposition : function()
    {
        var fields = this.fieldSelectors;
        if(typeof($.theknot.uw.view.commonGuestviewElements.Site) !== typeof(void(0)))
        {
            if($.theknot.uw.view.commonGuestviewElements.Site.SiteConfiguration.Disposition == "Parenting")
            {
                $(fields.pageMainImageContainer).insertAfter(fields.pageOutputContainer);
                
            }
        }
    },
    
    breakupLeftNavLinksWithoutSpaces : function()
    {
        with(this.fieldSelectors)
        {
            $(leftNavContainer).find('a').each(function()
            {
                $(this).html($.theknot.breakupLongTextWithoutSpaces($(this).text(), 15, '<br />'));
            });
        }
    },
    
    hideMainImageContinerWhenNoImage : function()
    {
        with($(this.fieldSelectors.mainImageContainer))
        {
            if($.trim(html()).length == 0)
            { 
                hide(); 
            }
        }
    },
    
    showCharityPopup : function(url)
    {
        
        var popName = 'CharityInformation';
        var popProps = "toolbar=no,scrollbars=yes,location=no,statusbar=no,menubar=no,resizable=yes,width=800,height=700";
        var dirPopup = window.open(url, popName, popProps);
        //return dirPopup;
    }
});
