/* 
 * Author: Blaine Jester
 * RR Construction JavaScript
 * Date: 10/20/09
 * Classes: navigation
 */
(function() {
    var navigation, window = this, _nav = window.navigation;

    navigation = window.navigation = function() {
        var Private = {
            page: "",
            category: "",
            IE: null,
            load: false,
            header: {
                src: "",
                text: ""
            },
            initiated: false,
            isN: function(T) { if (T == null || T == undefined || T == 'undefined' || typeof T === "undefined" || T === undefined || T == false) { return true; } else { return false; } },
            ajax: function() {
                var xmlhttp, bComplete = false; try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); }
                catch (e) {
                    try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }
                    catch (e) {
                        try { xmlhttp = new XMLHttpRequest(); }
                        catch (e) { xmlhttp = false; }
                    }
                }
                if (!xmlhttp) return null; this.connect = function(sURL, sMethod, sVars, fnDone) {
                    if (!xmlhttp) return false; bComplete = false; sMethod = sMethod.toUpperCase(); try {
                        if (sMethod == "GET") { xmlhttp.open(sMethod, sURL + "?" + sVars, true); sVars = ""; }
                        else { xmlhttp.open(sMethod, sURL, true); xmlhttp.setRequestHeader("Method", "POST " + sURL + " HTTP/1.1"); xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); }
                        xmlhttp.onreadystatechange = function() { if (xmlhttp.readyState == 4 && !bComplete) { bComplete = true; fnDone(xmlhttp); } }; xmlhttp.send(sVars);
                    }
                    catch (z) { return false; }
                    return true;
                }; return this;
            },
            loadGeneric: function(O) {
                $(".tm").css("background-image", "url(" + O.src + ")");
                $("#welcome").html("<h4>" + O.text + "</h4>");
                $("#welcome").css({
                    background: "url(http://www.rrconstruction.com/images/logo3.png) no-repeat transparent",
                    position: "absolute",
                    top: "176px",
                    height: "124px",
                    width: "292px",
                    right: "70px",
                    'z-index': "5500",
                    display: 'none',
                    'text-align': 'right',
                    'vertical-align': 'text-bottom'
                });
                $("#welcome > h4").css({
                    position: "absolute",
                    right: "8px",
                    bottom: "5px"
                });
                $(".tm").hover(function() {
                    if (Private.IE) {
                        $("#welcome").show();
                    }
                    else {
                        $("#welcome").fadeIn();
                    }
                },
                function() {
                    if (Private.IE) {
                        $("#welcome").hide();
                    }
                    else {
                        $("#welcome").fadeOut();
                    }
                });
            },
            interval: "",
            refreshFix: function() {
                window.setTimeout(function() {
                    (!Private.loadLightBox) ? (Public.loadLightBox(true)) : ("");
                    (!Private.externalLinks) ? (Public.externalLinks(true)) : ("");
                    (!Private.events) ? (Public.events(true)) : ("");
                    (!Private.loadHeader) ? (Public.loadHeader(true)) : ("");
                    /*if (!Private.loadHeader && !Private.events && !Private.externalLinks && !Private.loadLightBox) {
                    window.clearInterval(Private.interval);
                    }*/
                }, 500);
            },
            loadLightBox: false,
            externalLinks: false,
            events: false,
            loadHeader: false
        };
        var Public = {
            init: function(O) {
                var me = this;
                for (var i in O) {
                    Private[i] = O[i];
                }
                try {
                    var test = gzip();
                    var ajax = Private.ajax();
                    if (test) {
                        ajax.connect("http://www.rrconstruction.com/scripts/get.js", "GET", "script=jquery", function(sc) {
                            var m = Private.obj;
                            m = ("\n " + m + ".loadLightBox(); " + m + ".externalLinks(); " + m + ".events(); " + m + ".loadHeader();");
                            window.eval(sc.responseText + m);
                            Private.initiated = true;
                            Private.IE = $.browser.msie;
                            if (!Private.isN(O['callback'])) {
                                O.callback("");
                            }
                            Private.refreshFix();
                        });
                    }
                }
                catch (e) {
                    if (!Private.load) {
                        var ajax = Private.ajax();
                        if (!Private.isN(ajax)) {
                            ajax.connect("http://www.rrconstruction.com/scripts/get.js", "GET", "script=jquery-uncompressed", function(sc) {
                                var m = Private.obj;
                                m = ("\n " + m + ".loadLightBox(); " + m + ".externalLinks(); " + m + ".events(); " + m + ".loadHeader();");
                                window.eval(sc.responseText + m);
                                Private.initiated = true;
                                Private.IE = $.browser.msie;
                                if (!Private.isN(O['callback'])) {
                                    O.callback("-uncompressed");
                                }
                                Private.load = true;
                                Private.refreshFix();
                            });
                        }
                    }
                }
            },
            loadHeader: function(B) {
                var F = function() {
                    var end = Private.header.src.split("?");
                    if (end.length > 1) {
                        var src = end[0].split(".");
                    }
                    else {
                        var src = Private.header.src.split(".");
                    }
                    if (src.length > 1) {
                        var exp = src[src.length - 1].toLowerCase();
                        if (exp == "swf") {
                            var url = ((!Private.load) ? ("swf") : ("swf-uncompressed"));
                            $.get("http://www.rrconstruction.com/scripts/get.js", { script: url }, function() {
                                var u = swfobject.getFlashPlayerVersion();
                                if (u.major != "") {
                                    swfobject.embedSWF(Private.header.src, "welcome", "850", "270", "8.0.0", "", "", { wmode: "transparent", quality: "high" });
                                }
                                else {
                                    Private.header.src = "a.unk";
                                    Public.loadHeader();
                                }
                            }, "script");
                        }
                        else if (exp == "jpg" || exp == "png") {
                            Private.loadGeneric({ src: Private.header.src, text: Private.header.text });
                        }
                        else {
                            Private.loadGeneric({ src: "http://www.rrconstruction.com/images/pa.jpg", text: "" });
                        }
                    }
                    else {
                        Private.loadGeneric({ src: "http://www.rrconstruction.com/images/pa.jpg", text: "" });
                    }
                    Private.loadHeader = true;
                };
                if (B) {
                    F();
                }
                else {
                    this.wait(F);
                }
            },
            loadLightBox: function(B) {
                var F = function() {
                    if (Private.category == "projects" || Private.page == "testimonials") {
                        var url = ((!Private.load) ? ("slimbox") : ("slimbox-uncompressed"));
                        $.get("http://www.rrconstruction.com/scripts/get.js", { script: url }, function() {
                            $("a img").attr("title", "Click to View Larger");
                        }, "script");
                    }
                    Private.loadLightBox = true;
                };
                if (B) {
                    F();
                }
                else {
                    this.wait(F);
                }
            },
            loadForm: function() {
                var me = this;
                if (Private.category == "subs") {
                    $("input").focus(function() {
                        $(".prow").removeClass("editing");
                        $(this).closest(".prow").addClass("editing");
                    });
                    $("#phone, #fax").keyup(function() {
                        var m = $(this).val();
                        if (m.length == 1 && m[0] == "(") {
                        }
                        else if (m.length < 15 && isNaN(m[m.length - 1])) {
                            $(this).val(m.substr(0, (m.length - 1)));
                        }
                        else {
                            if (m.length == 3 && m[0] != "(") {
                                $(this).val("(" + m + ") ");
                            }
                            else if (m.length == 4 && m[0] == "(") {
                                $(this).val(m + ") ");
                            }
                            else if (m.length == 9) {
                                $(this).val(m + "-");
                            }
                        }
                    });
                    me.form = {
                        data: {
                            'form-viewed': false
                        },
                        view: function() {
                            if (!this.data['form-viewed']) {
                                for (op in this.data) {
                                    if(op.search(/form/g) < 0)
                                        $(document.createElement('div')).html("<div class='pblock textright'>" + op + "</div><div class='pblock'>" + this.data[op] + "</div>").addClass("prow textcenter").appendTo(".main");
                                }
                                this.data['form-viewed'] = true;
                            }
                        },
                        send: function() {
                            var me = this;
                            $("input").closest(".prow").remove();
                            $("textarea").closest(".prow").remove();
                            $(document.createElement('div')).html("Sending...<br/><img src='http://www.rrconstruction.com/styles/loading.gif' />").addClass("textcenter prow loading").appendTo(".main");
                            if (!Private.isN(this.data['form-email'])) {
                                $.post("http://www.rrconstruction.com/form.php", this.data, function(d, ts) {
                                    if (ts == "success") {
                                        $(".loading").remove();
                                        $(document.createElement('div')).html(d + "<br/><a href='javascript:page.form.view();'>Click Here to View Your Submission</a>").addClass("prow textcenter").appendTo(".main");
                                    }
                                    else {
                                        me.error();
                                    }
                                }, "html");
                            }
                            else {
                                this.error();
                            }
                        },
                        error: function() {
                            $(".loading").remove();
                            $(document.createElement('div')).html("An error occurred while sending your information.<br/><a href='javascript:page.form.send();'>Click Here to Try Again</a><input type='hidden' />").addClass("prow textcenter").appendTo(".main");
                        },
                        submit: function() {
                            var me = this;
                            var flag = true;
                            $(".pblock").each(function() {
                                $(this).find("input, textarea").each(function() {
                                    if ($(this).is("*")) {
                                        $(this).attr("style", "background-color:white;");
                                        if (!$(this).hasClass("not-required") && $(this).val() == "") {
                                            flag = false;
                                            $(this).attr("style", "border:red 1px solid;");
                                        }
                                        else {
                                            me.data[$(this).attr("title")] = $(this).val();
                                        }
                                    }
                                });
                            });
                            if (!flag) {
                                alert("Please enter information for the fields marked red");
                            }
                            else {
                                this.send();
                            }
                        }
                    };
                }
            },
            externalLinks: function(B) {
                var F = function() {
                    $("a").each(function() {
                        var m = $(this);
                        if (m.attr("rel") == "external" && m.attr("href") != "") {
                            m.attr("target", "_blank");
                        }
                    });
                    Private.externalLinks = true;
                };
                if (B) {
                    F();
                }
                else {
                    this.wait(F);
                }
            },
            events: function(B) {
                var F = function() {
                    $('.nav').data('timer', { time: '0' });
                    $("#" + Private.category + " + .drop").eq(0).slideDown("normal");
                    $(".nav > *").mouseenter(function(ob) {
                        $(".nav").data("cancelHide", true);
                        clearTimeout($('.nav').data('timer').time);
                        if ($(this).is('.i') && !$(this).is("#" + Private.category)) {
                            $(this).next(".drop").eq(0).slideDown("normal");
                        }
                    });
                    $(".nav").mouseleave(function(ob) {
                        $(this).data("cancelHide", false);
                        $(this).data('timer', { time: setTimeout(function() { if (!$(".nav").data("cancelHide")) { $(".drop:not(#" + Private.category + " + .drop)").slideUp("slow"); } }, 800) });
                    });
                    $(".drop").hover(function() {
                        var c = $(this).prev(".i").eq(0).attr('id');
                        $(this).prev(".i").eq(0).addClass(c + '-hover');
                    },
                    function() {
                        var c = $(this).prev(".i").eq(0).attr('id');
                        $(this).prev(".i").eq(0).removeClass(c + '-hover');
                    });
                    Public.loadForm();
                    Private.events = true;
                };
                if (B) {
                    F();
                }
                else {
                    this.wait(F);
                }
            },
            form: {},
            debug: function() {
                return Private;
            },
            wait: function(F) {
                $(function() {
                    F();
                });
            }
        };
        return Public;
    };
})();