﻿

$(document).ready(function() {
    $("form.formwrapper").live("submit", (function(e) {
        e.preventDefault();
        var f = $(this)
        var serializedForm = $(f).serialize();
        var action = $(f).attr("action");
        if (serializedForm.length > 0 && action.length > 0) { serializedForm += "&sendto=" + action; }
        $.post("formsprocessor.aspx", serializedForm, function(data) {
            var ra = data.split("||");
            if ((ra[0] == "OK") || (ra[0] == "HasErrors")) {
                //There is no default action
                if ((ra[2].charAt(0) == "@") && (ra[0] == "OK")) {
                    window.location.href = ra[2].substr(1) + "?" + ra[1];
                } else {
                    $(f).html(ra[2]);
                }
            }
        });
    }));

    $("#fadeHeader1").fadeIn(2500, function () {
        $("#fadeHeader2").fadeIn(2500);
    });

    $("tr.dropContent").hide();

    $(".btnPlus").live("click", function () {

        var id = $(this).attr("ref");
        $("#drop" + id).show();
        $(this).addClass("btnMinus");
        $(this).removeClass("btnPlus");

    });

    $(".btnMinus").live("click", function () {

        var id = $(this).attr("ref");
        $("#drop" + id).hide();
        $(this).addClass("btnPlus");
        $(this).removeClass("btnMinus");

    });

    $("#assaySearchResult .hot").live("click", function () {
        window.location.href = $(this).attr("pid") + ".aspx"
    });


    $('#searchResults li').live("click", function () {
        window.location.href = $(this).attr('sid')
    });

//    $("#pagination a").live("click", function () {
//        var p = $(this).attr("page");
//        dbAJAX("/scripts/yoddle/ui.aspx?", "action=page&page=" + p + salt(), pageResults);
//        return false;
//    });

    $("#assaySearchResult table tr th").live("click", function () {
        var sort = $(this).attr("sort");
        if (sort != undefined) {
            dbAJAX("/scripts/yoddle/ui.aspx?", "action=sort&sort=" + sort + salt(), refreshResults);
        }
    });
    $("#sitemap").children("#mainNav").removeAttr("id");
    $("a.tier0").live("mouseover", function() {
        $(this).parent().siblings().children(".tier1").hide("slow");
        $(".serviceListing li span").attr("class", "btnPlus");
        $(this).siblings("span").removeClass("btnPlus");
        $(this).siblings(".tier1").show("slow");
        $(this).siblings("span").addClass("btnMinus");
    });
    $(".btnPlus").live("click", function() {
        $(".tier1").hide("slow");
        $(this).siblings(".tier1").show("slow");
        $(".serviceListing li span").attr("class", "btnPlus");
        $(this).attr("class", "btnMinus");
    });
    $(".btnMinus").live("click", function() {
        $(".tier1").hide("slow");
    });

    // MODAL WINDOW

    $("#popUp .close").live("click", function() {
        HidePopUp();
    });

    $('#infobox, .contactForm').live('click', function ($e) {        
        try {
            _gaq.push(['_trackEvent', 'Contact', $e.originalEvent.type, $e.target.id]);     // google analytic tracking
        }
        catch (Error) { }

        SetPopUpContent();                                                                  // popup window
        $e ? $e.preventDefault() : (window.event.returnValue = false);                      // prevent default action as in return false
    });

});

function refreshResults(r) {
    var ra = r.split("||");

    if (ra[0] == "OK") {
        $("#lit_searchResults").html(ra[1]);
        $("#pagination").html(ra[2]);
        $("#searchResults").show();
    }
}
function pageResults(r) {
    var ra = r.split("||");

    if (ra[0] == "OK") {
        $("#lit_searchResults").html(ra[1]);
        $("#pagination").html(ra[2]);
        $("#searchResults").show();
    }
}

function submitForm(theToken, val) {
    var f = $("#" + theToken);
    var serializedForm = f.serialize();
    serializedForm = serializedForm + "&submit=" + val;
    var action = "formsprocessor.aspx";
    $.post(action, serializedForm, webFormHandler);
}

function webFormHandler(r) {
    var ra = r.split("||");
    if ((ra[0] == "OK") || (ra[0] == "HasErrors")) {
        if ((ra[2].charAt(0) == "@") && (ra[0] == "OK")) {
            window.location.href = ra[2].substr(1) + "?" + ra[1];
        } else {
            var f = "#" + ra[1]
            $(f).html(ra[2]);
        }
    }
    return false;
}
  
// infobox modal form

function SetPopUpContent() {
    
    dbAJAX("/scripts/yoddle/ui.aspx?", "action=infobox" + salt(), infoboxresult);
     
    return false;

}

function infoboxresult(r) {
    var ra = r.split("||");

    if (ra[0] == "OK") {
        $("#popUp .content").html(ra[1]);
        ShowPopUp()
        
    }
}

function ShowPopUp() {
    //get content width
    
    var w = parseInt($("#popUp").width(), 10) + parseInt($("#popUp").css("padding-left"), 10) + parseInt($("#popUp").css("padding-right"), 10);
    //alert(w / 2 + "px");

    var h = parseInt($("#popUp").height(), 10) + parseInt($("#popUp").css("padding-top"), 10) + parseInt($("#popUp").css("padding-bottom"), 10);
    //alert(h / 2 + "px");

    $("#popUp").css("margin-top",(0 - (h / 2)) + "px");
    $("#popUp").css("margin-left",(0 - (w / 2)) + "px");
    $("#popUpMask").show();
    $("#popUp").show();
}

$.fn.centerInClientGeneral = function (options) {
    var opt = { forceAbsolute: false,
        container: window,    // selector of element to center in
        completeHandler: null,
        minX: 0,
        minY: 0
    };
    $.extend(opt, options);

    return this.each(function (i) {
        var el = $(this);
        var jWin = $(opt.container);
        var isWin = opt.container == window;

        // force to the top of document to ENSURE that 
        // document absolute positioning is available
        if (opt.forceAbsolute) {
            if (isWin)
                el.remove().appendTo("body");
            else
                el.remove().appendTo(jWin.get(0));
        }

        // have to make absolute
        el.css("position", "absolute");

        // height is off a bit so fudge it
        var heightFudge = isWin ? 2.0 : 1.8;

        var x = (isWin ? jWin.width() : jWin.outerWidth()) / 2 - el.outerWidth() / 2;
        var y = (isWin ? jWin.height() : jWin.outerHeight()) / heightFudge - el.outerHeight() / 2;

        el.css("left", x < opt.minX ? opt.minX + jWin.scrollLeft() : x + jWin.scrollLeft());
        el.css("top", y < opt.minY ? opt.minY + jWin.scrollTop() : y + jWin.scrollTop());

        // if specified make callback and pass element
        if (opt.completeHandler)
            opt.completeHandler(this);
    });
}

function HidePopUp() {
    $("#popUp").hide();
    $("#popUpMask").hide();
    $("#popUp .content").html("");


}

