/*jslint white: true, browser: true, onevar: true, undef: true, nomen: true, eqeqeq: true, bitwise: true, regexp: true, newcap: true, immed: true */
/*global jQuery, alert, window */
jQuery(function ($) {
    // Hide the list, we're going to fade them in in a bit
    //$(".treating li").css("display", "none");
    
    // We hid them in the CSS so users without JS wouldn't see them
    $(".tabContainer").show();

    // Tabs
    $('#tabs').tabs();
    
    function loadPage(href) {
        var newloc = href.substring(0, href.indexOf(".")) + "body" + href.substring(href.indexOf("."), href.length);
        $("#main").load(newloc, function () {
            $('#tabs').tabs();
            location.href = location.protocol + "//" + location.hostname + "/" + "#" + href;
        });
    }
    $(".tab a.internal").click(function () {
        loadPage($(this).attr("href"));
        return false;
    });
    
    if (location.hash) {
        loadPage(location.hash.substring(1));
    }
    
    // Sliding items
    /*
    $(".treating li:first").fadeIn(function () {
        var index = $(".treating li").index(this);
        if (index < $(".treating li").size()) {
            $(".treating li").eq(index + 1).css("visibility", "visible").fadeIn(arguments.callee);
        }
    });
    */
    
    // sects with moving backgrounds
    /*
    $.fn.moveBackground = function () {
        var backInterval;
        $(this).each(function () {
            var $div = $(this);
            $div.hover(function () {
                var i = 0;
                //$div.css("background-image", "url(images/sidebar_background.jpg)").css("background-position", "0px " + i++ + "px");//.find("h1").add($("h2", $div)).css("color", "white");
                $div.css("background-position", "-1px " + i++ + "px");//.find("h1").add($("h2", $div)).css("color", "white");
                backInterval = setInterval(function () {
                    $div.css("background-position", "-1px " + (i += 10) + "px");
                }, 20);
            }, function () {
                clearInterval(backInterval);
                //$div.css("background-image", "none");//.find("h1").add("h2", $div).css("color", "#2f8cda");
            });
        });
    };
    */
    
    //$("div.sect").moveBackground();
    
    // preload background image
    //$("<img>").attr("src", "images/sidebar_background.jpg");
    
    // set copyright year
    $("#thisYear").text(new Date().getFullYear());
    
    // Form submission
    $("#newsletterLink").click(function () {
        jQuery.post(
            "/mailman/subscribe/newsletter_springridgechiro.com",
            {email: $("#email").val()},
            function () {
                alert("Thank you for signing up.\n\nPlease check your inbox for a confirmation email.");
            }
            );
        return false;
    });
    
    
    //go2();
    //setInterval(go2, $("div.words").size() * 3000);
    //$(".words").filter(":even").css("float", "left").end().filter(":odd").css("text-align", "right");
    
    var standardWidth = $("#testimonials2").width();
    
    $.fn.left = function (width) {
        if (typeof width === "undefined") {
            if (this.length === 0) {
                // nothing
            }
            else {
                var left = $(this).css("left");
                if (left === "auto") {
                    return 0;
                }
                if (left.substring(-2) === "px") {
                    return parseInt(left.substring(-2), 10);
                }
                else {
                    return parseInt(left, 10);
                }
            }
        }
        else {
            $(this).css("left", "" + width + "px");
            return $(this);
        }
    };
    
    function moveTestimonials() {
        if ($("#testimonials2 table").left() < -800) {
            $("#testimonials2 table").animate({left: $("#testimonials2 table").left() - standardWidth}, function () {
                $(this).hide()
                .left(standardWidth)
                .show()
                .animate({left: 0});
            });
        }
        else {
        //$("table", this).left($("table", this).left() - standardWidth);
            $("#testimonials2 table").animate({left: $("#testimonials2 table").left() - standardWidth});
        }

    }
    setInterval(moveTestimonials, 10000);
});

function showModal(id) {
    jQuery("#" + id).dialog({
        bgiframe: true,
        modal: true,
        buttons: {
            Ok: function () {
                jQuery(this).dialog('close');
            }
        },
        width: 600
    }).dialog("open");
}

/*
function go2() {
    try {
        var w = $("div.words").hide().parent().css({position: "relative"}).width();
        var h = $("div.words").parent().height();
        
        $(".words").each(function (i) {
            var $div = $(this);
            setTimeout(function () {
                $div.show().css( 
                    {top: h/2 * (Math.random() + .5), left: w/4 * (Math.random() - .5), opacity: 1, fontSize: "1em"}
                    ).animate(
                    {top: h/3 * (Math.random() + .5), left: w/4 * (Math.random() - .5), fontSize: "2.2em", opacity: 0.1}, 2000, function () {
                        $(this).hide();
                    });
                    
            }, 3000 * i);
        });
    }
    catch (e) {
        alert(e);
    }
}
*/

