$(document).ready(function() {
      initCollapsable();
});

function initCollapsable() {
    // find template dir
    var tamplatePath = $("script:first").attr("src");
    tamplatePath = tamplatePath.split("script/")[0];
    
    // add ui to collapsable boxes
    $("div.collapsable_box").each(
        function() {
            $(this).prepend("<div class='collapse_button'><img src='" + tamplatePath + "images/collapse.png' alt='' border='0' /></div>").children("div.collapse_button").click(
                function() {
                    $(this).parent().children(":not(.collapse_button):not(.collpase_box_heading)").toggleClass("collapsed");
                    if ($(this).children("img").attr("src") == tamplatePath +"images/collapse.png") {
                        $(this).children("img").attr("src", tamplatePath + "images/expand.png");
                    } else {
                        $(this).children("img").attr("src", tamplatePath + "images/collapse.png");
                    }
                });
            $(this).find("h2:first").addClass("collpase_box_heading");
        }
    );
    
    // add ui to collapsed boxes
    $("div.collapsed_box").each(
        function() {
            $(this).prepend("<div class='collapse_button'><img src='" + tamplatePath + "images/expand.png' alt='' border='0' /></div>").children("div.collapse_button").click(
                function() {
                    $(this).parent().children(":not(.collapse_button):not(.collpase_box_heading)").toggleClass("collapsed");
                    if ($(this).children("img").attr("src") == tamplatePath +"images/collapse.png") {
                        $(this).children("img").attr("src", tamplatePath + "images/expand.png");
                    } else {
                        $(this).children("img").attr("src", tamplatePath + "images/collapse.png");
                    }
                });
            $(this).find("h2:first").addClass("collpase_box_heading");
        }
    ).each (
        function() {
            $(this).children(":not(.collapse_button):not(.collpase_box_heading)").addClass("collapsed");
        }
    );
}

function alternateColorTable(tableid, all) {
    if (all == undefined) {
        all = false;
    }
    var visStr = all ? "" : ":visible";
    $("#" + tableid + " tr" + visStr + ":odd").addClass("color1").removeClass("color2");
    $("#" + tableid + " tr" + visStr + ":even").addClass("color2").removeClass("color1");
}

function error(message, outOfSync) {
	$("#error_message_box #error_text").html(message); 
	$("#error_message_box").dialog("open");
}

function ajax(url, dataString, type, onSuccess, onFailure) {
    $.ajax({
        type: type,
        url: url,
        data: dataString,
        contentType: "application/x-www-form-urlencoded; charset=windows-1252",
        dataType: "json",
        success: 
            function(response, status) {
                if (typeof response == "string") {
					onSuccess(response);
				} else if (response !== null && response.className == "ErrorMessage") {
					error(response.text);
                    onFailure();
                } else {
                    onSuccess(response);
                }
            },
        error: 
            function(XMLHttpRequest, textStatus, errorThrown) {
                onFailure();
                error(textStatus);
            }
    });
    return false;
}

/**
 * Finds the form the input are in and presses the submit button on that form
 * @param {Object} input
 * @param {Object} event
 */
function submitOnEnter(input, event, submitButtonName) {
	if (event.keyCode == 13) {
		$(input).closest("form").find("[name='" + submitButtonName + "']").click();
	}
}

/**
 * Clears all inputs below selector
 */
function clearInput(selector) {
	$(selector).find(":input:not(:button):not([type='hidden'])").val("");
	$(selector).find(":checkbox").attr("checked", "");
}


function showDiv(id, visible) {
    var div = $("#" + id).toggleClass("collapsed", !visible);
    if (visible) {
        // center if floating
        // center
        if (div.css("position") == "absolute") {
            var w = div.innerWidth();
            var h = div.innerHeight();
            
            var scrolledX, scrolledY;
            if( self.pageYOffset ) {
                scrolledX = self.pageXOffset;
                scrolledY = self.pageYOffset;
            } else if( document.documentElement && document.documentElement.scrollTop ) {
                scrolledX = document.documentElement.scrollLeft;
                scrolledY = document.documentElement.scrollTop;
            } else if( document.body ) {
                scrolledX = document.body.scrollLeft;
                scrolledY = document.body.scrollTop;
            }
            var centerX, centerY;
            if( self.innerHeight ) {
                centerX = self.innerWidth;
                centerY = self.innerHeight;
            } else if( document.documentElement && document.documentElement.clientHeight ) {
                centerX = document.documentElement.clientWidth;
                centerY = document.documentElement.clientHeight;
            } else if( document.body ) {
                centerX = document.body.clientWidth;
                centerY = document.body.clientHeight;
            }
            //alert(w + " " + h + " " + screenW + " " + screenH);
            div.css({"left": scrolledX + (centerX-w)/2,
                     "top": scrolledY + (centerY-h)/2
            });
            // show blanket
            var documentH = document.body.clientHeight;
            $("#blanket").css("display", "block").css("height", documentH + "px");
            // disable scroller
            $("body").css("overflow", "hidden");
        }
        // show it with fadin
        div.hide();
        div.fadeIn("fast");
        div.find("input:visible:not(:disabled):first").focus();
    } else {
        div.hide();
        $("#blanket").hide();
        // enable scroller
        $("body").css("overflow", "auto");
    }
}


