/**
 * @author jasonroy
 */
/*global $,jQuery,document,DD_belatedPNG,navigator,window */
(function($)
{
    $.vp = 
    {
    
        //dom fragments
        domFragments: 
        {
            mainForm: {},
            pageHeader: {},
            contentPane: {},
            leftSidebar: {},
            footer: {}
        },
        
        //global variables
        globals: 
        {
            isIE6: false,
            isIE: false,
            ieScriptLocation: "http://ie7-js.googlecode.com/svn/version/2.1(beta3)/IE9.js",
            jsKitScriptLocation: "http://js-kit.com/for/labcompare.com/comments.js",
            jsKitCommentCountLocation: 'http://js-kit.com/comments-count.js'
        },
        //forms methods
        forms: 
        {
            highlight_array: [],
            
            clearSafariRadios: function()
            {
                $.each(this.highlight_array, function(i, item)
                {
                    $(item).parents('li').removeClass('highlighted');
                });
            },
            initializeFocus: function()
            {
                var fields = $.vp.domFragments.mainForm.find('.inputElements input, .inputElements select');
                fields.each($.proxy(function(i, domElement)
                {
                    if ($(domElement).attr('type') == 'radio' || $(domElement).attr('type') == 'checkbox') 
                    {
                        $(domElement).click($.proxy(function()
                        {
                            this.clearSafariRadios();
                            $(domElement).parents('li').addClass("highlighted");
                        }, this));
                        $(domElement).focus($.proxy(function()
                        {
                            this.clearSafariRadios();
                            $(domElement).parents('li').addClass("highlighted");
                        }, this));
                        this.highlight_array.splice(this.highlight_array.length, 0, domElement);
                    }
                    else 
                        if ($(domElement).attr('type') != 'button' && $(domElement).attr('type') != 'submit') 
                        {
                            $(domElement).focus($.proxy(function()
                            {
                                this.clearSafariRadios();
                                $(domElement).parents('li').addClass("highlighted");
                            }, this));
                            $(domElement).blur($.proxy(function()
                            {
                                $(domElement).parents('li').removeClass("highlighted");
                            }, this));
                        }
                }, this));
            },
            setupCollapsible: function()
            {
                $('.collapsible label:first').each(function(i, domElement)
                {
                    $(domElement).toggle(function()
                    {
                        $(domElement).siblings('.inputElements').slideDown(400);
                        $(domElement).parent().addClass("open");
                    }, function()
                    {
                        $(domElement).siblings('.inputElements').slideUp(400);
                        $(domElement).parent().removeClass("open");
                    });
                });
                
            },
            
            init: function()
            {
                this.initializeFocus();
                this.setupCollapsible();
            }
            
        },
        //helper methods used on multiple pages
        helper: 
        {
            setupLeadFormTest: function(labelLocation)
            {
                $('.formHolder').removeClass('leftLabel').addClass(labelLocation + 'Label').show();
            },
            cacheDomFragments: function()
            {
                $.vp.domFragments.mainForm = $('form#frmMain');
                $.vp.domFragments.pageHeader = $('div.pageHeader');
                $.vp.domFragments.contentPane = $('div.contentPane');
                $.vp.domFragments.leftSidebar = $('div.leftSidebar');
                $.vp.domFragments.footer = $('div.footer');
            },
            //TODO: Fix On the Server
            adjustPaging: function()
            {
                $.vp.domFragments.contentPane.find('div.pagingHolder li').each(function(i, domElement)
                {
                    if ($('a', domElement).length < 1 && !$(domElement).hasClass('active')) 
                    {
                        $(domElement).hide();
                    }
                });
            },
            adjustNavigationMenu: function(event)
            {
                $.vp.domFragments.pageHeader.find('ul.menu li:first').addClass('first').siblings(':last').addClass('last');
                if (event.menuIndex > -1) 
                {
                    $(event.menuItems[event.menuIndex - 1]).parent().addClass('prior');
                }
            },
            //TODO: Fix On the Server
            adjustArticleListingLength: function()
            {
                $.vp.domFragments.leftSidebar.find('.articleList .articleListRow').each(function(i, domElement)
                {
                    if (i > 4) 
                    {
                        $(domElement).hide();
                    }
                });
            },
            //TODO: Fix On the Server
            fixInputClasses: function()
            {
                $('input[type=text]').addClass('textbox');
                $('input[type=password]').addClass('textbox');
                $('select').addClass('dropdownList');
                $('input[type=submit]').addClass('button');
            },
            //TODO: Fix On the Server
            adjustTagControls: function()
            {
                $.vp.domFragments.contentPane.find('.tagButton').val('Add tags');
                
            },
            checkForIE6: function()
            {
                if (/MSIE ((5\.5)|6)/.test(navigator.userAgent) && navigator.platform === "Win32") 
                {
                    $.vp.globals.isIE6 = true;
                }
                return $.vp.globals.isIE6;
            },
            checkForIE: function()
            {
                $.vp.globals.isIE = !$.support.leadingWhitespace;
                return $.vp.globals.isIE;
            },
            fixIEPNG: function()
            {
                $.getScript("/Media/1/Js/IEHacks/DD_belatedPNG_0.0.8a-min.js", function()
                {
                    DD_belatedPNG.fix('.pageHeaderOuter .menuHorizontal, .pageHeaderOuter .menuHorizontal ul, .pageHeaderOuter .menuHorizontal ul li.active a, .pageHeaderOuter .menuHorizontal ul li.active a:hover, .pageHeaderOuter .menuHorizontal ul li.first a:hover, .pageHeaderOuter .menuHorizontal ul li.last a:hover, .chevronOverlay, .pageHeaderOuter .searchControl, .pageHeaderOuter .searchControl .searchButton');
                });
            },
            loadIEJSFix: function()
            {
                $.vp.domFragments.leftSidebar.find('div.featuredProductBlurb .articleListRow:last').addClass('last');
                //$.getScript($.vp.globals.ieScriptLocation);
            },
            matchContentHeights: function()
            {
                var breadCrumbHeight = 0;
                if ($.vp.domFragments.contentPane.height() < $.vp.domFragments.leftSidebar.height()) 
                {
                    if (!$('form').hasClass('Home')) 
                    {
                        breadCrumbHeight = 40;
                    }
                    $.vp.domFragments.contentPane.find('primaryContainer .primaryContainerContent').height($.vp.domFragments.leftSidebar.height() - 102 - breadCrumbHeight);
                }
            },
            setupBreadCrumb: function()
            {
                $.vp.domFragments.mainForm.append('<div class="chevronOverlay main"></div>');
                $.vp.domFragments.pageHeader.find(".breadcrumbMap").addClass("breadCrumb").jBreadCrumb(
                {
                    maxFinalElementLength: 300,
                    minFinalElementLength: 150,
                    minimumCompressionElements: 3
                });
            },
            addJsKitComments: function(commentLocation, hintLocation)
            {
                var uniq = window.location.pathname;
                var href = window.location.href;
                var commentDom = $.vp.domFragments.contentPane.find(commentLocation);
                $(hintLocation).html('<a href="#' + commentDom.attr('id') + '" class="icon comments">Comment (<span class="js-kit-comments-count" uniq="' + uniq + '">0<\/span>)<\/a><script src="http://js-kit.com/comments-count.js"><\/script>');
                commentDom.html('<div class="js-kit-comments" permalink="' + window.location.href + '" uniq="' + uniq + '"><\/div><script src="' + $.vp.globals.jsKitScriptLocation + '"><\/script>');
            }
        },
        
        //Page Specific manipulation
        pages: 
        {
            leadForm: 
            {
            
                init: function()
                {
                    this.autoCheckOptinBox();
                },
                autoCheckOptinBox: function()
                {
                    var isOptinChecked = false;
                    $.vp.domFragments.contentPane.find('.researchfields :checkbox').click(function()
                    {
                    
                        if (!isOptinChecked) 
                        {
                            $('.optin :checkbox').attr('checked', true);
                            isOptinChecked = true;
                        }
                    });
                }
            },
            
            
            
            //Adjusts the height of the main container so that there is not a large whitespace with uneven content
            
            
            homePage: 
            {
                init: function()
                {
                    this.addDirectoryCommas();
                },
                //TODO: Fix On the Server
                addDirectoryCommas: function()
                {
                    $.vp.domFragments.contentPane.find('.directoryModule ul li ul').each(function(i, domElement)
                    {
                        $(domElement).find('a:not(:last)').append(',&nbsp;&nbsp;');
                    });
                }
            },
            matrix: 
            {
                init: function()
                {
                    this.changeCompareButtonName();
                    this.setCheckboxArrows();
                },
                //TODO : Fix On Server
                changeCompareButtonName: function()
                {
                    $.vp.domFragments.contentPane.find('.matrixModule .compareButton').attr('value', 'Compare Selected');
                },
                //TODO : Fix On Server
                setCheckboxArrows: function()
                {
                    $.vp.domFragments.contentPane.find('.matrixModule .functionButtonPanel:last').addClass('last');
                }
                
            },
            productDetail: 
            {
                init: function()
                {
                    this.addBackgroundStriping();
                    this.setProductImage();
                },
                //TODO: Fix On the Server
                //Ugly fix to add background striping, will be fixed when the product detail page is re coded
                addBackgroundStriping: function()
                {
                    var rowCounter = 0;
                    $.vp.domFragments.contentPane.find('.productDetailModule table tr:first').addClass('first');
                    $.vp.domFragments.contentPane.find('.productDetailModule table tr').each(function(i, domElement)
                    {
                        if ($(domElement).hasClass('productDetailSectionHeader')) 
                        {
                            rowCounter = 1;
                        }
                        else 
                        {
                            if (rowCounter === 1) 
                            {
                                $(domElement).addClass('alternate');
                                rowCounter = 0;
                            }
                            else 
                            {
                                rowCounter = 1;
                            }
                            if ($(domElement).children('th').length > 0) 
                            {
                                $(domElement).children('th').width(180);
                                $(domElement).children('td').width(514);
                            }
                            else 
                            {
                                $('td', domElement).width('100%');
                            }
                        }
                    });
                },
                //TODO: Fix On the Server
                setProductImage: function()
                {
                
                    if ($.vp.domFragments.mainForm.find('.productDetailHeaderImage').find('img').length > 0) 
                    {
                        $.vp.domFragments.mainForm.addClass('withImage');
                    }
                }
            },
            featuredProducts: 
            {
                init: function()
                {
                    this.addReadMoreLink();
                },
                addReadMoreLink: function()
                {
                    $.vp.domFragments.contentPane.find('div.articleListRow').each(function(i, domFragment)
                    {
                        $(domFragment).find('h4 a').clone().addClass('readMore').html("read more").appendTo($(domFragment).find('div.articleListSummary'));
                    });
                }
            },
            featuredArticle: 
            {
                init: function()
                {
                    this.adjustRelatedProducts();
                    this.removeEmptyListings();
                },
                adjustRelatedProducts: function()
                {
                    $.vp.domFragments.leftSidebar.find('div.relatedProducts').each(function(i, domFragment)
                    {
                        $(domFragment).children('div:eq(2)').addClass('description').find('a').addClass('pillButton').html("More Information")
                    });
                },
                removeEmptyListings: function()
                {
                    $.vp.domFragments.leftSidebar.find('div.articleModule div.bodyText:not(:has(a))').parents('.container').remove();
                    
                }
            },
            videos: 
            
            
            {
                hasScrolled: false,
                bcModules: 
                {
                    experience: {},
                    content: {},
                    videoPlayer: {}
                
                },
                bcExperienceId: "",
                init: function()
                {
                },
                addBCListeners: function(experienceID)
                {
                    this.bcExperienceId = experienceID;
                    this.bcModules.experience = brightcove.getExperience(experienceID);
                    this.bcModules.content = this.bcModules.experience.getModule(APIModules.CONTENT);
                    this.bcModules.videoPlayer = this.bcModules.experience.getModule(APIModules.VIDEO_PLAYER);
                    this.bcModules.videoPlayer.addEventListener(BCVideoEvent.VIDEO_START, this.onVideoLoad);
                },
                onVideoLoad: function(event)
                {
                    if (!this.hasScrolled) 
                    {
                    
                        $('html, body').animate(
                        {
                            scrollTop: $("div.videoHolder").offset().top
                        }, 600);
                        this.hasScrolled = true;
                    }
                    
                },
                onVideoComplete: function(event)
                {
                
                }
            }
        },
        site: 
        {
            init: function()
            {
                //Cache HTML Fragments to avoid Dom Lookups
                $.vp.helper.cacheDomFragments();
                
                //Site Wide Helper functions
                $.vp.helper.adjustPaging();
                $.vp.helper.adjustTagControls();
                $.vp.helper.fixInputClasses();
                $.vp.helper.setupBreadCrumb();
                //$.vp.helper.adjustArticleListingLength();
                
                //Initialize Forms on any page
                $.vp.forms.init();
                
                //Set Preview Text For Search Bar
                $('#txtSearchFor', $.vp.domFragments.pageHeader).inputPreviewText();
                if ($.vp.helper.checkForIE6()) 
                
                {
                    $.vp.helper.fixIEPNG();
                    $.vp.helper.loadIEJSFix();
                }
                else 
                    if ($.vp.helper.checkForIE()) 
                    
                    {
                        $.vp.helper.loadIEJSFix();
                    }
                
                this.addEventListeners();
                this.setupCurrentPage();
            },
            addEventListeners: function(event)
            {
                $('body').bind('menuReady', $.vp.helper.adjustNavigationMenu);
            },
            setupCurrentPage: function()
            {
                var theForm = $.vp.domFragments.mainForm;
                if (theForm.hasClass('Home')) 
                {
                    $.vp.pages.homePage.init();
                    return false;
                }
                if (theForm.hasClass('LeadForm')) 
                {
                    $.vp.pages.leadForm.init();
                    return false;
                }
                if (theForm.hasClass('VerticalMatrix')) 
                
                {
                    $.vp.pages.matrix.init();
                    return false;
                }
                if (theForm.hasClass('ProductDetail')) 
                
                {
                    $.vp.pages.productDetail.init();
                    return false;
                }
                if (theForm.hasClass('FeaturedProducts')) 
                
                {
                    $.vp.pages.featuredProducts.init();
                    return false;
                }
                if (theForm.hasClass('FeaturedArticle')) 
                
                {
                    $.vp.pages.featuredArticle.init();
                }
                if (theForm.hasClass('NewsPage')) 
                {
                    $.vp.pages.featuredArticle.init();
                }
                
            }
            
        }
    
    };
    
    
    $(document).ready(function()
    {
        //Initialize the main javascript application
        $.vp.site.init();
    });
})(jQuery);

//plugins


//Input Preview Text
(function($)
{
    var _options = {};
    var _inputElement = {};
    
    function onInputFocus(eventObject)
    {
        if (_inputElement.val() === _options.inputPreviewText) 
        {
            _inputElement.val("");
        }
    }
    function onInputBlur(eventObject)
    {
        if (_inputElement.val() === "") 
        {
            _inputElement.val(_options.inputPreviewText);
        }
    }
    function init()
    {
        _inputElement.val(_options.inputPreviewText);
        _inputElement.focus(onInputFocus);
        _inputElement.blur(onInputBlur);
    }
    $.fn.inputPreviewText = function(options)
    {
        _options = $.extend({}, $.fn.inputPreviewText.defaults, options);
        _inputElement = $(this);
        init();
        return this;
    };
    //Public static options
    $.fn.inputPreviewText.defaults = 
    {
        inputPreviewText: 'Search For Products'
    };
    
    
})(jQuery);
//Global Brightcove Listener
function onTemplateLoaded(experienceID)
{
	$.vp.pages.videos.addBCListeners(experienceID);
}





