jQuery(document).ready(function() {
    //Move Primary nav to appropriate spot
    handleResize();

    //Handle keeping about us green if you're on about us
    handlePersist();

    //If LeftPane is empty, hide it and resize ContentPane
    var leftContent = document.getElementById("dnn_LeftPane").innerHTML;
    if (leftContent.length > 0) { }
    else if (leftContent.length == 0) {
        jQuery("#dnn_LeftPane").hide();
        jQuery("#divMainContentArea").animate({ marginLeft: 20, width: 914 }, 500);
        jQuery("#divMainTitle").animate({ paddingLeft: 0, width: 870 }, 500);
        jQuery("#divTertiary").animate({ paddingLeft: 0 }, 500);
        jQuery("#dnn_ContentPane").animate({ paddingLeft: 0, width: 870 }, 500);
    }

    //Video load post page load
    jQuery('#imgAAA').hover(
        function() { jQuery('#divAAA').html('<iframe src="http://www.lectrus.com/video/announce.html" frameborder="0" width="320" scrolling="no" height="280"></iframe>'); },
        function() { }
    );
    jQuery('#imgBBB').hover(
        function() { jQuery('#divBBB').html('<iframe src="http://www.lectrus.com/video/AFC_EmbedPlayer.html" frameborder="0" width="320" scrolling="no" height="280"></iframe>'); },
        function() { }
    );
    jQuery('#imgCCC').hover(
        function() { jQuery('#divCCC').html('<iframe src="http://www.lectrus.com/video/safety.html" frameborder="0" width="320" scrolling="no" height="280"></iframe>'); },
        function() { }
    );
    jQuery('#imgDDD').hover(
        function() { jQuery('#divDDD').html('<iframe src="http://lectrus.com/video/iir.html" frameborder="0" width="320" scrolling="no" height="280"></iframe>'); },
        function() { }
    );
    jQuery('#imgEEE').hover(
        function() { jQuery('#divEEE').html('<iframe src="http://www.lectrus.com/video/blastResistant.html" frameborder="0" width="320" scrolling="no" height="280"></iframe>'); },
        function() { }
    );
    jQuery('#imgFFF').hover(
        function() { jQuery('#divFFF').html('<iframe src="http://www.lectrus.com/video/driscoll.html" frameborder="0" width="320" scrolling="no" height="280"></iframe>'); },
        function() { }
    );                        

    //Make Header Clickable
    jQuery("#divNavHeader").click(function() {
        location.href = "/";
    });

    //Make search clickable
    jQuery("#btnSearch").click(function() {
        var searchString = jQuery("#dnn_txtSearch").val();
        location.href = "/tabid/37/default.aspx?search=" + searchString;
    });

    //Populate contact links without direct html
    jQuery(".aContact").append('sales@lectrus.com');

    //Add Events to the Primary Navigation
    jQuery('#tblPrimaryNav img').hover(
        function() { this.src = this.src.replace("_Off", "_On"); },
        function() { this.src = this.src.replace("_On", "_Off"); }
    );

    //Show Secondary Nav
    jQuery('#btnAboutUs').hover(
        function() { jQuery('.divSecondary').hide(); jQuery("#divSecondary55").show() },
        function() { }
    );
    jQuery('#btnMarkets').hover(
        function() { jQuery('.divSecondary').hide(); jQuery("#divSecondary56").show() },
        function() { }
    );
    jQuery('#btnProducts').hover(
        function() { jQuery('.divSecondary').hide(); jQuery("#divSecondary57").show() },
        function() { }
    );
    jQuery('#btnProjects').hover(
        function() { jQuery('.divSecondary').hide(); jQuery("#divSecondary58").show() },
        function() { }
    );
    jQuery('#btnServices').hover(
        function() { jQuery('.divSecondary').hide(); jQuery("#divSecondary59").show() },
        function() { }
    );
    jQuery('#btnResources').hover(
        function() { jQuery('.divSecondary').hide(); jQuery("#divSecondary60").show() },
        function() { }
    );
    jQuery('#btnContactUs').hover(
        function() { jQuery('.divSecondary').hide(); jQuery("#divSecondary61").show() },
        function() { }
    );

    //Show Current Secondary navigation
    jQuery('.aCurrent').parent().show();

    //Add the Our Process and Timeline flash videos
    jQuery('#divOurProcess').append('<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" width="870" height="506" id="MSI-UpdateMoved" align="middle"><param name="allowScriptAccess" value="sameDomain" /><param name="allowFullScreen" value="false" /><param name="movie" value="/Portals/_default/Skins/MSI/msiProcess.swf" /><param name="quality" value="high" /><param name="bgcolor" value="#ffffff" /><embed src="/Portals/_default/Skins/MSI/msiProcess.swf" quality="high" bgcolor="#ffffff" width="870" height="506" name="MSI-UpdateMoved" align="middle" allowScriptAccess="sameDomain" allowFullScreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" /></object>');
    jQuery('#divTimeline').append('<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="861" height="294" id="msiTimelinefla" align="middle"><param name="allowScriptAccess" value="sameDomain" /><param name="allowFullScreen" value="false" /><param name="movie" value="/Portals/_default/Skins/MSI/msiTimeline.swf" /><param name="quality" value="high" /><param name="bgcolor" value="#ffffff" /><embed src="/Portals/_default/Skins/MSI/msiTimeline.swf" quality="high" bgcolor="#ffffff" width="861" height="294" name="msiTimelinefla" align="middle" allowScriptAccess="sameDomain" allowFullScreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" /></object>');

    //Add Production Option Search Functionality
    jQuery('input#inline-search').search('#divOptionElements ul li');

   jQuery('#inSpanish').attr("href","http://translate.google.com/translate?js=y&prev=_t&hl=en&ie=UTF-8&layout=1&eotf=1&sl=auto&tl=es&u=" + location.href);

    //THE END
});

