$(function() {

    $("#nav>li.item:last").addClass("end").children("ul.subnav").addClass("subnavright");

    $("input[type=text], input[type=password]").watermark();

    $(".loginshow").click(function(evt) {

        $("#login").animate({
            top: 0
        }, "normal");

    });

    $(".loginclose").click(function(evt) {

        $("#login").animate({
            top: -$("#login").outerHeight()
        }, "normal");

    });

    $("form#login-form").submit(function(evt) {

        var form = $("form#login-form");

        $("form#login-form .submit").hide();
        $("form#login-form .loading").show();

        $.post("/Proxy/Login.ashx?url=" + encodeURIComponent(form.attr("action")), form.serialize(), function(data, textStatus) {

            if (data == "true") {
                //$.cookie('heatloggedin', 'true', { path: '/', domain: 'tiburoninc.com' });
                location.reload(true);
            } else {
                $("form#login-form .submit").show();
                $("form#login-form .loading").hide();
                alert("The login details you provided were not valid. Please, try again.");
            }

        });

        return false;

    });

    function closeSitemap(evt) {

        var sitemap = $("#sitemap-wrap");

        if (sitemap.not(":animated")) {

            var footer = $("#footer-wrap");

            var height = sitemap.height();
            sitemap
            $("#sitemap .closesitemap").fadeOut();

            sitemap.animate({
                height: 0,
                top: "+=" + height + "px"
            }, 750, function(evt) { $(this).hide(); });

        }

        return false;

    }

    function showSitemap(evt) {

        var sitemap = $("#sitemap-wrap");

        if (sitemap.is(":hidden") && !sitemap.is(":animated")) {

            var footer = $("#footer-wrap");

            var height = sitemap.height("auto").height();

            sitemap.height(0).css("top", footer.offset().top).show().animate({
                height: height + "px",
                top: "-=" + height + "px"
            }, 750, function(evt) { $("#sitemap .closesitemap").fadeIn(); });

        } else if (!sitemap.is(":hidden") && !sitemap.is(":animated")) {

            closeSitemap(evt);

        }

        return false;

    }

    $(".showsitemap").click(showSitemap);

    $(".closesitemap").click(closeSitemap);

    // home

    $("#hilites li.featureditem:first").show();
    $("#hilites li.chooseitem:first").show();

    $("#hilites a.nexthilite").click(function(evt) {

        var item = $("#hilites").find("li.featureditem:visible");
        item.hide();

        if (item.next().size() > 0)
            item.next("li.featureditem").show();
        else
            item.siblings(":first").show();

        return false;

    });

    $("#hilites a.prevhilite").click(function(evt) {

        var item = $("#hilites").find("li.featureditem:visible");
        item.hide();

        if (item.prev().size() > 0)
            item.prev("li.featureditem").show();
        else
            item.siblings(":last").show();

        return false;

    });

    $(".showpanel").click(function(evt) {

        var link = $(this);
        link.siblings(".active").removeClass("active").parent().toggleClass("shiftback");
        $(link.addClass("active").attr("rel")).show().siblings().hide();

        return false;

    });

    $("#sidebar li:not(.active) .content").hide();

    $("#sidebar h3 a").click(function(evt) {

        var box = $(this).parent().parent();

        if (!box.hasClass("active")) {

            box.siblings(".active").removeClass("active").children(".content").slideUp();
            box.addClass("active").children(".content").slideDown();

            return false;

        }

    });

    $("#sidebar .shadowed").shadowed();

    $("#sidebar").height($("#sidebar").height() > $("#content").height() ? $("#sidebar").height() : $("#content").height());
    $("#sidebar .static").floater();

    $("#nav li.item").hover(function(evt) { $(this).addClass("itemhover"); }, function(evt) { $(this).removeClass("itemhover"); });
    $("#nav li.subitem").hover(function(evt) { $(this).addClass("subitemhover"); }, function(evt) { $(this).removeClass("subitemhover"); });

    $("#ticker ul li:first").show().addClass("active");
    if ($("#ticker ul li").size() > 1)
        setTimeout("rotateTicker()", 6500);

    $(".accordion").haccordion();
    $(".v-accordion").vaccordion();

    function CloseLightBox(evt) { $(".lightbox, #lightbox-wrapper").hide(); $("object,embed").css("visibility", "visible"); return false; }

    function CreateLightBoxWrapper() {

        if ($("#lightbox-wrapper").size() == 0) {

            $("body").prepend("<div id='lightbox-wrapper'></div>");

            var wrapper = $("#lightbox-wrapper");

            wrapper.css({
                opacity: 0.5,
                height: $(document).height()
            }).click(CloseLightBox);

        }

        $("object,embed").css("visibility", "hidden");

        return $("#lightbox-wrapper").show();

    }

    function CreateLightBox(target) {

    }

    $(".capture-data").click(function(evt) {

        var obj = $(this);
        var redirect = obj.attr("rel");
        var target = obj.attr("target");

        if (pageTracker != null)
            pageTracker._trackPageview(redirect);

        if ($("#capture-lightbox").size() == 1) {

            $("#_hidRetUrl").val("http://www.tiburoninc.com/" + redirect);
            $("#capture-form").attr("target", target);

            CreateLightBoxWrapper();

            var lb = $("#capture-lightbox");

            lb.css({
                top: ($(window).height() / 2) - (lb.height() / 2) + $(window).scrollTop()
            }).show();

            lb.find(".close").click(CloseLightBox);

            return false;

        } else {

            if (target == "")
                location.href = redirect;
            else
                window.open(redirect);

        }

        return false;

    });

    $("#capture-form").submit(function(evt) {

        var emailtest = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
        var phonetest = /^[0-9]{3}.*[0-9]{3}.*[0-9]{4}$/;

        var txtFirstName = $("#_dl_txtFirstName");
        var txtLastName = $("#_dl_txtLastName");
        var txtTitle = $("#_dl_txtTitle");
        var txtOrg = $("#_dl_txtOrganization");
        var txtEmail = $("#_dl_txtEmail");
        var txtPhone = $("#_dl_txtPhone");
        var txtCity = $("#_dl_txtCity");
        var txtState = $("#_dl_txtState");
        var txtZip = $("#_dl_txtZip");

        if (txtFirstName.val().length < 2 || txtFirstName.val() == txtFirstName.attr("title")) {
            alert("First name is required.");
            txtFirstName.focus();
            return false;
        } else if (txtLastName.val().length < 2 || txtLastName.val() == txtLastName.attr("title")) {
            alert("Last name is required.");
            txtLastName.focus();
            return false;
        } else if (txtTitle.val().length < 2 || txtTitle.val() == txtTitle.attr("title")) {
            alert("Title is required.");
            txtTitle.focus();
            return false;
        } else if (txtOrg.val().length < 2 || txtOrg.val() == txtOrg.attr("title")) {
            alert("Organization is required.");
            txtOrg.focus();
            return false;
        } else if (!emailtest.test(txtEmail.val())) {
            alert("Valid email address is required.");
            txtEmail.focus();
            return false;
        } else if (!phonetest.test(txtPhone.val())) {
            alert("Valid phone number is required.");
            txtPhone.focus();
            return false;
        } else if (txtCity.val().length < 2 || txtCity.val() == txtCity.attr("title")) {
            alert("City is required.");
            txtCity.focus();
            return false;
        } else if (txtState.val().length < 2 || txtState.val() == txtState.attr("title")) {
            alert("State/Province is required.");
            txtState.focus();
            return false;
        } else if (txtZip.val().length < 2 || txtZip.val() == txtZip.attr("title")) {
            alert("Zip/Postal Code is required.");
            txtZip.focus();
            return false;
        }

        $.cookie('download-form', 'true', { expires: 7, path: '/', domain: 'tiburoninc.com' });

        CloseLightBox();

    });

    $(".contact-locations-map").click(function(evt) {

        var obj = $(this);

        CreateLightBoxWrapper();

        var lb = $("#map-lightbox");

        $("body").prepend(lb);

        lb.css({
            top: ($(window).height() / 2) - (lb.height() / 2) + $(window).scrollTop()
        }).show();

        lb.find("object,embed").css("visibility", "visible");

        lb.find(".close").click(CloseLightBox);

        return false;

    });

});

function rotateTicker() {

    var item = $("#ticker ul li.active");

    item.fadeOut().removeClass("active");

    if (item.next().size() > 0)
        item.next().fadeIn().addClass("active");
    else
        item.siblings(":first").fadeIn().addClass("active");

    setTimeout("rotateTicker()", 5500);

}

/*var dispatch = { src: '/fonts/dispatch.swf' };
 
sIFR.useStyleCheck = true;
sIFR.activate(dispatch);

sIFR.replace(dispatch, {
selector: 'h2',
wmode: 'transparent',
css: [
'.sIFR-root { color: #283944 }'
],
tuneHeight: -7
});

sIFR.replace(dispatch, {
selector: '#footer h6',
wmode: 'transparent',
css: [
'.sIFR-root { color: #3c505d }'
],
tuneHeight: -8
});

sIFR.replace(dispatch, {
selector: '#content .rightcol div.video h3',
wmode: 'transparent',
css: [
'.sIFR-root { color: #ff2b18 }'
],
tuneHeight: -8
});*/