jQuery.fn.extend({
    AjaxReady: function (fn) {
        if (fn) {
            return jQuery.event.add(this[0], "AjaxReady", fn, null);
        } else {
            var ret = jQuery.event.trigger("AjaxReady", null, this[0], false, null);
            // if there was no return value then the even validated correctly
            if (ret === undefined)
                ret = true;
            return ret;
        }
    }
});

//anything which needs to be rebound after a partial postback goes here
jQuery(document).AjaxReady(function () {
    displayDropdown();
});

jQuery(document).ready(function () {
    // Expands and contracts the Archives for blog and articles
    //---------------------------------------------------------------------
    jQuery('ul.month, .entries').each(function () {
        jQuery(this).hide();
    });

    jQuery(".year").each(function () {
        jQuery(this).toggle(
            function () {
                jQuery('#month-' + jQuery(this).text() + ',a.month').show();
                jQuery(this).addClass('expanded');
            },
            function () {
                jQuery('#month-' + jQuery(this).text() + ',a.month').hide();
                jQuery(this).removeClass('expanded');
            }
        )
    });

    jQuery("a.month").each(function () {
        jQuery(this).toggle(
            function () {
                elem = jQuery(this);
                jQuery('#' + elem.next('ul').attr('id')).show();
                jQuery(this).addClass('expanded');
            },
            function () {
                elem = jQuery(this);
                jQuery('#' + elem.next('ul').attr('id')).hide();
                jQuery(this).removeClass('expanded');
            }
        )
    });

    // Does the check for the current page
    var url = window.location.toString().replace('$', '');

    jQuery('.entries li a').each(function () {
        var myHref = $(this).attr('href');
        if (url.match(myHref.replace('$', ''))) {
            jQuery(this).parents('ul.month').siblings('a.year').trigger('click');
            jQuery(this).parents('ul.entries').siblings('a.month').trigger('click');
        }
    });

    //Hides the image border if no author image is displayed
    jQuery(".image-frame a").each(function () {
        if (jQuery(this).children().size() == 0) {
            jQuery(this).prev().hide()
        }
    });

    //---------------------------------------------------------------------
    jQuery(".txtSearch").focus(function () {
        if (this.value == this.defaultValue) {
            this.value = "";
        }
    }).blur(function () {
        if (!this.value.length) {
            this.value = this.defaultValue;
        }
    });

    if (jQuery('#selectLocation').length < 1)
        displayDropdown();

    var t = null;

    jQuery('div#nav .menu li').bind("mouseenter", function () {
        jQuery('#header .wrap .dropThis').hide();
        jQuery('div#nav .menu li').removeClass("hover");

        if (t != null) clearTimeout(t);


        jQuery(this).toggleClass("hover");

        var container = jQuery(this).find("a").attr("id").replace("item", "drop");

        if (jQuery('#header .wrap #' + container).length) {


            var dropW = jQuery('#header .wrap #' + container).outerWidth();
            var linkW = jQuery(this).outerWidth();
            var linkC = jQuery(this).offset();
            var wrapL = jQuery('#header .wrap').offset();
            var wrapLeft = wrapL.left;
            var linkLeft = linkC.left + (linkW / 2);
            var newLeft = linkLeft - (dropW / 2) - wrapLeft;

            var total = newLeft + dropW;
            var difference = total - 970;

            if (total > 970)
                newLeft = newLeft - difference;


            jQuery('#header .wrap #' + container).css("left", newLeft);
            jQuery('#header .wrap #' + container).show();
        }

    });

    jQuery('div#nav .menu li').bind("mouseleave", function () {

        var container = jQuery(this).find("a").attr("id").replace("item", "drop");

        if (!jQuery('#header .wrap #' + container).length) {
            jQuery(this).removeClass("hover");
        } else if (jQuery('#header .wrap #' + container).length) {
            t = setTimeout(function () {
                jQuery('div#nav .menu li').removeClass("hover");
                jQuery('#header .wrap #' + container).hide();
            }, 100);
            //change this to .delay
        }

    });

    jQuery('#header .wrap .dropThis').bind("mouseleave", function () {
        jQuery(this).hide();

        var container = jQuery(this).attr("id").replace("drop", "item");

        jQuery('div#nav .menu li a#' + container).parents("li").removeClass("hover");
    });

    jQuery('#header .wrap .dropThis').bind("mouseenter", function () {
        clearTimeout(t);
    });

    if (jQuery('ul.accordian').length) {

        jQuery('ul.accordian li div.content').hide();

        jQuery('ul.accordian li.single div.header-container').click(function () {

            if (jQuery(this).siblings('div.content').is(":visible")) {
                jQuery('div.content').slideUp();
            }
            else {
                jQuery('div.content').slideUp();
                jQuery(this).siblings('div.content').slideDown();
            }
        });

        jQuery('ul.accordian li.multiple div.header-container').click(function () {

            if (jQuery(this).siblings('div.content').is(":visible")) {
                jQuery(this).siblings('div.content').slideUp();
            }
            else {
                jQuery(this).siblings('div.content').slideDown();
            }
        });

    }

    jQuery('a').each(function () {
        var href = jQuery(this).attr('href');
        if (href && (href.match(/youtube\.com\/watch/i) || href.match(/youtu\.be/i) || href.match(/vimeo\.com/i) || href.indexOf('.flv') != -1 || href.indexOf('.mov') != -1)) {
            jQuery(this).attr('rel', 'prettyPhoto');
        }

        if (jQuery(this).parent(".sidebox").length) {
            jQuery("a[rel^='prettyPhoto']").addClass("caseStudyVideo");
        }
    });

    if (jQuery("a[rel^='prettyPhoto']").length) {
        jQuery("a[rel^='prettyPhoto']").prettyPhoto();
    }

    jQuery('.formPopup').each(function () {
        var urlExtension = "?iframe=true&width=60%&height=85%"
        var url = jQuery(this).attr('href');

        url = url + urlExtension;

        jQuery(this).attr('rel', 'prettyPhoto');
        jQuery(this).removeClass("caseStudyVideo");
        jQuery(this).attr('href', url);

        jQuery(this).prettyPhoto();
    });
});