function handleResize() {
    //Move Primary Navigation to the correct spot
    var windowWidth = jQuery(document).width();
    var divWidth = jQuery("div.divCenter").width();
    if (windowWidth > divWidth) {
        var leftGap = (windowWidth - divWidth) / 2;
        jQuery("#tblPrimaryNav").animate({ left: leftGap }, 500);
    }
};
var resizeTimer = null;
jQuery(window).bind('resize', function() {
    if (resizeTimer) clearTimeout(resizeTimer);
    resizeTimer = setTimeout(handleResize, 100);
});

function handlePersist() {
    var path = jQuery.url.attr("path").toString();
    var button = "";

    if (path.indexOf("/tabid/55") != -1) { button = "btnAboutUs"; }
    if (path.indexOf("/tabid/56") != -1) { button = "btnMarkets"; }
    if (path.indexOf("/tabid/57") != -1) { button = "btnProducts"; }
    if (path.indexOf("/tabid/58") != -1) { button = "btnProjects"; }
    if (path.indexOf("/tabid/59") != -1) { button = "btnServices"; }
    if (path.indexOf("/tabid/60") != -1) { button = "btnResources"; }
    if (path.indexOf("/tabid/61") != -1) { button = "btnContactUs"; }

    if (button.length > 1) {
        document.getElementById(button).src = document.getElementById(button).src.replace("nav","nav2");
    }
}

jQuery.fn.search = function(searchElements) {
    jQuery(this).keyup(function(){
        var searchString = $(this).val().toLowerCase();

        if (searchString.length > 0){
            jQuery(searchElements).hide();
            jQuery(searchElements + ':contains(' + searchString + ')').show();
        }
        else {
            jQuery(searchElements).show();
        }
    });
}; 

jQuery.url = function() { var segments = {}; var parsed = {}; var options = { url: window.location, strictMode: false, key: ["source", "protocol", "authority", "userInfo", "user", "password", "host", "port", "relative", "path", "directory", "file", "query", "anchor"], q: { name: "queryKey", parser: /(?:^|&)([^&=]*)=?([^&]*)/g }, parser: { strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/, loose: /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|jQuery)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/} }; var parseUri = function() { str = decodeURI(options.url); var m = options.parser[options.strictMode ? "strict" : "loose"].exec(str); var uri = {}; var i = 14; while (i--) { uri[options.key[i]] = m[i] || "" } uri[options.q.name] = {}; uri[options.key[12]].replace(options.q.parser, function(jQuery0, jQuery1, jQuery2) { if (jQuery1) { uri[options.q.name][jQuery1] = jQuery2 } }); return uri }; var key = function(key) { if (!parsed.length) { setUp() } if (key == "base") { if (parsed.port !== null && parsed.port !== "") { return parsed.protocol + "://" + parsed.host + ":" + parsed.port + "/" } else { return parsed.protocol + "://" + parsed.host + "/" } } return (parsed[key] === "") ? null : parsed[key] }; var param = function(item) { if (!parsed.length) { setUp() } return (parsed.queryKey[item] === null) ? null : parsed.queryKey[item] }; var setUp = function() { parsed = parseUri(); getSegments() }; var getSegments = function() { var p = parsed.path; segments = []; segments = parsed.path.length == 1 ? {} : (p.charAt(p.length - 1) == "/" ? p.substring(1, p.length - 1) : path = p.substring(1)).split("/") }; return { setMode: function(mode) { strictMode = mode == "strict" ? true : false; return this }, setUrl: function(newUri) { options.url = newUri === undefined ? window.location : newUri; setUp(); return this }, segment: function(pos) { if (!parsed.length) { setUp() } if (pos === undefined) { return segments.length } return (segments[pos] === "" || segments[pos] === undefined) ? null : segments[pos] }, attr: key, param: param} } ();