$(document).ready(function(){
/*
    $('#industry_id_').mouseover(function(){

            $(this).data("origWidth", $(this).css("width")).css("width", "auto").css("position", "absolute")

        }).mouseout(function(){
            $(this).css("width", $(this).data("origWidth")).css("position", "relative");
        });
*/
	if($(".success").length > 0 && $(".success").html().length > 0){
		$(".success").after("<hr />");
		$(".success").fadeIn(2000);
	}

    /*$("#messageContainer").hide().fadeIn(1000).delay(2500).slideUp("slow");*/

	$("#refine_criteria").toggle(function(){
		$("#search_criteria").slideDown(1000);
		$("#refine_criteria").find("span").html("Hide Search Criteria");
	}, function(){
		$("#search_criteria").slideUp(1000);
		$("#refine_criteria").find("span").html("Change Search Criteria");
	});

	$("#reset_form").click(function(){
		Lightbox.confirm("Are you sure? All criteria will be lost.", function(){
			$("#frmsearch").find("input[type!=hidden]").each(function(){
				if($(this).attr("type") == "text"){ $(this).val("") }
				else if($(this).attr("type") == "checkbox"){ $(this).attr("checked", "") }});
			$("#frmsearch").find("select").each(function(){ $(this).attr("selectedIndex", 0); });
		});
        return false;
	});

});

function lookupSelect(key, value, source, define, display){

    if (key == 'country_id' && value != '244') {
        $("#region_id").find("option:first").html("Not Applicable");
        $("#region_id").val('');
        $("#region_id").attr("disabled", true);
        /* $("#region_id").attr("readonly", true); */
        $("#location_id").find("option:first").html("Not Applicable");
        $("#location_id").val('');
        $("#location_id").attr("disabled", true);
        /* $("#location_id").attr("readonly", true); */
    } else {

        if (key == 'country_id' && value == '244') {
            $("#region_id").attr("disabled", false);
            /* $("#region_id").attr("readonly", false); */
            $("#location_id").attr("disabled", false);
            /* $("#location_id").attr("readonly", false);*/
        }

        showAjax(define);
        $.get('javascript/includes/lookups.php', { key: key, value: value, source: source, define: define, display: display },
        function(data){
            $("#"+define).empty();
            $(data).find("select option").each(function(element){
                var option = $("<option></option>").attr("value", $(this).find("value").text()).html($(this).find("text").text());
                $('#'+define).append(option);
            });
            $("#region_id").find("option:first").html("Any Region");
            $("#location_id").find("option:first").html("Any Location");
            $("#ajax_"+define).hide();
        });
    }
}

function getCheckedIds(){
	var checked = [];
	$("input[id^='check'][type='checkbox']").each(function(){ if($(this).attr("checked") === true){ checked.push($(this).val()); }});
	return checked;
}

function getCheckedCount(){
	var count = 0;
	$("input[id^='check'][type='checkbox']").each(function(){ if($(this).attr("checked") === true){ count++; }});
	return count;
}

function showAjax(key){
	if($("#ajax_"+key).length == 0){
		var ajax = $("<img></img>").attr({ id: "ajax_"+key, src: "images/ajax/small.gif" }).css({ paddingTop: "1px", paddingLeft: "5px" });
		$("#"+key).after(ajax);
	}else{
		$("#ajax_"+key).show();
	}
}

