﻿// JScript File
var lw = {
    vroot: "",
    _pageInit: [],
    _pageLoad: [],
    _SearchMsg: "Search Site...",
    _Newsletter: "Type your E-mail",
    _StatusMsg: "Share your status...",
    AppendInit: function (obj) {
        lw._pageInit.push(obj);
    },
    AppendLoad: function (obj) {
        lw._pageLoad.push(obj);
    },
    load: function ($) {
        for (var i = 0; i < lw._pageLoad.length; i++)
            if ("function" == typeof lw._pageLoad[i])
                lw._pageLoad[i]();
    },
    init: function (vroot, $) {
        lw.vroot = vroot;
        lw._init($);
        lw._initAnchors();
        lw.quotes();
        try {
            lw._initSiteSearch();
        } catch (e) { }
    },
    _init: function () {
        for (var i = 0; i < lw._pageInit.length; i++)
            if ("function" == typeof lw._pageInit[i])
                lw._pageInit[i]();
    },
    CheckSiteSearch: function (f, ac) {
        if ("focus" == ac && f.value.Trim() == lw._SearchMsg) {
            f.value = '';
            f.style.color = f.oldColor;
        }
        if ("blur" == ac && f.value.Trim() == '') {
            f.oldColor = f.style.color;
            f.value = lw._SearchMsg;
            f.style.color = "#fff";
        }
    },
    CheckNewsletter: function (f, ac) {
        if ("focus" == ac && f.value.Trim() == lw._Newsletter) {
            f.value = '';
            f.style.color = f.oldColor;
        }
        if ("blur" == ac && f.value.Trim() == '') {
            f.oldColor = f.style.color;
            f.value = lw._Newsletter;
            f.style.color = "#a0a0a0";
        }
    },
    CheckStatus: function (f, ac) {
        if ("focus" == ac && f.value.Trim() == lw._StatusMsg) {
            f.value = '';
            f.style.color = f.oldColor;
        }
        if ("blur" == ac && f.value.Trim() == '') {
            f.oldColor = f.style.color;
            f.value = lw._StatusMsg;
            f.style.color = "#f1f1f1";
        }
    },
    _initSiteSearch: function () {
        var sq = $("input[name=q]");
        sq.bind("focus", function () {
            lw.CheckSiteSearch(this, 'focus');
        });
        sq.bind("blur", function () {
            lw.CheckSiteSearch(this, 'blur');
        });
        sq[0].value = "";
        lw.CheckSiteSearch(sq[0], 'blur');
        var sf = document.forms["SearchForm"];
        sf.onsubmit = function () {
            var err = sq[0].value.Trim() == lw._SearchMsg ||
				sq[0].value.Trim() == "";
            if (err) {
                alert("Please enter your search query!");
                sq[0].focus();
                return false;
            }

        }
    },
    _initNewsletter: function () {
        var sq = $("input[name=newsletter-email]");
        sq.bind("focus", function () {
            lw.CheckNewsletter(this, 'focus');
        });
        sq.bind("blur", function () {
            lw.CheckNewsletter(this, 'blur');
        });
        sq[0].value = "";
        lw.CheckNewsletter(sq[0], 'blur');
        var sf = document.forms["Newsletter"];
        sf.onsubmit = function () {
            var err = sq[0].value.Trim() == lw._Newsletter ||
				sq[0].value.Trim() == "";
            if (err) {
                alert("Please enter your email address!");
                sq[0].focus();
                return false;
            }
            err = lw_Val.ValEmail(sq[0].value);
            if (!err) {
                alert("Please enter a correct email address!");
                sq[0].focus();
                return false;
            }
            return true;
        }
    },
    _initStatus: function () {
        var sq = $("input[name=status]");
        if ($("input[name=status]")[0].value == "") {
            sq.bind("focus", function () {
                lw.CheckStatus(this, 'focus');
            });
            sq.bind("blur", function () {
                lw.CheckStatus(this, 'blur');
            });
            sq[0].value = "";
            lw.CheckStatus(sq[0], 'blur');
        }
    },
    SubmitNewsletter: function () {
        var sq = $("input[name=newsletter-email]");
        var err = sq[0].value.Trim() == lw._Newsletter ||
			sq[0].value.Trim() == "";
        if (err) {
            alert("Please enter your email.");
            sq[0].focus();
            return false;
        }
        err = lw_Val.ValEmail(sq[0].value);
        if (!err) {
            alert("Please enter your correct email address.");
            sq[0].focus();
            return false;
        }
        var url = lw.vroot + "/newsletter/newsletter.axd";
        var data = {
            action: "create",
            "email": sq[0].value.Trim()
        };
        $.ajax({
            url: url,
            data: data,
            type: "GET",
            sucess: function (d) {
                if (d.responseText == "success") {
                    alert("Your email was successfully registered!");
                    sq[0].value = "";
                    return;
                }
                alert(d.responseText);
            },
            error: function (d) {
                alert(d.responseText);
            },
            complete: function (d) {
                if (d.responseText == "success") {
                    alert("Your email was successfully registered!");
                    sq[0].value = "";
                    return;
                }
                alert(d.responseText);
            }
        });
    },
    _initAnchors: function () {
        $("a[href^=#]").bind("click", function () {
            var win = $("html");
            if ($.browser.safari)
                win = $("body");
            win.animate({
                scrollTop: $("a[name=" + this.href.split("#")[1] + "]").offset().top
            }, {
                duration: 600,
                easing: "easeInOutQuad"
            }
			);
            return false;
        });
    },
    homeTabs: function () {
        var nav = $(".tabs .navi ul li a");
        var tab = $(".tabs .tab-content");
        var currentNav = nav[0].href;
        var currentTab = tab[0].id;
        var currentParent = nav[0].parentNode;
        currentParent.className = "selected";
        tab.each(function (i, el) {
            if (i > 0) {
                $(el).css({
                    display: "none"
                });
            }
        });
        $(nav).bind("click", function () {
            var s = this.href.split('#');
            clickedNav = s[1];
            if (clickedNav == currentTab)
                return;
            currentParent.className = "";
            this.parentNode.className = "selected";
            currentParent = this.parentNode;
            $("#" + currentTab).fadeOut("slow");
            currentTab = clickedNav;
            $("#" + clickedNav).fadeIn("slow");
        });
    },
    quotes: function () {
        var quotes = 3;
        var randomNumber = Math.round(Math.random() * (quotes - 1)) + 1;
        var imgPath = (lw.vroot + '/2010/styles/images/quotes/' + randomNumber + '.jpg');
        $('#quote').css('background-image', ('url("' + imgPath + '")'));
    }
};
