
var j = jQuery.noConflict();

function Site() {
    var I = this;
    //BEFORE DOM IS READY
    I.overlayFrame = new I.OverlayFrame();

    //WHEN DOM IS READY
    j(document).ready(function() {
        //browser-specific intialization
        I.browserInit();
        //highlight active links in nav and maybe more
        I.handleNav();
        //remove specific empty elements
        I.removeEmpty();
        //set first-child/last-child classes
        I.firstAndLastChildren();
    });

}

//browser-specific intialization
Site.prototype.browserInit = function() {
    if (j.browser.msie) {

    } else if (j.browser.safari) {

    }
}

//highlight active links in nav and maybe more
Site.prototype.handleNav = function() {
    var navHighlighter = new tbelt.url.Highlighter({
        url: document.location.href,
        alterElements: [{ element: "each", className: "active"}]
    });
    //highlight main nav
    var mainNavLinks = j("div#siteNav a");
    navHighlighter.options.links = mainNavLinks;
    navHighlighter.highlight();
   
}

//remove specific empty elements
Site.prototype.removeEmpty = function() {
    j("li").filter(":empty").remove();
    
    j("ul").each(function(i, ul) {
        if((/^\s*?$/).test(ul.innerHTML)){
            j(ul).remove();
        }
    });
}

//set first-child/last-child classes
Site.prototype.firstAndLastChildren = function() {
    var firstChildren = j(".blog-actions > ul > li:first-child, .entry-actions > li:first-child, .box-actions > li:first-child, .action-bar > ul > li:first-child");
    firstChildren.addClass("first-child");
    
    var lastChildren = j("div#nothing");
    lastChildren.addClass("last-child");
}



/* OverlayFrame SUB CLASS */
Site.prototype.OverlayFrame = function() {
    var I = this;

}

Site.prototype.OverlayFrame.prototype = {
    frame: null,
    popup: null,
    open: function(url, height, width) {
        var I = this;
        var frameHtml = "<div style='border:3px solid #C1DCEA;float:left; height: " + (height || 350) + "px; width: " + (width || 550) + "px;'><iframe frameborder='0' scrolling='no' style='height:100%;width:100%'></iframe></div>";
        //var frameHtml = "<iframe src='/dashboard/test.htm' height='" + (height || 350) + "' width='" + (width || 550) + "' frameborder='0' scrolling='no' style='border:3px solid #C1DCEA;'></iframe>";
        I.popup = new tbelt.ui.Popup({
            html: frameHtml,
            height: height,
            width: width,
            overlay: { color: "#E1EEF5" },
            displayOnLoad: true
        });
        //I.popup.open();
        I.frame = I.popup.content.children("iframe");
        //set the iframe src url - this is done outside of the html string for ie6 compatibility
        I.frame[0].src = url;
    },
    close: function(confirmed) {
        var I = this;
        if (confirmed) j("input[value='Refresh']").click();
        I.popup.close();
        delete I.overlay;
    },
    setContentScroll: function() {
        var I = this;

        var contentHeight = I.frame.contents().find("div#popupContainer").height();
        var popupHeight = I.popup.content.height();

        if (contentHeight > popupHeight) {
            var newHeight = contentHeight + 15;
            if (newHeight > j(window).height()) newHeight = j(window).height() - 30;
            //alert(I.frame.width());
            I.popup.position({ height: newHeight, width: I.popup.content.width(), animate: { duration: 300} });
        }
        //alert(contentHeight + " : " + popupHeight);
    }

}


/* CREATE THE SITE CLASS INSTANCE */
var site = new Site();