function displayExpandedViewMenu(item) {
    jQuery(item).parents('ul.entries').show();
    jQuery(item).parents('.entries').prev().addClass('expanded');
    jQuery(item).parents('ul.month').show();
    jQuery(item).parents('ul.month').prev().addClass('expanded');
}

function displayDropdown() {
    if (jQuery('.jSelect').length) {

        jQuery('.jSelect').selectbox();

        jQuery('.selectbox').click(function () {
            jQuery(this).siblings('.jScrollPaneContainer').show();
        });

        jQuery('.selectbox-wrapper').jScrollPane({ scrollbarWidth: 32, reinitialiseOnImageLoad: true, maintainPosition: false, showArrows: true });
        jQuery('.jScrollPaneContainer').hide();
    }
}

function replaceText() {
    if (document.getElementById("main") != null) {
        while (jQuery("#main").html().indexOf("[youtube]") != -1) {
            var startIndex = jQuery("#main").html().indexOf("[youtube]");
            var endIndex = jQuery("#main").html().indexOf("[/youtube]");
            var removeText = jQuery("#main").html().substring(startIndex + 9, endIndex + 10);
            var urlOrg = jQuery("#main").html().substring(startIndex + 9, endIndex);
            urlSplit = urlOrg.replace('.be', 'be.com/embed');

            var height = "294";
            var width = "465";
            var align = "";
            var url = "";
            if (urlOrg.indexOf("?width=") != -1) {
                var heightInd = urlOrg.indexOf("&amp;height=");
                var widthInd = urlOrg.indexOf("?width=")
                var alignInd = urlOrg.indexOf("&amp;align=");
                width = urlOrg.substring(widthInd + 7, heightInd);
                if (alignInd == -1) {
                    height = urlOrg.substring(heightInd + 12, urlOrg.length);
                } else {
                    height = urlOrg.substring(heightInd + 12, alignInd);
                    align = urlOrg.substring(alignInd + 11, urlOrg.length);
                }
                url = urlSplit.substring(0, urlSplit.indexOf("?width="));
            } else {
            url = urlSplit;
            }
            

            var iframeStart = "<iframe align='" + align + "' width='" + width + "' height='" + height + "' src=\"";
            var iframeEnd = "\" frameborder=\"0\" allowfullscreen></iframe>";

            jQuery("#main").each(function () {

                var first = jQuery(this).html().replace('[youtube]', iframeStart + url + iframeEnd);
                var second = jQuery(this).html(first.replace(removeText, " "));
            });
        }
    }
}
jQuery(document).ready(function () {
    replaceText();
});
